18
votes

I would like to implement logic like this in argparse:

If argument A is selected, the user cannot select arguments B or C.
B and C can both be selected

It looks like add_mutually_exclusive_group is what I would want for this, but it looks like you can only choose one option from a mutually exclusive group, so I cannot put all three in a mutually exclusive group.

Is there a way to do this in argparse?

3
Did you consider simply checking after argparse with an if? - erikbstack
I could, the nice thing about argparse is it handles the error message for you. - Kevin Burke
I though you could add a normal group of options using add_argument_group to the mutually exclusive group, but this does not work at all(it's like not having a mutually exclusive group at all). Probably the better choice you have is to write a custom action that implements this kind of logic. It should be too hard to do. - Bakuriu
I meant "should not" be too hard to do. - Bakuriu

3 Answers

5
votes

You could not really do it with argparse, however you can do it after argparse has run.

Here is an example:

parser = argparse.ArgumentParser()

# group 1 
parser.add_argument("-q", "--query", help="query", required=False)
parser.add_argument("-f", "--fields", help="field names", required=False)

# group 2 
parser.add_argument("-a", "--aggregation", help="aggregation",
                    required=False)

I am using here options given to a command line wrapper for querying a mongodb. The collection instance can either call the method aggregate or the method find with to optional arguments query and fields, hence you see why the first two arguments are compatible and the last one isn't.

So now I run parser.parse_args() and check it's content:

args = parser().parse_args()

print args.aggregation
if args.aggregation and (args.query or args.fields):
    print "-a and -q|-f are mutually exclusive ..."
    sys.exit(2)

Of course, this little hack is only working for simple cases and it would become a nightmare to check all the possible options if you have many mutually exclusive options and groups. In that case you should break your options in to command groups. For that, you should follow the suggestion here Python argparse mutual exclusive group.

1
votes

Docopt could work in this instance.

It uses pipes for mutually exclusive elements.

my_program (aggregate | find [-q | -f])
-1
votes

You could negate the meaning of A and then use an subparser. The subparsers allows you to specify that "If and only if A is selected, user can select B or C."

http://docs.python.org/2/library/argparse.html