This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse: add_argument(... nargs='+', metavar=) does not work with positional arguments
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, m_khvoinitsky, paul.j3, rhettinger
Priority: normal Keywords: patch

Created on 2020-12-02 21:15 by m_khvoinitsky, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 23617 open m_khvoinitsky, 2020-12-02 21:21
Messages (4)
msg382341 - (view) Author: Mikhail Khvoinitsky (m_khvoinitsky) * Date: 2020-12-02 21:15
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
msg382368 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-12-03 02:57
Duplicate of

https://bugs.python.org/issue14074

argparse allows nargs>1 for positional arguments but doesn't allow metavar to be a tuple
msg382369 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-12-03 03:00
I'll reopen it - your patch, while not a complete resolution, does take care of the immediate error.
msg401340 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-07 19:41
Changing type since crash typically means segfault and not an exception.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86713
2021-09-07 19:41:21iritkatrielsettype: crash -> behavior

messages: + msg401340
nosy: + iritkatriel
2020-12-03 03:00:28paul.j3setstatus: closed -> open
resolution: duplicate ->
messages: + msg382369
2020-12-03 02:57:52paul.j3setstatus: open -> closed
resolution: duplicate
messages: + msg382368

stage: patch review -> resolved
2020-12-03 02:48:49xtreaksetnosy: + rhettinger, paul.j3
2020-12-02 21:21:42m_khvoinitskysetkeywords: + patch
stage: patch review
pull_requests: + pull_request22485
2020-12-02 21:15:50m_khvoinitskycreate