【问题标题】:Python argparse conditional requirementsPython argparse 条件要求
【发布时间】:2013-08-04 05:46:42
【问题描述】:

如何设置 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 如果这很重要。

【问题讨论】:

  • 制作一个以-2为键的子解析器,将其他命令复制为可选。在顶层,将 -3 和 -4 链接在一起。
  • 使用以- 开头的子解析器命令可能会很棘手。 -2 可能有效,但 -t--two 不会,因为它们看起来像可选项。但是如果-3 被定义为一个参数,那么-2 就不再作为一个子解析器命令(或一个选项)。

标签: python python-2.7 argparse


【解决方案1】:

子解析器(如 cmets 中所建议的)可能会起作用。

另一种选择(因为 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
$ 

【讨论】:

  • 在没有更好的选择的情况下,手动操作似乎是最好的方法
  • stackoverflow.com/questions/17917265/… 我建议两步解析。首先使用parse_known_args 来检查-2 标志,如果缺少,使用另一个解析器查找-3-4
  • 我认为除了为每个参数设置帮助之外,没有其他方法可以影响 --help 将打印的内容...
  • @Leonid:似乎不是。对于类似的事情(从来没有这样),我刚刚在帮助结尾添加了解释。
【解决方案2】:

我认为仅使用标准的 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)

【讨论】:

  • args.2 不起作用,getattr(args,'2') 起作用。像-2 这样的参数是允许的,但会增加不必要的复杂性。
猜你喜欢
  • 2014-10-26
  • 2020-11-05
  • 1970-01-01
  • 2013-09-13
  • 2015-09-23
  • 2021-09-09
  • 2015-02-10
  • 2019-02-04
  • 2015-09-13
相关资源
最近更新 更多