Python 中的一个参数如何使用 argparse 设置可变数量的参数?

How have a variable number of parameters for one argument in Python with argparse?

我正在使用 argparse 向我的脚本用户建议一个映射选项,如下所示:

parser.add_argument('-m', '--mapping_strategy', 
                    help='mapping strategy', 
                    choices=['ZigZag', 
                    'RoundRobin'])

所以我可以这样使用脚本:

> script.py -m ZigZag

我现在需要提供一种新的映射策略,用户可以在其中指定描述映射的自定义文件。因此我现在需要这样的东西:

> script.py -m Custom /home/manu/custom.map

如何使用 argparse 实现此目的?

将您的下方行更改为:

parser.add_argument('-m', '--mapping_strategy', 
                     help='mapping strategy', nargs="+")

这会将所有位置参数收集到一个列表中。如果没有至少一个可以操作,它也会产生错误。

查看 nargs documentation

您当然可以使用简单的 nargs="+",但这有一些缺点,--help 输出的信息量要少得多。您必须自己验证 mapping_strategy[0]['ZigZag', 'RoundRobin', 'Custom'] 中。

现在,另一种方法是要求地图路径有一个 -p 参数,并要求在 mapping_strategy == 'Custom' 时设置它。如果他们提供了错误的 mapping_strategy.

,您还可以打印出 "ignoring -p argument, only required for Custom mapping strategy" 警告消息
import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-m', "--mapping_strategy",
                    help='valid strategies: ZigZag, RoundRobin, Custom', 
                    choices=['ZigZag', 
                    'RoundRobin',
                    'Custom']
                    )

parser.add_argument('-p', "--path",
                    help='path to custom map file, required '
                         'if using Custom mapping_strategy', 
                    )

args = parser.parse_args()
if args.mapping_strategy == 'Custom' and args.path == None:
    parser.error('Custom mapping strategy requires a path')
if args.mapping_strategy != 'Custom' and args.path != None:
    print('Ignoring path parameter, only used for Custom mapping_strategy')
print args

您也可以使用自定义 class 来验证您的参数。此代码提供更好的帮助消息,以及更好的警告和错误检查。虽然它有点脆弱,因为我已经复制了 valid_strategies 的列表。不过这很容易克服。

import argparse

class ValidateMapping(argparse.Action):
    def __call__(self, parser, args, values, option_string=None):
        valid_strategies = ('ZigZag', 'RoundRobin', 'Custom')
        strategy = values[0]
        if strategy not in valid_strategies:
            parser.error('Invalid mapping strategy {}'.format(strategy))
        if strategy == 'Custom':
            if len(values) == 1:
                parser.error('Custom mapping strategy requires a path')
            elif len(values) == 2:
                path = values[1]
            elif len(values) > 2:
                path = '"' + ' '.join(values[1:]) + '"'
            setattr(args, self.dest, [strategy, path])
        else:
            if len(values) > 1:
                print "path to map only used by Custom mapping strategy"
                print "ignoring: ",
                for i in range(1, len(values)):
                    print values[i],
                print
            setattr(args, self.dest, strategy)


parser = argparse.ArgumentParser()

parser.add_argument('-m', "--mapping_strategy",
                    help='valid strategies: ZigZag, RoundRobin, Custom', 
                    nargs="+",
                    action=ValidateMapping,
                    metavar=('mapping strategy', 'path to map')
                    )


args = parser.parse_args()
print args

这是帮助输出:

$python mapping_strategy.py -h
usage: mapping_strategy.py [-h] [-m mapping strategy [path to map ...]]

optional arguments:
  -h, --help            show this help message and exit
  -m mapping strategy [path to map ...], --mapping_strategy mapping strategy [path to map ...]
                        valid strategies: ZigZag, RoundRobin, Custom

如果您只提供 -m 会发生以下情况:

$ python mapping_strategy.py -m 
usage: mapping_strategy.py [-h] [-m mapping strategy [path to map ...]]
mapping_strategy.py: error: argument -m/--mapping_strategy: expected at least one argument

如果您键入 -m Custom 但不提供路径,您会看到以下内容:

$ python mapping_strategy.py -m Custom
usage: mapping_strategy.py [-h] [-m mapping strategy [path to map ...]]
mapping_strategy.py: error: Custom mapping strategy requires a path

如果您给出 -m ZigZag 并附加一条无意义的路径,会发生以下情况:

$ python mapping_strategy.py -m ZigZag blah blah
path to map only used by Custom mapping strategy
ignoring:  blah blah

如果您提供包含空格的路径的自定义选项,您会得到以下结果:

$ python mapping_strategy.py -m Custom c:\My Documents
Namespace(mapping_strategy=['Custom', '"c:My Documents"'])

但是谁会使用Windows,或者目录名中有空格呢?异教徒。

如果您指定了无效的映射策略,您会得到以下结果:

$ python mapping_strategy.py -m Foo
usage: mapping_strategy.py [-h] [-m mapping strategy [path to map ...]]
mapping_strategy.py: error: Invalid mapping strategy Foo