Message187182
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. |
|
Date |
User |
Action |
Args |
2013-04-17 17:03:22 | paul.j3 | set | recipients:
+ paul.j3, bethard, chris.jerdonek |
2013-04-17 17:03:22 | paul.j3 | set | messageid: <1366218202.5.0.50925236797.issue17050@psf.upfronthosting.co.za> |
2013-04-17 17:03:22 | paul.j3 | link | issue17050 messages |
2013-04-17 17:03:22 | paul.j3 | create | |
|