mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
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:
parent
9ce90206b7
commit
9b31a2d83f
3 changed files with 109 additions and 4 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue