Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:06:20+00:00 2026-05-14T15:06:20+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:06:21+00:00Added an answer on May 14, 2026 at 3:06 pm

    Possibly by extending optparse.OptionParser:

    class Conflict(object):
        __slots__ = ("combination", "message", "parser")
    
        def __init__(self, combination, message, parser):
            self.combination = combination
            self.message = str(message)
            self.parser = parser
    
        def accepts(self, options):
            count = sum(1 for option in self.combination if hasattr(options, option))
            return count <= 1
    
    class ConflictError(Exception):
        def __init__(self, conflict):
            self.conflict = conflict
    
        def __str__(self):
            return self.conflict.message
    
    class MyOptionParser(optparse.OptionParser):
        def __init__(self, *args, **kwds):
            optparse.OptionParser.__init__(self, *args, **kwds)
            self.conflicts = []
    
        def set_not_allowed(self, combination, message):
            self.conflicts.append(Conflict(combination, message, self))
    
        def parse_args(self, *args, **kwds):
            # Force-ignore the default values and parse the arguments first
            kwds2 = dict(kwds)
            kwds2["values"] = optparse.Values()
            options, _ = optparse.OptionParser.parse_args(self, *args, **kwds2)
    
            # Check for conflicts
            for conflict in self.conflicts:
                if not conflict.accepts(options):
                    raise ConflictError(conflict)
    
            # Parse the arguments once again, now with defaults
            return optparse.OptionParser.parse_args(self, *args, **kwds)
    

    You can then handle ConflictError where you call parse_args:

    try:
        options, args = parser.parse_args()
    except ConflictError as err:
        parser.error(err.message)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a simple GUI Python script to do some simple tasks on
I don't know what to do, I have a 39 line Python script and
My python script executes fine in Jail shell, putting out html which I can
I have a small python script which draws some turtle graphics. When my script
I have a simple python script (say, simple.py) , like a = 5 b
I would like to build a small python script that basicaly does the reverse
I'm using a small Python script to generate some binary data that will be
I want to run a Python script from another Python script. I want to
I have a python script that creates a list of lists of server uptime
I have a simple python script which calls 6 threads and for some reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.