我正在尝试在不同组之间建立互斥组:
我有参数-a,-b,-c,并且我想要将-a和-b一起或者-a和-c一起设置为冲突。帮助应该显示类似于[-a | ([-b] [-c])]的内容。
以下代码似乎没有互斥选项:
import argparse
parser = argparse.ArgumentParser(description='My desc')
main_group = parser.add_mutually_exclusive_group()
mysub_group = main_group.add_argument_group()
main_group.add_argument("-a", dest='a', action='store_true', default=False, help='a help')
mysub_group.add_argument("-b", dest='b', action='store_true',default=False,help='b help')
mysub_group.add_argument("-c", dest='c', action='store_true',default=False,help='c help')
parser.parse_args()
解析不同的组合 - 全部通过:
> python myscript.py -h
usage: myscript.py [-h] [-a] [-b] [-c]
My desc
optional arguments:
-h, --help show this help message and exit
-a a help
> python myscript.py -a -c
> python myscript.py -a -b
> python myscript.py -b -c
我尝试将mysub_group更改为add_mutually_exclusive_group,这会使所有内容变为互斥:
> python myscript.py -h
usage: myscript.py [-h] [-a | -b | -c]
My desc
optional arguments:
-h, --help show this help message and exit
-a a help
-b b help
-c c help
我该如何为 [-a | ([-b] [-c])] 添加参数?