paul j3 added the comment:
Here's a way of passing an optional-like argument to a subparser:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='cmd')
sub1 = subparsers.add_parser('cmd')
sub1.add_argument('foo',nargs='*')
args = parser.parse_args('cmd -- --def 1 2 3'.split())
producing
Namespace(cmd='cmd', foo=['--def', '1', '2', '3'])
The '--' forces the parser to treat '--def' as a positional. If
nargs='REMAINDER', foo=['--', '--def', ...].
But the following subparser definition would be even better:
sub1.add_argument('--def', action='store_true')
sub1.add_argument('rest',nargs='...')
Here the '--def' is handle explicitly, as opposed to being passed on.
You don't need the whole subparsers mechanism if you are just going to pass
those arguments (unparsed) to another program.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue17050>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com