Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up mutually exclusive sets in argparse

I'm using a mutually exclusive group, but I more than one option to be in one of the exclusive sets. I want my usage to look like this:

[--conf CONF | --hostname HOSTNAME --port PORT]

Any idea how to accompish this? I've tried adding an argument group to a mutually exclusive group, but the mutual exclusion part doesn't apply to subgroups, and it allows --conf file --hostname host --port 22

like image 360
Lucretiel Avatar asked Feb 20 '13 16:02

Lucretiel


1 Answers

Not an answer that uses argparse, but a possible solution to your problem nonetheless: using docopt instead of argparse would let you just write your usage patterns, and let docopt automatically parse and validate the args passed to your program. In your case,

prog_name (--conf CONF | --hostname HOST --port PORT)

would get you the mutually exclusive group you want.

like image 78
Ryan Artecona Avatar answered Sep 21 '22 06:09

Ryan Artecona