]> granicus.if.org Git - python/commitdiff
Fix for issue 9355 where with multiple mutually exclusive arguments, some brackets...
authorSteven Bethard <steven.bethard@gmail.com>
Mon, 1 Nov 2010 16:29:26 +0000 (16:29 +0000)
committerSteven Bethard <steven.bethard@gmail.com>
Mon, 1 Nov 2010 16:29:26 +0000 (16:29 +0000)
Lib/argparse.py
Lib/test/test_argparse.py

index 223790c49d21ad9daddcc623ded763c9db96e3fe..38dd8e6b9d338e14ebcd983bae2f0d95d25054e1 100644 (file)
@@ -392,10 +392,16 @@ class HelpFormatter(object):
                     for action in group._group_actions:
                         group_actions.add(action)
                     if not group.required:
-                        inserts[start] = '['
+                        if start in inserts:
+                            inserts[start] += ' ['
+                        else:
+                            inserts[start] = '['
                         inserts[end] = ']'
                     else:
-                        inserts[start] = '('
+                        if start in inserts:
+                            inserts[start] += ' ('
+                        else:
+                            inserts[start] = '('
                         inserts[end] = ')'
                     for i in range(start + 1, end):
                         inserts[i] = '|'
index 30302e9c4b19655dba3529fe7e701692e10b2f24..1503ccd5d197ecff81d1642eca54a141a29c2400 100644 (file)
@@ -2163,6 +2163,25 @@ class TestMutuallyExclusiveGroupErrors(TestCase):
         raises(ValueError, add_argument, 'bar', nargs=1)
         raises(ValueError, add_argument, 'bar', nargs=argparse.PARSER)
 
+    def test_help(self):
+        parser = ErrorRaisingArgumentParser(prog='PROG')
+        group1 = parser.add_mutually_exclusive_group()
+        group1.add_argument('--foo', action='store_true')
+        group1.add_argument('--bar', action='store_false')
+        group2 = parser.add_mutually_exclusive_group()
+        group2.add_argument('--soup', action='store_true')
+        group2.add_argument('--nuts', action='store_false')
+        expected = '''\
+            usage: PROG [-h] [--foo | --bar] [--soup | --nuts]
+
+            optional arguments:
+              -h, --help  show this help message and exit
+              --foo
+              --bar
+              --soup
+              --nuts
+              '''
+        self.assertEqual(parser.format_help(), textwrap.dedent(expected))
 
 class MEMixin(object):