Python Argparse共有独家组
我需要的是:
pro [-a xxx | [-b yyy -c zzz]]
我尝试过,但行不通。有人可以帮我吗?
group= parser.add_argument_group('Model 2')
group_ex = group.add_mutually_exclusive_group()
group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")
group_ex_2 = group_ex.add_argument_group("option 2")
group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")
group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")
谢谢!
What I need is:
pro [-a xxx | [-b yyy -c zzz]]
I tried this but does not work. Could someone help me out?
group= parser.add_argument_group('Model 2')
group_ex = group.add_mutually_exclusive_group()
group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")
group_ex_2 = group_ex.add_argument_group("option 2")
group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")
group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
add_mutaly_exclusive_group
不会使整个组相互排斥。它在组中互斥的选项。您正在寻找的是 sub-commands 。而不是prog [-a xxxx | [-b yyy -c zzz]],您必须:
使用第一组参数调用:
使用第二组参数调用:
您还可以将sub命令参数设置为位置。
有点像git或svn:
工作示例
测试它
好运。
add_mutually_exclusive_group
doesn't make an entire group mutually exclusive. It makes options within the group mutually exclusive.What you're looking for is subcommands. Instead of prog [ -a xxxx | [-b yyy -c zzz]], you'd have:
To invoke with the first set of arguments:
To invoke with the second set of arguments:
You can also set the sub command arguments as positional.
Kind of like git or svn:
Working Example
Test it
Good luck.
而 Jonathan的答案对于复杂的选项来说是完全可以的,有一个非常简单的解决方案,它将适用于简单的情况,例如1选项不包括2个其他选项,例如在
原始问题中或什至是原始问题:
这是我的操作方式:
我在此处使用命令行包装器的选项来查询mongoDB。
集合
实例可以调用方法汇总
或方法查找 with to to to to to to to to to to to to to to to to to to to to to to to to to to QUERY> QUERY>查询和>字段
,因此您会明白为什么前两个参数兼容而最后一个参数不兼容。因此,现在我运行
parser.parse_args()
并检查其内容:当然,这个小骇客仅适用于简单的情况,如果您有许多相互的相互互动,请检查所有可能的选项,将成为一场噩梦独家选项和组。在这种情况下,您应该像乔纳森(Jonathan)所建议的那样将选项分解为命令组。
While Jonathan's answer is perfectly fine for complex options, there is a very simple solution which will work for the simple cases, e.g. 1 option excludes 2 other options like in
or even as in the original question:
Here is how I would do it:
I am using here options given to a command line wrapper for querying a mongodb. The
collection
instance can either call the methodaggregate
or the methodfind
with to optional argumentsquery
andfields
, hence you see why the first two arguments are compatible and the last one isn't.So now I run
parser.parse_args()
and check it's content:Of course, this little hack is only working for simple cases and it would become a nightmare to check all the possible options if you have many mutually exclusive options and groups. In that case you should break your options in to command groups like Jonathan suggested.
如果您不想要地下书,目前可以通过相互排他性的组来完成,但是公平的警告,它涉及访问私人变量,因此请自行使用它。这个想法是您希望
-a
与-b
和-c
相互排斥,但是-b
和-c
不想彼此相互排斥,现在我们有
-a
-c
和独有-b
。请注意,它确实弄乱了帮助消息,但是您可能可以覆盖它,或者只是因为您拥有想要的功能而忽略它,这可能还是更重要的。
如果要确保我们是否使用B和C的任何一个,我们必须使用它们,然后在实例化相互排斥的组时,只需添加
quired offer = true
关键字arg即可。If you don't want subparsers, this can currently be done with mutually exclusive groups, but fair warning, it involves accessing private variables so use it at your own risk. The idea is you want
-a
to be mutually exclusive with-b
and-c
, but-b
and-c
don't want to be mutually exclusive with each otherNow we've got
-a
exclusive to both-c
and-b
.Note, it does mess up the help message, but you could probably override that, or just ignore it because you've got the functionality you want, which is probably more important anyway.
If you want to ensure if we're using any of b and c, we have to use both of them, then simply add the
required=True
keyword arg when instantiating the mutually exclusive groups.有一个Python补丁(在开发中)可以让您这样做。
http://bugs.python.org/issue10984
这个想法是允许重叠的互斥组重叠。因此,
用法
可能看起来像:更改ArgParse代码,因此您可以创建两个类似的组是简单的部分。更改
用法
格式化代码所需的编写自定义helpformatter
。在
argparse
中,行动组不会影响解析。它们只是帮助
格式化工具。在帮助
中,相互排斥的组仅影响使用
行。解析时,解析器
使用互斥组来构建潜在冲突的字典(a
不能使用b
或发生。 c
,b
在a
等)中无法发生,然后如果发生冲突,则会引起错误。没有该ArgParse补丁,我认为您的最佳选择是测试由
parse_args
您自己产生的名称空间(例如,如果a
a 和b
均具有非eftefault值),并提出自己的错误。您甚至可以使用解析器自己的错误机制。There is a python patch (in development) that would allow you to do this.
http://bugs.python.org/issue10984
The idea is to allow overlapping mutually exclusive groups. So
usage
might look like:Changing the argparse code so you can create two groups like this was the easy part. Changing the
usage
formatting code required writing a customHelpFormatter
.In
argparse
, action groups don't affect the parsing. They are just ahelp
formatting tool. In thehelp
, mutually exclusive groups only affect theusage
line. When parsing, theparser
uses the mutually exclusive groups to construct a dictionary of potential conflicts (a
can't occur withb
orc
,b
can't occur witha
, etc), and then raises an error if a conflict arises.Without that argparse patch, I think your best choice is to test the namespace produced by
parse_args
yourself (e.g. if botha
andb
have nondefault values), and raise your own error. You could even use the parser's own error mechanism.