]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101599: argparse: simplify the option help string (GH-103372)
authorJokimax <77680901+Jokimax@users.noreply.github.com>
Fri, 2 Feb 2024 22:13:00 +0000 (00:13 +0200)
committerGitHub <noreply@github.com>
Fri, 2 Feb 2024 22:13:00 +0000 (22:13 +0000)
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

Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2023-04-08-11-41-07.gh-issue-101599.PaWNFh.rst [new file with mode: 0644]

index a32884db80d1ea1f5818cc696b63a184187ff017..9e19f39fadd87bcd9499fae965bb78b58045bcd9 100644 (file)
@@ -564,22 +564,18 @@ class HelpFormatter(object):
             return metavar
 
         else:
-            parts = []
 
             # if the Optional doesn't take a value, format is:
             #    -s, --long
             if action.nargs == 0:
-                parts.extend(action.option_strings)
+                return ', '.join(action.option_strings)
 
             # if the Optional takes a value, format is:
-            #    -s ARGS, --long ARGS
+            #    -s, --long ARGS
             else:
                 default = self._get_default_metavar_for_optional(action)
                 args_string = self._format_args(action, default)
-                for option_string in action.option_strings:
-                    parts.append('%s %s' % (option_string, args_string))
-
-            return ', '.join(parts)
+                return ', '.join(action.option_strings) + ' ' + args_string
 
     def _metavar_formatter(self, action, default_metavar):
         if action.metavar is not None:
index 7c1f5d36999a3d5577d5cb8a997cc6c2889ea84a..940d7e95f96e20a865d64e8a915f6cd0b001ee47 100644 (file)
@@ -3922,7 +3922,7 @@ class TestHelpUsageWithParentheses(HelpTestCase):
 
         options:
           -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 = ''
 
@@ -4405,8 +4405,8 @@ class TestHelpAlternatePrefixChars(HelpTestCase):
     help = usage + '''\
 
         options:
-          ^^foo              foo help
-          ;b BAR, ;;bar BAR  bar help
+          ^^foo          foo help
+          ;b, ;;bar BAR  bar help
         '''
     version = ''
 
diff --git a/Misc/NEWS.d/next/Library/2023-04-08-11-41-07.gh-issue-101599.PaWNFh.rst b/Misc/NEWS.d/next/Library/2023-04-08-11-41-07.gh-issue-101599.PaWNFh.rst
new file mode 100644 (file)
index 0000000..a1608a1
--- /dev/null
@@ -0,0 +1 @@
+Changed argparse flag options formatting to remove redundancy.