[New-bugs-announce] [issue42547] argparse: add_argument(... nargs='+', metavar=<tuple>) does not work with positional arguments

Mikhail Khvoinitsky report at bugs.python.org
Wed Dec 2 16:15:50 EST 2020


New submission from Mikhail Khvoinitsky <bugs.python.org at khvoinitsky.org>:

Example which works:

    parser.add_argument('--test', nargs='+', metavar=('TEST', 'TEST2'))



Example which doesn't work:

    parser.add_argument('test', nargs='+', metavar=('TEST', 'TEST2'))

it raises:

    Traceback (most recent call last):
      File <omitted>
        args = parser.parse_args()
      File "/usr/lib/python3.8/argparse.py", line 1768, in parse_args
        args, argv = self.parse_known_args(args, namespace)
      File "/usr/lib/python3.8/argparse.py", line 1800, in parse_known_args
        namespace, args = self._parse_known_args(args, namespace)
      File "/usr/lib/python3.8/argparse.py", line 2006, in _parse_known_args
        start_index = consume_optional(start_index)
      File "/usr/lib/python3.8/argparse.py", line 1946, in consume_optional
        take_action(action, args, option_string)
      File "/usr/lib/python3.8/argparse.py", line 1874, in take_action
        action(self, namespace, argument_values, option_string)
      File "/usr/lib/python3.8/argparse.py", line 1044, in __call__
        parser.print_help()
      File "/usr/lib/python3.8/argparse.py", line 2494, in print_help
        self._print_message(self.format_help(), file)
      File "/usr/lib/python3.8/argparse.py", line 2471, in format_help
        formatter.add_arguments(action_group._group_actions)
      File "/usr/lib/python3.8/argparse.py", line 276, in add_arguments
        self.add_argument(action)
      File "/usr/lib/python3.8/argparse.py", line 261, in add_argument
        invocations = [get_invocation(action)]
      File "/usr/lib/python3.8/argparse.py", line 549, in _format_action_invocation
        metavar, = self._metavar_formatter(action, default)(1)
    ValueError: too many values to unpack (expected 1)


Expected result: help message should look like this:

    usage: test_argparse [-h] TEST [TEST2 ...]

    positional arguments:
      TEST

    optional arguments:
      -h, --help  show this help message and exit

----------
components: Library (Lib)
messages: 382341
nosy: m_khvoinitsky
priority: normal
severity: normal
status: open
title: argparse: add_argument(... nargs='+', metavar=<tuple>) does not work with positional arguments
type: crash
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42547>
_______________________________________


More information about the New-bugs-announce mailing list