argparse多选项组合

时间:2016-03-12 16:59:34

标签: python python-3.x argparse

我正在编写一个带有argparse选项的代码:

./mycode.py --nb --xsf

并被正确调用。

但是,我希望--xsf与--md选项一起使用。如果我使用

--xsf

它应该给出错误/警告,--nb不能与--md一起使用,而只能与Style="{DynamicResource MenuItemHover}"一起使用

1 个答案:

答案 0 :(得分:2)

您可以添加互斥组:

parser.add_argument("--md", help="Create xyz file", action='store_true')

group = parser.add_mutually_exclusive_group()

group.add_argument("--nb", help="show number", action='store_true')
group.add_argument("--xsf", help="Create xsf file for md(default is xyz)"
                    , action='store_true')