GNU 并行数组参数

GNU parallel array argument

假设我有一个 python 文件 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)

我可以 运行 test.py 从终端使用参数如下:

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

结果符合预期:

a b
2 3
10

我如何 运行 test.py 在带有数组参数的终端中使用 GNU parallel? 解决方案等同于 运行ning :

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

我在回答我自己的问题时的错误尝试是:

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'

希望正如评论中所讨论的那样,这对您有用:

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

示例输出

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

如果命令看起来不错,请删除 --dry-run 部分,然后 运行 再次真正执行。