4

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.py from the terminal with arguments as follows:

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

The results are as expected:

a b
2 3
10

How do I run test.py using GNU parallel in the terminal with array arguments? The solution would 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 at answering my own question is:

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'
Pax Vobiscum
  • 2,551
  • 2
  • 21
  • 32
ajohnrobertson
  • 267
  • 2
  • 10

1 Answers1

4

Hopefully, 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

Sample Output

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 --dry-run part if the command looks good, then run it again for real.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432