-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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')) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
] | ||||||||||||
argument_group_signatures = [] | ||||||||||||
usage = '''\ | ||||||||||||
usage: PROG [-h] bar baz | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
''' | ||||||||||||
help = usage + '''\ | ||||||||||||
|
||||||||||||
positional arguments: | ||||||||||||
bar baz foo help | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
options: | ||||||||||||
-h, --help show this help message and exit | ||||||||||||
''' | ||||||||||||
version = '' | ||||||||||||
|
||||||||||||
|
||||||||||||
class TestHelpRawText(HelpTestCase): | ||||||||||||
"""Test the RawTextHelpFormatter""" | ||||||||||||
|
||||||||||||
|
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.