gh-101599: argparse: simplify the option help string (GH-103372)

If the option with argument has short and long names,
output argument only once, after the long name:

   -o, --option ARG    description

instead of

   -o ARG, --option ARG    description
This commit is contained in:
Jokimax 2024-02-03 00:13:00 +02:00 committed by GitHub
parent 73d20cafb5
commit c4a2e8a2c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 10 deletions

View file

@ -564,22 +564,18 @@ class HelpFormatter(object):
return metavar return metavar
else: else:
parts = []
# if the Optional doesn't take a value, format is: # if the Optional doesn't take a value, format is:
# -s, --long # -s, --long
if action.nargs == 0: if action.nargs == 0:
parts.extend(action.option_strings) return ', '.join(action.option_strings)
# if the Optional takes a value, format is: # if the Optional takes a value, format is:
# -s ARGS, --long ARGS # -s, --long ARGS
else: else:
default = self._get_default_metavar_for_optional(action) default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default) args_string = self._format_args(action, default)
for option_string in action.option_strings: return ', '.join(action.option_strings) + ' ' + args_string
parts.append('%s %s' % (option_string, args_string))
return ', '.join(parts)
def _metavar_formatter(self, action, default_metavar): def _metavar_formatter(self, action, default_metavar):
if action.metavar is not None: if action.metavar is not None:

View file

@ -3922,7 +3922,7 @@ class TestHelpUsageWithParentheses(HelpTestCase):
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
-p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)} -p, --optional {1 (option A), 2 (option B)}
''' '''
version = '' version = ''
@ -4406,7 +4406,7 @@ class TestHelpAlternatePrefixChars(HelpTestCase):
options: options:
^^foo foo help ^^foo foo help
;b BAR, ;;bar BAR bar help ;b, ;;bar BAR bar help
''' '''
version = '' version = ''

View file

@ -0,0 +1 @@
Changed argparse flag options formatting to remove redundancy.