argparse(python)是否支持互斥的参数组? [英] Does argparse (python) support mutually exclusive groups of arguments?

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

问题描述

如果我有参数'-a', '-b', '-c', '-d',则通过add_mutually_exclusive_group()函数,我的程序将仅使用其中之一.有没有一种方法可以组合起来,以便程序仅接受'-a 999 -b 999''-c 999 -d 999'?

If I have the arguments '-a', '-b', '-c', '-d', with the add_mutually_exclusive_group() function my program will have to use just one of them. Is there a way to combine that, so that the program will accept only either '-a 999 -b 999' or '-c 999 -d 999'?

,添加了一个简单的程序以使其更加清晰:

adding a simple program for more clarity:

>>> parser = argparse.ArgumentParser()
>>> group = parser.add_mutually_exclusive_group()
>>> group.add_argument('-a')
>>> group.add_argument('-b')
>>> group.add_argument('-c')
>>> group.add_argument('-d')

然后只能调用./app.py -a | ./app.py -b | ./app.py -c | ./app.py -d.可以将argparse组排除在外,以便仅调用./app.py -a .. -b .. | ./app.py -c .. -d ..吗?

Then only ./app.py -a | ./app.py -b | ./app.py -c | ./app.py -d can be called. Is it possible to have argparse group the exclusion groups, so that only ./app.py -a .. -b .. | ./app.py -c .. -d .. be called?

推荐答案

EDIT :没关系.因为argparse使得在调用group.add_argument时必须创建一个选项的选择非常糟糕.那不是我的设计选择.如果您急于使用此功能,可以尝试使用 ConflictsOptionParser :

EDIT: Never mind. Because argparse makes the horrible choice of having to create an option when invoking group.add_argument. That wouldn't be my design choice. If you're desperate for this feature, you can try doing it with ConflictsOptionParser:

# exclusivegroups.py
import conflictsparse

parser = conflictsparse.ConflictsOptionParser()
a_opt = parser.add_option('-a')
b_opt = parser.add_option('-b')
c_opt = parser.add_option('-c')
d_opt = parser.add_option('-d')

import itertools
compatible_opts1 = (a_opt, b_opt)
compatible_opts2 = (c_opt, d_opt)
exclusives = itertools.product(compatible_opts1, compatible_opts2)
for exclusive_grp in exclusives:
    parser.register_conflict(exclusive_grp)


opts, args = parser.parse_args()
print "opts: ", opts
print "args: ", args

因此,当我们调用它时,我们可以看到达到了预期的效果.

Thus when we invoke it, we can see we get the desired effect.

$ python exclusivegroups.py -a 1 -b 2
opts:  {'a': '1', 'c': None, 'b': '2', 'd': None}
args:  []
$ python exclusivegroups.py -c 3 -d 2
opts:  {'a': None, 'c': '3', 'b': None, 'd': '2'}
args:  []
$ python exclusivegroups.py -a 1 -b 2 -c 3
Usage: exclusivegroups.py [options]

exclusivegroups.py: error: -b, -c are incompatible options.

警告消息不会通知您'-a''-b''-c'不兼容,但是可以制作更适当的错误消息.下面是较旧的错误答案.

The warning message doesn't inform you that both '-a' and '-b' are incompatible with '-c', however a more appropriate error message could be crafted. Older, wrong answer below.

[此编辑是错误的,但是如果argparse以这种方式工作,那岂不是一个完美的世界吗?] 我以前的回答实际上是错误的方法,您应该可以通过argparse通过在每个互斥选项中指定一组来做到这一点.我们甚至可以使用itertools来概括该过程.并做到这一点,这样我们就不必显式地键入所有组合:

OLDER [This edit is wrong, although wouldn't it be just a perfect world if argparse worked this way?] My previous answer actually was incorrect, you should be able to do this with argparse by specifying one group per mutually exclusive options. We can even use itertools to generalize the process. And make it so we don't have to type out all the combinations explicitly:

import itertools
compatible_opts1 = ('-a', '-b')
compatible_opts2 = ('-c', '-d')
exclusives = itertools.product(compatible_opts1, compatible_opts2)
for exclusive_grp in exclusives:
    group = parser.add_mutually_exclusive_group()
    group.add_argument(exclusive_grp[0])
    group.add_argument(exclusive_grp[1])

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

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