argparse是否支持多个互斥参数? [英] Does argparse support multiple exclusive arguments?

查看:78
本文介绍了argparse是否支持多个互斥参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有两组论点.您可以在每个组中使用任意数量的参数,但是不能在组之间混合参数.

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')

即. anyallandor的某种组合.但是count是处理xor条件的好方法.

ie. some combination of any,all,and,or. But count is a nice way of handling the xor condition.

这篇关于argparse是否支持多个互斥参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆