[Python-checkins] bpo-29553: Fix ArgumentParser.format_usage() for mutually exclusive groups (GH-14976)

Berker Peksag webhook-mailer at python.org
Sun Aug 25 17:30:58 EDT 2019


https://github.com/python/cpython/commit/31ea447ffe591736af1d7a3178c0f7ca3eb50d70
commit: 31ea447ffe591736af1d7a3178c0f7ca3eb50d70
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2019-08-26T00:30:49+03:00
summary:

bpo-29553: Fix ArgumentParser.format_usage() for mutually exclusive groups (GH-14976)

Co-authored-by: Andrew Nester <andrew.nester.dev at gmail.com>
Co-authored-by: Flavian Hautbois <flavianh at sicara.com>

(cherry picked from commit da27d9b9dc44913ffee8f28d9638985eaaa03755)

files:
A Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index a03074924762..24af355b7441 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -407,13 +407,19 @@ def _format_actions_usage(self, actions, groups):
                             inserts[start] += ' ['
                         else:
                             inserts[start] = '['
-                        inserts[end] = ']'
+                        if end in inserts:
+                            inserts[end] += ']'
+                        else:
+                            inserts[end] = ']'
                     else:
                         if start in inserts:
                             inserts[start] += ' ('
                         else:
                             inserts[start] = '('
-                        inserts[end] = ')'
+                        if end in inserts:
+                            inserts[end] += ')'
+                        else:
+                            inserts[end] = ')'
                     for i in range(start + 1, end):
                         inserts[i] = '|'
 
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 51f0effaf2ff..3cdaff61c7cc 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2772,6 +2772,46 @@ def get_parser(self, required):
           -c          c help
         '''
 
+class TestMutuallyExclusiveNested(MEMixin, TestCase):
+
+    def get_parser(self, required):
+        parser = ErrorRaisingArgumentParser(prog='PROG')
+        group = parser.add_mutually_exclusive_group(required=required)
+        group.add_argument('-a')
+        group.add_argument('-b')
+        group2 = group.add_mutually_exclusive_group(required=required)
+        group2.add_argument('-c')
+        group2.add_argument('-d')
+        group3 = group2.add_mutually_exclusive_group(required=required)
+        group3.add_argument('-e')
+        group3.add_argument('-f')
+        return parser
+
+    usage_when_not_required = '''\
+        usage: PROG [-h] [-a A | -b B | [-c C | -d D | [-e E | -f F]]]
+        '''
+    usage_when_required = '''\
+        usage: PROG [-h] (-a A | -b B | (-c C | -d D | (-e E | -f F)))
+        '''
+
+    help = '''\
+
+        optional arguments:
+          -h, --help  show this help message and exit
+          -a A
+          -b B
+          -c C
+          -d D
+          -e E
+          -f F
+        '''
+
+    # We are only interested in testing the behavior of format_usage().
+    test_failures_when_not_required = None
+    test_failures_when_required = None
+    test_successes_when_not_required = None
+    test_successes_when_required = None
+
 # =================================================
 # Mutually exclusive group in parent parser tests
 # =================================================
diff --git a/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst b/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst
new file mode 100644
index 000000000000..3472db7bf209
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-07-27-10-14-45.bpo-29553.TVeIDe.rst
@@ -0,0 +1,2 @@
+Fixed :meth:`argparse.ArgumentParser.format_usage` for mutually exclusive groups.
+Patch by Andrew Nester.



More information about the Python-checkins mailing list