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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No need for the try catch block to handle a… May 16, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer From Xtream How do I specify that a field should… May 16, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer Best is to use some appropriate combination of these window… May 16, 2026 at 5:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm using a small Python script to generate some binary data that will be
I have created a LaTeX \todo{} command which outputs todo notes in the margin:
Today, as I tried to put together a script in Octave, I thought, this
I would like to convert flat form data to recursive JSON data in python
I'm trying to run an Icecast stream using a simple Python script to pick
I'm working on a small Python script to clean up HTML documents. It works
I have a python script that is trying to create a directory tree dynamically
I'm writing a script to generate a CSR in Python. The script is very
I want to search for images using a Python script and pull them off
I wrote a python script that rotates an image 90 degrees. I am including

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.