[issue9351] argparse set_defaults on subcommands should override top level set_defaults
Steven Bethard
report at bugs.python.org
Fri Jul 23 16:06:52 CEST 2010
New submission from Steven Bethard <steven.bethard at gmail.com>:
If you use set_defaults on a subparser, but a default exists on the top level parser, the subparser defaults are ignored:
>>> parser = argparse.ArgumentParser()
>>> xparser = parser.add_subparsers().add_parser('X')
>>> parser.set_defaults(foo=1)
>>> xparser.set_defaults(foo=2)
>>> parser.parse_args(['X'])
Namespace(foo=1)
This is counter to what people probably expect, that the subparser, when selected, would override the top level parser.
The behavior is because of the following code in parse_known_args:
for dest in self._defaults:
if not hasattr(namespace, dest):
setattr(namespace, dest, self._defaults[dest])
This happens before the subparser sees the namespace object, and so the subparser sees that no defaults need to be filled in.
----------
components: Library (Lib)
messages: 111324
nosy: bethard
priority: normal
severity: normal
stage: needs patch
status: open
title: argparse set_defaults on subcommands should override top level set_defaults
type: behavior
versions: Python 2.7, Python 3.2
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9351>
_______________________________________
More information about the Python-bugs-list
mailing list