[issue9571] argparse: Allow the use of -- to break out of nargs and into subparser
paul j3
report at bugs.python.org
Mon Jul 7 02:18:27 CEST 2014
paul j3 added the comment:
In elsdoerfer's example, the '--' effectively ends the argument list for '--ignore'. '--' are not allowed in the arguments of an optional (see the end of '_get_nargs_pattern()').
Rather the problem is at the start of _get_values()
if action.nargs not in [PARSER, REMAINDER]:
arg_strings = [s for s in arg_strings if s != '--']
'--' is not stripped out of the 'parser' input, hence the error message:
error: invalid choice: '--'
You can see this by replacing the subparsers with an argument with 'nargs=PARSER'. The argument will get `['--','COMMAND',...]`.
http://bugs.python.org/issue13922 tries to rework how '--' are handled. Ideally only the current '--' should be removed, leaving the rest to be handled by the subparser (or whoever else gets the strings).
Looks like the 13922 fix needs another fix, one that removes '--' if it is the 1st string for a REMAINDER or PARSER argument.
With that fix:
./script.py --ignore one two -- COMMAND arg1 arg2
should work, assigning ['one','two'] to 'ignore', and ['arg1','arg2'] to 'COMMAND's positional.
(I've tested this in a development version with many other changes. I'll try to write a simpler patch.)
----------------
If a 'end of a list' flag is still needed (as between 2 * positionals), a 'counter' or 'store_true' optional could be used. Or a new action class that doesn't write anything to the namespace could be written.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9571>
_______________________________________
More information about the Python-bugs-list
mailing list