Author paul.j3
Recipients bethard, chris.jerdonek, paul.j3
Date 2013-04-17.17:03:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366218202.5.0.50925236797.issue17050@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-04-17 17:03:22paul.j3setrecipients: + paul.j3, bethard, chris.jerdonek
2013-04-17 17:03:22paul.j3setmessageid: <1366218202.5.0.50925236797.issue17050@psf.upfronthosting.co.za>
2013-04-17 17:03:22paul.j3linkissue17050 messages
2013-04-17 17:03:22paul.j3create