[issue15327] Argparse: main arguments and subparser arguments indistinguishable
samwyse
report at bugs.python.org
Sat Jul 14 14:05:16 CEST 2012
samwyse <samwyse at gmail.com> added the comment:
The Namespace object contains a combined list of all of the arguments, from both the main parser and the subparser. I don't see any way to resolve identically name augments without breaking a lot of code that relies on the current behavior. I would instead propose that anyone needing to distinguish between identically named args in nested parsers use distinct names as the primary name of the argument, and the non-distictive name as an alias. For example:
parser.add_argument('--verbose(main)', '--verbose', ...)
Running the attached file produces the following output.
# using non-distinctive args
['--verbose', 'command'] => Namespace(command='command', verbose=True)
['command', '--verbose'] => Namespace(command='command', verbose=True)
['--verbose', 'command', '--verbose'] => Namespace(command='command', verbose=True)
# using distinctive args
['--verbose', 'command'] => Namespace(command='command', verbose(main)=True, verbose(subcommand)=False)
['command', '--verbose'] => Namespace(command='command', verbose(main)=False, verbose(subcommand)=True)
['--verbose', 'command', '--verbose'] => Namespace(command='command', verbose(main)=True, verbose(subcommand)=True)
----------
nosy: +samwyse
Added file: http://bugs.python.org/file26379/argparsetest.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15327>
_______________________________________
More information about the Python-bugs-list
mailing list