1

I would like to achieve the following behavior in argparse

pro [-a xxx | [-b yyy -c zzz]] -d uuu -e vvv -f www

This is an extension to the problem in "Python argparse mutual exclusive group" which recommends using subparsers.

But in this case, creating two subparsers to make the set of arguments -a and set -b and -c exclusive means duplicating -d, -e, -f in each subparser.

Is there another way to do this within argparse? Or the checks will have to be done manually?

kentwait
  • 1,969
  • 2
  • 21
  • 42
  • Btw a hack would be to use positional arguments with nargs and create a mutually-exclusive group, but in this case it's 2 sets of mutually-exclusive keyword arguments – kentwait Mar 14 '19 at 03:50
  • Is it `-b or -c` or `-b and -c`? If the second, why not define a `--bc` with `nargs=2`? In any case, as discussed in the link, the existing testing in `argparse` is a simple flat `xor`, without any `and/or` subgroups. But you can always do the required testing after parsing. Just use `if args.a is None:` to test whether `-a` has been used or not. – hpaulj Mar 14 '19 at 04:08

0 Answers0