Skip to content

Commit 68c857f

Browse files
Jokimaxaisk
authored andcommitted
pythongh-101599: argparse: simplify the option help string (pythonGH-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
1 parent 55aeb8d commit 68c857f

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

Lib/argparse.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,22 +564,18 @@ def _format_action_invocation(self, action):
564564
return metavar
565565

566566
else:
567-
parts = []
568567

569568
# if the Optional doesn't take a value, format is:
570569
# -s, --long
571570
if action.nargs == 0:
572-
parts.extend(action.option_strings)
571+
return ', '.join(action.option_strings)
573572

574573
# if the Optional takes a value, format is:
575-
# -s ARGS, --long ARGS
574+
# -s, --long ARGS
576575
else:
577576
default = self._get_default_metavar_for_optional(action)
578577
args_string = self._format_args(action, default)
579-
for option_string in action.option_strings:
580-
parts.append('%s %s' % (option_string, args_string))
581-
582-
return ', '.join(parts)
578+
return ', '.join(action.option_strings) + ' ' + args_string
583579

584580
def _metavar_formatter(self, action, default_metavar):
585581
if action.metavar is not None:

Lib/test/test_argparse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,7 +3922,7 @@ class TestHelpUsageWithParentheses(HelpTestCase):
39223922
39233923
options:
39243924
-h, --help show this help message and exit
3925-
-p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)}
3925+
-p, --optional {1 (option A), 2 (option B)}
39263926
'''
39273927
version = ''
39283928

@@ -4405,8 +4405,8 @@ class TestHelpAlternatePrefixChars(HelpTestCase):
44054405
help = usage + '''\
44064406
44074407
options:
4408-
^^foo foo help
4409-
;b BAR, ;;bar BAR bar help
4408+
^^foo foo help
4409+
;b, ;;bar BAR bar help
44104410
'''
44114411
version = ''
44124412

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Changed argparse flag options formatting to remove redundancy.

0 commit comments

Comments
 (0)