Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python argparse创建互斥选项组?_Python_Interface_Argparse - Fatal编程技术网

如何使用python argparse创建互斥选项组?

如何使用python argparse创建互斥选项组?,python,interface,argparse,Python,Interface,Argparse,python argparse模块提供互斥选项组“add_mutual_exclusive_group”,使概要如下所示: 'script.py [-a | -b]' Example: script.py -a is valid, script.py -a -b is invalid. 'script.py [[-a | -b] | [-c | -d]]' Example: script.py -a is valid, script.py -c -d is invalid, script.

python argparse模块提供互斥选项组“add_mutual_exclusive_group”,使概要如下所示:

'script.py [-a | -b]'

Example: script.py -a is valid, script.py -a -b is invalid.
'script.py [[-a | -b] | [-c | -d]]'

Example: script.py -a is valid, script.py -c -d is invalid, script.py -a -c is invalid.
'script.py [mode1 -a -b| mode2 -c -d]'

Example: script.py mode1 -a -b is valid, script.py mode1 -a -c is invalid.
我找到了一种创建互斥组的方法,互斥组的外观如下:

'script.py [-a | -b]'

Example: script.py -a is valid, script.py -a -b is invalid.
'script.py [[-a | -b] | [-c | -d]]'

Example: script.py -a is valid, script.py -c -d is invalid, script.py -a -c is invalid.
'script.py [mode1 -a -b| mode2 -c -d]'

Example: script.py mode1 -a -b is valid, script.py mode1 -a -c is invalid.
我还了解了子命令,它们看起来是这样的:

'script.py [-a | -b]'

Example: script.py -a is valid, script.py -a -b is invalid.
'script.py [[-a | -b] | [-c | -d]]'

Example: script.py -a is valid, script.py -c -d is invalid, script.py -a -c is invalid.
'script.py [mode1 -a -b| mode2 -c -d]'

Example: script.py mode1 -a -b is valid, script.py mode1 -a -c is invalid.
我没有找到但非常想要的是:

'script.py [-a -b | -c -d]'

Example: script.py -a -b is valid, script.py -a -c is invalid.

argparse支持这一点吗?

在互斥组中没有嵌套组的方法(使用“any”或“all”逻辑)。即使您的第二个示例实际上也是一个扁平的
xor
组-只允许4个组中的一个script.py[-a |-b |-c |-d]'