Skip to content

bpo-14074: argparse doesn't allow metavar to be a tuple #10847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ def _format_action(self, action):
def _format_action_invocation(self, action):
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
if isinstance(action.nargs, int):
return ' '.join(self._metavar_formatter(action, default)(1))
metavar, = self._metavar_formatter(action, default)(1)
return metavar
Comment on lines +531 to 534
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(action.nargs, int):
return ' '.join(self._metavar_formatter(action, default)(1))
metavar, = self._metavar_formatter(action, default)(1)
return metavar
return ' '.join(self._metavar_formatter(action, default)(1))


Expand Down Expand Up @@ -704,6 +706,9 @@ def _get_action_name(argument):
elif argument.option_strings:
return '/'.join(argument.option_strings)
elif argument.metavar not in (None, SUPPRESS):
if isinstance(
argument.nargs, int) and isinstance(argument.metavar, tuple):
return ' '.join(argument.metavar)
Comment on lines +709 to +711
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not covered by tests.

return argument.metavar
elif argument.dest not in (None, SUPPRESS):
return argument.dest
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4665,6 +4665,28 @@ class TestHelpTupleMetavar(HelpTestCase):
version = ''


class TestHelpTupleMetavarPositional(HelpTestCase):
"""Test specifying metavar on a Positional as a tuple"""

parser_signature = Sig(prog='PROG')
argument_signatures = [
Sig('foo', help='foo help', nargs=2, metavar=('bar', 'baz'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Sig('foo', help='foo help', nargs=2, metavar=('bar', 'baz'))
Sig('w', help='w help', nargs='+', metavar=('W1', 'W2')),
Sig('x', help='x help', nargs='*', metavar=('X1', 'X2')),
Sig('y', help='y help', nargs=3, metavar=('Y1', 'Y2', 'Y3')),
Sig('z', help='z help', nargs='?', metavar=('Z1', )),

]
argument_group_signatures = []
usage = '''\
usage: PROG [-h] bar baz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
usage: PROG [-h] bar baz
usage: PROG [-h] W1 [W2 ...] [X1 [X2 ...]] Y1 Y2 Y3 [Z1]

'''
help = usage + '''\

positional arguments:
bar baz foo help
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bar baz foo help
W1 W2 w help
X1 X2 x help
Y1 Y2 Y3 y help
Z1 z help


options:
-h, --help show this help message and exit
'''
version = ''


class TestHelpRawText(HelpTestCase):
"""Test the RawTextHelpFormatter"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`argparse` metavar processing to allow positional arguments to have a
tuple metavar.
Loading