Message212228
http://stackoverflow.com/questions/11455218
python, argparse: enable input parameter when another one has been specified
$ python myScript.py --parameter1 value1
$ python myScript.py --parameter1 value1 --parameter2 value2
$ python myScript.py --parameter2 value2 # error
This is an example where a 'mutually inclusive group' wouldn't quite do the job.
The proposed answers mostly use a custom Action. I'd lean toward an after-the-parse test of the namespace.
With the patch I proposed this could be implemented with:
a1 = parser.add_argument("--parameter1")
a2 = parser.add_argument("--parameter2")
def test(parser, seen_actions, *args):
if a2 in seen_actions and a1 not in seen_actions:
parser.error('parameter2 requires parameter1')
parser.register('cross_tests', 'test', test)
One poster on that thread claimed that the use of 'a1 = parser.add_argument...' is using an undocumented feature. The fact that `add_argument` returns an `action` object, should be illustrated in the documentation, and may be explicitly noted. I'll have to review the documentation to see if this is the case. |
|
Date |
User |
Action |
Args |
2014-02-25 23:33:01 | paul.j3 | set | recipients:
+ paul.j3, bethard, xuanji, John.Didion, manveru |
2014-02-25 23:33:01 | paul.j3 | set | messageid: <1393371181.36.0.145323999215.issue11588@psf.upfronthosting.co.za> |
2014-02-25 23:33:01 | paul.j3 | link | issue11588 messages |
2014-02-25 23:33:00 | paul.j3 | create | |
|