GNU parallel array argument

Suppose I have a python file test.py:

import os

class print_args(object):

    def__init__(self, x, y, z):

        self.x = x
        self.y = y
        self.z = z

        print(x)
        print(y)
        print(z)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--x', nargs='+', type = str)
    parser.add_argument('--y', nargs='+', type = int)
    parser.add_argument('--z', type = int)
    args = parser.parse_args()

    print_args(args.x, args.y, args.z)

I can run test.pyfrom the terminal with arguments as follows:

python3 test.py --x a b --y 2 3 --z 10

Results expected:

a b
2 3
10

How to run test.pyusing GNU-parallel in a terminal with array arguments? The solution will be equivalent to running:

python3 test.py --x a b --y 2 3 --z 10
python3 test.py --x a b --y 2 3 --z 20 
python3 test.py --x a b --y 2 3 --z 30
python3 test.py --x a b --y 2 3 --z 40

My incorrect attempt to answer my question:

parallel --link 'python3 test.py --x {1} --y {2} --z {3}' ::: \
> 'a b' 'a b' 'a b' 'a b' ::: \
> '2 3' '2 3' '2 3' '2 3' ::: \
> '10' '20' '30' '40'
+4
source share
1 answer

Hope that as discussed in the comments, this works for you:

parallel -k --dry-run python3 test.py --x "a b" --y "2 3" --z ::: 10 20 30 40

Output result

python3 test.py --x a b --y 2 3 --z 10
python3 test.py --x a b --y 2 3 --z 20
python3 test.py --x a b --y 2 3 --z 30
python3 test.py --x a b --y 2 3 --z 40

Remove the part --dry-runif the command looks good, and then run it again for the real one.

+3
source

Source: https://habr.com/ru/post/1693928/


All Articles