python / cpython

The Python programming language
https://www.python.org
Other
63.35k stars 30.34k forks source link

argparse: add_argument(... nargs='+', metavar=<tuple>) does not work with positional arguments #86713

Closed 321b01e9-392b-44d4-bb60-82ff20cce040 closed 1 month ago

321b01e9-392b-44d4-bb60-82ff20cce040 commented 3 years ago
BPO 42547
Nosy @rhettinger, @iritkatriel, @m-khvoinitsky
PRs
  • python/cpython#23617
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'library', '3.10'] title = "argparse: add_argument(... nargs='+', metavar=) does not work with positional arguments" updated_at = user = 'https://github.com/m-khvoinitsky' ``` bugs.python.org fields: ```python activity = actor = 'iritkatriel' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'm_khvoinitsky' dependencies = [] files = [] hgrepos = [] issue_num = 42547 keywords = ['patch'] message_count = 4.0 messages = ['382341', '382368', '382369', '401340'] nosy_count = 4.0 nosy_names = ['rhettinger', 'paul.j3', 'iritkatriel', 'm_khvoinitsky'] pr_nums = ['23617'] priority = 'normal' resolution = None stage = 'resolved' status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue42547' versions = ['Python 3.10'] ```

    321b01e9-392b-44d4-bb60-82ff20cce040 commented 3 years ago

    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
    7a064fe6-c535-4d80-a11f-a04ed39056c5 commented 3 years ago

    Duplicate of

    https://bugs.python.org/issue14074

    argparse allows nargs>1 for positional arguments but doesn't allow metavar to be a tuple

    7a064fe6-c535-4d80-a11f-a04ed39056c5 commented 3 years ago

    I'll reopen it - your patch, while not a complete resolution, does take care of the immediate error.

    iritkatriel commented 3 years ago

    Changing type since crash typically means segfault and not an exception.

    serhiy-storchaka commented 1 month ago

    124782 is more complete solution of this issue.