0

I’d like to be able to make a group of parameters mutually exclusive with another parameter (or another group). Something like:

program.py [-a alpha] [-b -c gamma -d delta]

The requirements are:

  • You can use -a or -b (or both, or none)
  • If you use -b, you MUST also use -c and -d

Is it possible at all? I think I could use a sub_parser, but I’m not sure how…

gregseth
  • 12,952
  • 15
  • 63
  • 96
  • can't you take n arguements and run through your requirements ? – hvaminion Jul 08 '19 at 12:02
  • 1
    `sub_parser` is a great tool, but not applicable here since you want the option of having both `-a` and `-b` or any... using `sub_parser` you can only go down one "branch" – Tomerikoo Jul 08 '19 at 13:10
  • 1
    The provided mutually exclusive mechanism only works with a flat list of parameters. There isn't a builtin way of applying it to groups. Usually we recommend doing your own testing after parsing. – hpaulj Jul 08 '19 at 17:20

1 Answers1

2

While the answer linked here is likely for python2.X, from my research it still holds true. To adapt the answer:

if args.b and (args.c is None or args.d is None):
parser.error("--b requires --c and --d.")
Ken N
  • 76
  • 5