python ArgParse 自定义操作和使用元变量的键值选项导致 -h 输出重复

python ArgParse custom action with key value options using metavar causes duplicates in -h output

我正在为参数解析器使用自定义操作以允许特定参数的键=值选项。

test.py --arg2 input1=something input2=something_else

自定义操作工作正常,但是当我使用 metavar 列出所有自定义选项时,我得到了重复项。

这是我的自定义操作:

class KeyValue(argparse.Action):
def __call__(self, parser, namespace,
             values, option_string=None):
    setattr(namespace, self.dest, dict())

    for value in values:
        # split it into key and value
        key, value = value.split('=')
        # assign into dictionary
        getattr(namespace, self.dest)[key] = value

这是使用率:

    sub_1.add_argument(
        "--arg2",
        action=KeyValue,
        nargs="*",
        default=NoArgumentProvided(),
        help='arg 2 help',
        metavar="key1=val1 key2=val2"
    )

帮助看起来像什么,出现重复项:

usage: arg_issue.py test [-h] [--arg1]
                     [--arg2 [key1=val1 key2=val2 [key1=val1 key2=val2 ...]]]

test description

optional arguments:
  -h, --help            show this help message and exit
  --arg1                arg1 help
  --arg2 [key1=val1 key2=val2 [key1=val1 key2=val2 ...]]
                        arg 2 help

知道为什么我会收到重复项吗?很明显是自定义的Action,不知道为什么?

完整代码:

import argparse


class NoArgumentProvided(object):
    pass


class KeyValue(argparse.Action):
    def __call__(self, parser, namespace,
                 values, option_string=None):
        setattr(namespace, self.dest, dict())

        for value in values:
            # split it into key and value
            key, value = value.split('=')
            # assign into dictionary
            getattr(namespace, self.dest)[key] = value


def main():
    parser = argparse.ArgumentParser(
        description='parser',
        formatter_class=argparse.RawTextHelpFormatter,
    )

    # top-level args.
    parser.add_argument('--verbose',
                        help='Verbose mode',
                        action='store_true',
                        required=False,
                        default=NoArgumentProvided())
    # Add the main sub parsers
    subparsers = parser.add_subparsers(dest='action')

    sub_1 = subparsers.add_parser(
        'test',
        help='test help',
        description='test description')

    sub_1.add_argument(
        "--arg1",
        action='store_true',
        required=False,
        default=NoArgumentProvided(),
        help='arg1 help'
    )

    sub_1.add_argument(
        "--arg2",
        action=KeyValue,
        nargs="*",
        default=NoArgumentProvided(),
        help='arg 2 help',
        metavar="key1=val1 key2=val2"
    )

    subparsers.required = True

    args = parser.parse_args()


if __name__=="__main__":
    main()

如果元变量是字符串元组

p.add_argument('--foo', nargs='*', metavar=('one','two'))

帮助将是

usage: ipython3 [-h] [--foo [one [two ...]]]

optional arguments:
  -h, --help            show this help message and exit
  --foo [one [two ...]]

使用nargs='*',使用2个部分,[one [two ...]]。使用自定义操作 class 不会更改该显示。

保持 metavar 简单,并根据需要在 help 中详细说明。 description 还可以添加详细信息。

编辑

几年前的一个补丁简化了 * 帮助。

https://bugs.python.org/issue38438 argparse“用法”与 nargs="*"

过于复杂

与Python3.10

In [218]: import argparse310 as argparse
In [219]: p=argparse.ArgumentParser()
In [220]: p.add_argument('--foo',nargs='*');
In [221]: p.print_help()
usage: ipython3 [-h] [--foo [FOO ...]]

optional arguments:
  -h, --help       show this help message and exit
  --foo [FOO ...]

元组版本仍然可用:

In [226]: p._actions[1].metavar=('one','two')
In [227]: p.print_help()
usage: ipython3 [-h] [--foo [one [two ...]]]

optional arguments:
  -h, --help            show this help message and exit
  --foo [one [two ...]]