argparse是否支持多个互斥参数? [英] Does argparse support multiple exclusive arguments?
问题描述
比方说,我有两组论点.您可以在每个组中使用任意数量的参数,但是不能在组之间混合参数.
Let's say I have two groups of arguments. You can use any number of arguments from each group but you cannot mix arguments between the groups.
是否可以在argparse
模块中自定义冲突的参数?我尝试使用方法add_mutually_exclusive_group
,但这不是我想要的.
Is there a way to customize conflicting arguments in argparse
module? I've tried using method add_mutually_exclusive_group
but it's not what I'm looking for.
推荐答案
我提出了一个(或多个)补丁,可以让您测试参数的一般逻辑组合. http://bugs.python.org/issue11588
.
I've proposed a patch (or rather patches) that would let you test for general logical combinations of arguments. http://bugs.python.org/issue11588
.
我的想法的核心是在parse_args
内添加一个钩子,该钩子使用户可以测试参数的所有逻辑组合.此时,它可以访问列表seen
参数.在parse_args
之外,您无法使用此列表(因此需要使用钩子).但是使用适当的defaults
,您可以编写使用args
命名空间的自己的测试.
The core of my ideas there is to add a hook just inside parse_args
that lets the user test for all logical combinations of arguments. At that it point it has access to a list seen
arguments. This list is not available to you outside parse_args
(hence the need for a hook). But with appropriate defaults
, you can write your own tests that use the args
Namespace.
实现通用argparse
版本的困难包括:
The difficulties with implementing a general argparse
version include:
a)实现某种嵌套组(在您的情况下,多个any
组嵌套在xor
组中)
a) implementing some sort of nesting groups (in your case several any
groups nesting within a xor
group)
b)在有意义的usage
行中显示这些组
b) displaying those groups in a meaningful usage
line
目前,最好的选择是要么使用子解析器解决问题(如果合适),要么在解析后进行自己的测试.并编写自己的usage
.
For now your best bet is either implement your problem with subparsers (if it fits), or do your own testing after parsing. And write your own usage
.
下面是可推广测试的草图,该测试可以在解析后应用于args
命名空间
Here's a sketch of a generalizable test that could be applied to the args
namespace after parsing
def present(a):
# test whether an argument is 'present' or not
# simple case, just check whether it is the default None or not
if a is not None:
return True
else:
return False
# sample namespace from parser
args = argparse.Namespace(x1='one',x2=None,y1=None,y2=3)
# a nested list defining the argument groups that need to be tested
groups=[[args.x1,args.x2],[args.y1,args.y2]]
# a test that applies 'any' test to the inner group
# and returns the number of groups that are 'present'
[any(present(a) for a in g) for g in groups].count(True)
如果count
为0,则找不到任何组,如果找到了1
个组,依此类推.我在bug问题中提到的hook
进行相同类型的测试,只是使用了不同的测试present
测试.
If the count
is 0, no groups are found, if 1
one group has been found, etc. The hook
that I mentioned in the bug issue does the same sort of testing, just using a different present
test.
如果计数>1
,则正常的mutually exclusive
测试将反对.必需的组将反对0
等.您还可以执行类似
The normal mutually exclusive
test would object if count >1
. A required group would object to 0
, etc. You could also do something like
if (present(args.x1) or present(args.x2)) and
(present(args.y1) or present(args.y2)):
parser.error('too many groups')
即. any
,all
,and
,or
的某种组合.但是count
是处理xor
条件的好方法.
ie. some combination of any
,all
,and
,or
. But count
is a nice way of handling the xor
condition.
这篇关于argparse是否支持多个互斥参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!