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
现在,您最好的选择是使用子解析器(如果合适)实现您的问题,或者在解析后进行自己的测试.并编写自己的用法
.
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
找到一组,等等.我提到的hook
在 bug issue 中进行相同类型的测试,只是使用不同的 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
,正常的互斥
测试会反对.一个必需的组会反对 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屋!