diff Lib/argparse.py @ 65888:573db2af2dda 2.7

Merged revisions 86111 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r86111 | steven.bethard | 2010-11-02 13:47:22 +0100 (Tue, 02 Nov 2010) | 1 line Fix bug 9340 - argparse parse_known_args didn't work with subparsers ........
author Steven Bethard <steven.bethard@gmail.com>
date Tue, 02 Nov 2010 12:48:15 +0000
parents 5952b937ab9a
children 556df087de4f
line wrap: on
line diff
--- a/Lib/argparse.py	Tue Nov 02 04:04:37 2010 +0000
+++ b/Lib/argparse.py	Tue Nov 02 12:48:15 2010 +0000
@@ -102,6 +102,7 @@
 ONE_OR_MORE = '+'
 PARSER = 'A...'
 REMAINDER = '...'
+_UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
 
 # =============================
 # Utility functions and classes
@@ -1083,7 +1084,12 @@
             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)
 
 
 # ==============
@@ -1701,7 +1707,11 @@
 
         # 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))