]> granicus.if.org Git - python/commitdiff
Fix bug 9340 - argparse parse_known_args didn't work with subparsers
authorSteven Bethard <steven.bethard@gmail.com>
Tue, 2 Nov 2010 12:47:22 +0000 (12:47 +0000)
committerSteven Bethard <steven.bethard@gmail.com>
Tue, 2 Nov 2010 12:47:22 +0000 (12:47 +0000)
Lib/argparse.py
Lib/test/test_argparse.py

index 38dd8e6b9d338e14ebcd983bae2f0d95d25054e1..855ff5b93f8f74738d88f2b66d6a9c4d9eb62a3b 100644 (file)
@@ -102,6 +102,7 @@ ZERO_OR_MORE = '*'
 ONE_OR_MORE = '+'
 PARSER = 'A...'
 REMAINDER = '...'
+_UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
 
 # =============================
 # Utility functions and classes
@@ -1083,7 +1084,12 @@ class _SubParsersAction(Action):
             raise ArgumentError(self, msg)
 
         # parse all the remaining options into the namespace
-        parser.parse_args(arg_strings, namespace)
+        # store any unrecognized options on the object, so that the top
+        # level parser can decide what to do with them
+        namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
+        if arg_strings:
+            vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
+            getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
 
 
 # ==============
@@ -1699,7 +1705,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
 
         # parse the arguments and exit if there are any errors
         try:
-            return self._parse_known_args(args, namespace)
+            namespace, args = self._parse_known_args(args, namespace)
+            if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
+                args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
+                delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
+            return namespace, args
         except ArgumentError:
             err = _sys.exc_info()[1]
             self.error(str(err))
index 1503ccd5d197ecff81d1642eca54a141a29c2400..74075bb4e7699b62f5d6c2f4d9bfff7bc12f4a94 100644 (file)
@@ -1773,6 +1773,28 @@ class TestAddSubparsers(TestCase):
             NS(foo=True, bar=0.125, w=None, x='c'),
         )
 
+    def test_parse_known_args(self):
+        self.assertEqual(
+            self.parser.parse_known_args('0.5 1 b -w 7'.split()),
+            (NS(foo=False, bar=0.5, w=7, x='b'), []),
+        )
+        self.assertEqual(
+            self.parser.parse_known_args('0.5 -p 1 b -w 7'.split()),
+            (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']),
+        )
+        self.assertEqual(
+            self.parser.parse_known_args('0.5 1 b -w 7 -p'.split()),
+            (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']),
+        )
+        self.assertEqual(
+            self.parser.parse_known_args('0.5 1 b -q -rs -w 7'.split()),
+            (NS(foo=False, bar=0.5, w=7, x='b'), ['-q', '-rs']),
+        )
+        self.assertEqual(
+            self.parser.parse_known_args('0.5 -W 1 b -X Y -w 7 Z'.split()),
+            (NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']),
+        )
+
     def test_dest(self):
         parser = ErrorRaisingArgumentParser()
         parser.add_argument('--foo', action='store_true')