diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-10-17 23:55:11 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-10-17 23:55:11 (GMT) |
commit | 7570cbdc6b394cd89990a9252284c7e4a87bd6f1 (patch) | |
tree | f1427aed1d42f7de3dc9c58954e2930f0919aa04 /Lib/argparse.py | |
parent | 685b3495e1fd9853b1ca5960db3e3cb705920d4f (diff) | |
download | cpython-7570cbdc6b394cd89990a9252284c7e4a87bd6f1.zip cpython-7570cbdc6b394cd89990a9252284c7e4a87bd6f1.tar.gz cpython-7570cbdc6b394cd89990a9252284c7e4a87bd6f1.tar.bz2 |
#9351: set_defaults on subparser is no longer ignored if set on parent.
Before, if a default was set on the parent parser, any default for that
variable set via set_defaults on a subparser would be ignored. Now
the subparser set_defaults is honored.
Patch by Jyrki Pullianinen.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 83878b1..4d26b07 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1122,7 +1122,14 @@ class _SubParsersAction(Action): # parse all the remaining options into the 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) + + # In case this subparser defines new defaults, we parse them + # in a new namespace object and then update the original + # namespace for the relevant parts. + subnamespace, arg_strings = parser.parse_known_args(arg_strings, None) + for key, value in vars(subnamespace).items(): + setattr(namespace, key, value) + if arg_strings: vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings) |