gh-58282: Fix support of tuple metavar for positional arguments in argparse (GH-124782)

Previously, formatting help output or error message for positional argument
with a tuple metavar raised exception.

Co-authored-by: Cyker Way <cykerway@gmail.com>
This commit is contained in:
Serhiy Storchaka 2024-10-02 11:28:02 +03:00 committed by GitHub
parent 9ce90206b7
commit 9b31a2d83f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 109 additions and 4 deletions

View file

@ -527,8 +527,7 @@ class HelpFormatter(object):
def _format_action_invocation(self, action):
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
metavar, = self._metavar_formatter(action, default)(1)
return metavar
return ' '.join(self._metavar_formatter(action, default)(1))
else:
@ -703,7 +702,15 @@ def _get_action_name(argument):
elif argument.option_strings:
return '/'.join(argument.option_strings)
elif argument.metavar not in (None, SUPPRESS):
return argument.metavar
metavar = argument.metavar
if not isinstance(metavar, tuple):
return metavar
if argument.nargs == ZERO_OR_MORE and len(metavar) == 2:
return '%s[, %s]' % metavar
elif argument.nargs == ONE_OR_MORE:
return '%s[, %s]' % metavar
else:
return ', '.join(metavar)
elif argument.dest not in (None, SUPPRESS):
return argument.dest
elif argument.choices: