https://github.com/python/cpython/commit/c4a2e8a2c5188c3288d57b80852e92c83f46f6f3
commit: c4a2e8a2c5188c3288d57b80852e92c83f46f6f3
branch: main
author: Jokimax <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-02-02T22:13:00Z
summary:

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

files:
A Misc/NEWS.d/next/Library/2023-04-08-11-41-07.gh-issue-101599.PaWNFh.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index a32884db80d1ea..9e19f39fadd87b 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -564,22 +564,18 @@ def _format_action_invocation(self, action):
             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:
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 7c1f5d36999a3d..940d7e95f96e20 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -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
index 00000000000000..a1608a1ae0d2fa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-04-08-11-41-07.gh-issue-101599.PaWNFh.rst
@@ -0,0 +1 @@
+Changed argparse flag options formatting to remove redundancy.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to