How do you handle options that can't be used together with OptionParser?

Posted by Joel on Stack Overflow See other posts from Stack Overflow or by Joel
Published on 2010-04-28T12:39:17Z Indexed on 2010/04/28 12:43 UTC
Read the original article Hit count: 192

Filed under:
|

My Python script (for todo lists) is started from the command line like this:

todo [options] <command> [command-options]

Some options can not be used together, for example

todo add --pos=3 --end "Ask Stackoverflow"

would specify both the third position and the end of the list. Likewise

todo list --brief --informative

would confuse my program about being brief or informative. Since I want to have quite a powerful option control, cases like these will be a bunch, and new ones will surely arise in the future. If a users passes a bad combination of options, I want to give an informative message, preferably along with the usage help provided by optparse. Currently I handle this with an if-else statement that I find really ugly and poor. My dream is to have something like this in my code:

parser.set_not_allowed(combination=["--pos", "--end"], 
                       message="--pos and --end can not be used together")

and the OptionParser would use this when parsing the options.

Since this doesn't exist as far as I know, I ask the SO community: How do you handle this?

© Stack Overflow or respective owner

Related posts about python

Related posts about optparse