Python argparse多选项组合

Python argparse多选项组合,python,python-3.x,argparse,Python,Python 3.x,Argparse,我正在使用argparse选项编写代码,如下所示: parser.add_argument("--nb", help="show number", action='store_true') parser.add_argument("--md", help="Create xyz file", action='store_true') parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)"

我正在使用argparse选项编写代码,如下所示:

parser.add_argument("--nb", help="show number", action='store_true')
parser.add_argument("--md", help="Create xyz file", action='store_true')
parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)"
                    , action='store_true')
并且被正确地调用

但是我想,比方说,-xsf与--md选项一起工作。如果我使用

./mycode.py --nb --xsf

它应该给出一个错误/警告,
--xsf
不能与
--nb
一起使用,并且只能与
--md
一起使用

您可以添加一个互斥组:

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')

您可以添加互斥组:

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')

md
nb
可以一起吗?不可以。事实上,正如我在zondo的帖子中发现的那样,
nb
md
是相互排斥的一组。我只想把
--xsf
作为
md
的一个子选项,您如何向用户解释替代方案?什么样的
用法
行比较清楚?我想的是,-h将打印md的帮助,下面是--xsf的预期帮助。nb将与md处于同一级别。例如,`ls--help`(-s选项)如果M-x组没有给你足够的测试能力,你可能只需要在解析后进行自己的测试。
md
nb
可以同时进行吗?不。事实上,正如我在zondo的帖子中发现的,
nb
md
是相互排斥的组。我只想把
--xsf
作为
md
的一个子选项,您如何向用户解释替代方案?什么样的
用法
行比较清楚?我想的是,-h将打印md的帮助,下面是--xsf的预期帮助。nb将用于md的相同级别。例如,`ls--help`(-s选项),如果M-x组没有给您足够的测试能力,您可能只需要在解析后进行自己的测试。