diff options
author | Steven Bethard <steven.bethard@gmail.com> | 2010-11-02 12:47:22 (GMT) |
---|---|---|
committer | Steven Bethard <steven.bethard@gmail.com> | 2010-11-02 12:47:22 (GMT) |
commit | fca2e8aeb8e841a18c4b9d2954254b2a086a3d56 (patch) | |
tree | 5139e39cd686fabb98dad2407c7df3b4a6d1c406 /Lib/argparse.py | |
parent | b68928bea70b0d9f33e0dc0a9c950291f8c65f15 (diff) | |
download | cpython-fca2e8aeb8e841a18c4b9d2954254b2a086a3d56.zip cpython-fca2e8aeb8e841a18c4b9d2954254b2a086a3d56.tar.gz cpython-fca2e8aeb8e841a18c4b9d2954254b2a086a3d56.tar.bz2 |
Fix bug 9340 - argparse parse_known_args didn't work with subparsers
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 38dd8e6..855ff5b 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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)) |