2013-08-02 29 views
13

如何设置argparse如下:Python的argparse条件要求

if -2 is on the command line, no other arguments are required 
if -2 is not on the command line, -3 and -4 arguments are required 

例如,

-2 [good] 
-3 a -4 b [good] 
-3 a [not good, -4 required] 
-2 -5 c [good] 
-2 -3 a [good] 

有许多的类似这里的问题,但他们要么没有解决这个问题情况还是我不明白。

Python 2.7如果有问题。

+0

使键安装在-2是复制其他命令可选一个子分析器。在顶层,将-3和-4链接在一起。 – Jiminion

+0

使用以“-'开头的子分析器命令可能会非常棘手。 '-2'可能有效,但'-t'或'--two'不会因为它们看起来像可选项。但是,如果'-3'被定义为一个参数,那么'-2'不再作为子分析器命令(或选择)。 – hpaulj

回答

13

子分析器(如评论中所建议的)可能有效。

另一种选择(因为mutually_exclusive_group不能完全做到这一点),是把它手工编码,因为它是:

import argparse 

def main(): 
    parser = argparse.ArgumentParser() 

    parser.add_argument('-2', dest='two', action='store_true') 
    parser.add_argument('-3', dest='three') 
    parser.add_argument('-4', dest='four') 
    parser.add_argument('-5', dest='five') 

    args = parser.parse_args() 

    if not args.two: 
     if args.three is None or args.four is None: 
      parser.error('without -2, *both* -3 <a> *and* -4 <b> are required') 

    print args 
    return 0 

加入少许司机这样的:

import sys 
sys.exit(main()) 

和运行用你的例子,它似乎做对了;这里有两个运行:

$ python mxgroup.py -2; echo $? 
Namespace(five=None, four=None, three=None, two=True) 
0 
$ python mxgroup.py -3 a; echo $? 
usage: mxgroup.py [-h] [-2] [-3 THREE] [-4 FOUR] [-5 FIVE] 
mxgroup.py: error: without -2, *both* -3 <a> *and* -4 <b> are required 
2 
$ 
+0

在没有更好的替代方案的情况下,手动做似乎最好的课程 – foosion

+0

在http://stackoverflow.com/questions/17917265/python-argparser-for-multiple-arguments-for-partial-choices/17982009#17982009我建议两步解析。首先使用'parse_known_args'来检查'-2'标志,如果缺少,使用另一个分析器来查找'-3'和'-4'。 – hpaulj

+0

我不认为有什么方法可以影响--help会打印,而不是为每个参数设置帮助... – Leonid

1

我认为这是很难实现,包括一个很好的帮助消息,而只使用标准argparse函数。但是,您可以在解析参数后自行轻松地进行测试。你可以在结尾描述额外的需求。请注意,使用数字作为选项是不寻常的,我必须使用dest ='two',因为args.2不是有效的语法。

#!/usr/bin/env python 

import argparse 

parser = argparse.ArgumentParser(
    description='bla bla', 
    epilog='Note: arguments -3 and -4 are required when -2 is missing') 

parser.add_argument('-2', dest='two', action='store_true') 
parser.add_argument('-3', dest='three') 
parser.add_argument('-4', dest='four') 
parser.add_argument('-5', dest='five') 

args = parser.parse_args() 

if not args.two and (args.three is None or args.four is None): 
    parser.error('arguments -3 and -4 are required when -2 is missing') 

print 'Good:', args 

有了这些结果:

[~]: ./test.py -h 
usage: test.py [-h] [-2] [-3 THREE] [-4 FOUR] [-5 FIVE] 

bla bla 

optional arguments: 
    -h, --help show this help message and exit 
    -2 
    -3 THREE 
    -4 FOUR 
    -5 FIVE 

Note: arguments -3 and -4 are required when -2 is missing 

[~]: ./test.py -2 
Good: Namespace(five=None, four=None, three=None, two=True) 
[~]: ./test.py -3 a -4 b 
Good: Namespace(five=None, four='b', three='a', two=False) 
[~]: ./test.py -3 a 
usage: test.py [-h] [-2] [-3 THREE] [-4 FOUR] [-5 FIVE] 
test.py: error: arguments -3 and -4 are required when -2 is missing 
[~]: ./test.py -2 -5 c 
Good: Namespace(five='c', four=None, three=None, two=True) 
[~]: ./test.py -2 -3 a 
Good: Namespace(five=None, four=None, three='a', two=True) 
+0

虽然'args.2'不起作用,'getattr(args,'2')'做。像'-2'这样的参数是允许的,但会增加不必要的复杂性。 – hpaulj