论据组之间的互斥 [英] Mutual exclusion between argument groups
问题描述
我正在尝试使用argparse模块实现以下参数依赖项:
./prog [-h | [-v schema] file]
意味着用户必须传递-h或文件,如果传递了文件,则用户可以选择传递-v模式.
I'm trying to implement the following argument dependency using the argparse module:
./prog [-h | [-v schema] file]
meaning the user must pass either -h or a file, if a file is passed the user can optionally pass -v schema.
这就是我现在所拥有的,但是似乎没有用:
That's what I have now but that doesn't seem to be working:
import argparse
parser = argparse.ArgumentParser()
mtx = parser.add_mutually_exclusive_group()
mtx.add_argument('-h', ...)
grp = mtx.add_argument_group()
grp.add_argument('-v', ...)
grp.add_argument('file', ...)
args = parser.parse_args()
您似乎无法将arg组添加到互斥组中,或者我错过了什么吗?
It looks like you can't add an arg group to a mutex group or am I missing something?
推荐答案
如果-h
表示默认帮助,那么这就是您所需要的(该帮助已经是排他的)
If -h
means the default help, then this is all you need (this help is already exclusive)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('-s','--schema')
parser.parse_args('-h'.split()) # parser.print_help()
生产
usage: stack23951543.py [-h] [-s SCHEMA] file
...
如果按-h
表示其他动作,请将其重命名为-x
.这将接近您的描述
If by -h
you mean some other action, lets rename it -x
. This would come close to what you describe
parser = argparse.ArgumentParser()
parser.add_argument('-s','--schema', default='meaningful default value')
mxg = parser.add_mutually_exclusive_group(required=True)
mxg.add_argument('-x','--xxx', action='store_true')
mxg.add_argument('file', nargs='?')
parser.parse_args('-h'.split())
用法是:
usage: stack23951543.py [-h] [-s SCHEMA] (-x | file)
现在需要-x
或file
(但不是两个).在任何一种情况下,-s
都是可选的,但是具有有意义的默认值,是否将其忽略并不重要.如果给出了-x
,则可以忽略-s
值.
Now -x
or file
is required (but not both). -s
is optional in either case, but with a meaningful default, it doesn't matter if it is omitted. And if -x
is given, you can just ignore the -s
value.
如果有必要,您可以在解析后测试args
,以确认如果args.file
不为None,则args.schema
也不能.
If necessary you could test args
after parsing, to confirm that if args.file
is not None, then args.schema
can't be either.
我以前写过(也许是在思考问题):
Earlier I wrote (maybe over thinking the question):
不能将argument_group
添加到mutually_exclusive_group
.这两种组具有不同的目的和功能.之前有关于此的SO讨论(请参阅相关"),以及几个相关的Python错误问题.如果您希望测试超出简单的互斥组,则可能应该在parse_args
之后进行自己的测试.这可能还需要您自己的usage
行.
An argument_group
cannot be added to a mutually_exclusive_group
. The two kinds of groups have different purposes and functions. There are previous SO discussions of this (see 'related'), as well as a couple of relevant Python bug issues. If you want tests that go beyond a simple mutually exclusive group, you probably should do your own testing after parse_args
. That may also require your own usage
line.
argument_group
只是在帮助部分中对参数进行分组和标记的一种方法.
An argument_group
is just a means of grouping and labeling arguments in the help section.
A mutually_exclusive_group
会影响usage
格式(如果可以),并且还会在parse_args
期间运行测试.两者都使用组"意味着它们比实际更紧密地联系在一起.
A mutually_exclusive_group
affects the usage
formatting (if it can), and also runs tests during parse_args
. The use of 'group' for both implies that they are more connected than they really are.
http://bugs.python.org/issue11588 要求嵌套组以及也测试包容性".我试图证明组"还不够笼统,无法表达用户想要的所有测试类型.但是概括测试机制是一回事,而提出直观的API则是另一回事.这样的问题表明argparse
确实需要某种嵌套组"语法.
http://bugs.python.org/issue11588 asks for nested groups, and the ability to test for 'inclusivity' as well. I tried to make the case that 'groups' aren't general enough to express all the kinds of testing that users want. But it's one thing to generalize the testing mechanism, and quite another to come up with an intuitive API. Questions like this suggest that argparse
does need some sort of 'nested group' syntax.
这篇关于论据组之间的互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!