argparse -- mutually exclusive sets of arguments?

Terry Reedy tjreedy at
Fri Nov 23 19:56:50 CET 2012

On 11/23/2012 1:46 PM, Roy Smith wrote:
> 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.

Make the two positional arguments be one duple?
Or tell argparse that all three are optional and handle the 'more 
complicated logic' in your own code after argparse returns.

Terry Jan Reedy

More information about the Python-list mailing list