[Python-checkins] r86092 - in python/branches/py3k/Lib: argparse.py test/test_argparse.py

steven.bethard python-checkins at python.org
Mon Nov 1 17:29:27 CET 2010


Author: steven.bethard
Date: Mon Nov  1 17:29:26 2010
New Revision: 86092

Log:
Fix for issue 9355 where with multiple mutually exclusive arguments, some brackets were being lost in the usage messages

Modified:
   python/branches/py3k/Lib/argparse.py
   python/branches/py3k/Lib/test/test_argparse.py

Modified: python/branches/py3k/Lib/argparse.py
==============================================================================
--- python/branches/py3k/Lib/argparse.py	(original)
+++ python/branches/py3k/Lib/argparse.py	Mon Nov  1 17:29:26 2010
@@ -392,10 +392,16 @@
                     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] = '|'

Modified: python/branches/py3k/Lib/test/test_argparse.py
==============================================================================
--- python/branches/py3k/Lib/test/test_argparse.py	(original)
+++ python/branches/py3k/Lib/test/test_argparse.py	Mon Nov  1 17:29:26 2010
@@ -2163,6 +2163,25 @@
         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):
 


More information about the Python-checkins mailing list