0

I need one group of mutually exclusive parameters is required or not dependents on the value of a parameter, e.g.

I have three parameters -a -b -c

-b, -c are mutually exclusive group

The following commands are some examples ok and not ok:

  1. python test.py # ok, no parameter is assigned

when -a is assigned a value, -b or -c need to be enabled:

  1. python test.py -a 100 -b # ok
  2. python test.py -a 100 -c # ok
  3. python test.py -a 100 # not ok, b or c is not enabled
  4. python test.py -a 100 - b -c # not ok, b and c are both enabled

when -a is not assigned a value, -b and -c can't be enabled. Thus the following cases are not ok.

  1. python test.py -b
  2. python test.py -c
  3. python test.py -b -c

Does argparse support this kinds of functions?

If so, how could I achieve it? Thanks!

Community
  • 1
  • 1
Wilson
  • 536
  • 1
  • 5
  • 15
  • If you're willing to make `-a` a subcommand rather than an option, you could use a [subparser](https://docs.python.org/3/library/argparse.html#sub-commands). – glibdud Nov 10 '16 at 13:36
  • testing after parsing is the simplest method. This has been discussed a many other `argparse` questions. `argparse` itself only has the flat mutually exclusive test. – hpaulj Nov 10 '16 at 17:47

0 Answers0