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

steven.bethard python-checkins at python.org
Mon Nov 1 16:57:37 CET 2010


Author: steven.bethard
Date: Mon Nov  1 16:57:36 2010
New Revision: 86090

Log:
Fix bug 9352 where characters were being lost in parsing some short options

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 16:57:36 2010
@@ -1794,13 +1794,13 @@
                     chars = self.prefix_chars
                     if arg_count == 0 and option_string[1] not in chars:
                         action_tuples.append((action, [], option_string))
-                        for char in self.prefix_chars:
-                            option_string = char + explicit_arg[0]
-                            explicit_arg = explicit_arg[1:] or None
-                            optionals_map = self._option_string_actions
-                            if option_string in optionals_map:
-                                action = optionals_map[option_string]
-                                break
+                        char = option_string[0]
+                        option_string = char + explicit_arg[0]
+                        new_explicit_arg = explicit_arg[1:] or None
+                        optionals_map = self._option_string_actions
+                        if option_string in optionals_map:
+                            action = optionals_map[option_string]
+                            explicit_arg = new_explicit_arg
                         else:
                             msg = _('ignored explicit argument %r')
                             raise ArgumentError(action, msg % explicit_arg)

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 16:57:36 2010
@@ -465,6 +465,30 @@
         ('/ba +f', NS(f=True, bar=None, baz=42))
     ]
 
+
+class TestOptionalsAlternatePrefixCharsMultipleShortArgs(ParserTestCase):
+    """Verify that Optionals must be called with their defined prefixes"""
+
+    parser_signature = Sig(prefix_chars='+-', add_help=False)
+    argument_signatures = [
+        Sig('-x', action='store_true'),
+        Sig('+y', action='store_true'),
+        Sig('+z', action='store_true'),
+    ]
+    failures = ['-w',
+                '-xyz',
+                '+x',
+                '-y',
+                '+xyz',
+    ]
+    successes = [
+        ('', NS(x=False, y=False, z=False)),
+        ('-x', NS(x=True, y=False, z=False)),
+        ('+y -x', NS(x=True, y=True, z=False)),
+        ('+yz -x', NS(x=True, y=True, z=True)),
+    ]
+
+
 class TestOptionalsShortLong(ParserTestCase):
     """Test a combination of single- and double-dash option strings"""
 


More information about the Python-checkins mailing list