argparse -- mutually exclusive sets of arguments?

Roy Smith roy at
Fri Nov 23 19:46:25 CET 2012

My command either takes two positional arguments (in which case, both 
are required):

$ command foo bar

or the name of a config file (in which case, the positional arguments 
are forbidden):

$ command --config file

How can I represent this with argparse; add_mutually_exclusive_group() 
isn't quite the right thing.  It could specify that foo and --config are 
mutually exclusive, but not (as far as I can see) the more complicated 
logic described above.

More information about the Python-list mailing list