繁体   English   中英

argparse多选项组合

[英]argparse multiple options combination

我正在用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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM