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 8696089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:11:53+00:00 2026-06-13T01:11:53+00:00

I have a python option parsers that parses an optional –list-something option. I also

  • 0

I have a python option parsers that parses an optional –list-something option.
I also want the –list-something option to have an optional argument (an option)

Using the argument default="simple" does not work here, otherwise simple will always
be the default, not only when –list-something was given.

from optparse import OptionParser, OptionGroup
parser = OptionParser()
options = OptionGroup(parser, "options")
options.add_option("--list-something",
                   type="choice",
                   choices=["simple", "detailed"],
                   help="show list of available things"
                  )
parser.add_option_group(options)
opts, args = parser.parse_args()
print opts, args

The above code is producing this:

[jens@ca60c173 ~]$ python main.py --list-something simple
{'list_something': 'simple'} []
[jens@ca60c173 ~]$ python main.py --list-something 
Usage: main.py [options]

main.py: error: --list-something option requires an argument
[jens@ca60c173 ~]$ python main.py 
{'list_something': None} []

But I want this to hapen:

[jens@ca60c173 ~]$ python main.py --list-something simple
{'list_something': 'simple'} []
[jens@ca60c173 ~]$ python main.py --list-something 
{'list_something': 'simple'} []
[jens@ca60c173 ~]$ python main.py 
{'list_something': None} []

I would like something that works out of the box in python 2.4 up till 3.0 (3.0 not included)

Since argparse is only introduced in python 2.7 this is not something I could use.

  • 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-06-13T01:11:54+00:00Added an answer on June 13, 2026 at 1:11 am

    Optparse does not have any options for doing this easily. Instead you’ll have to create a custom callback for your option. The callback is triggered when your option is parsed, at which point, you can check the remaining args to see if the user put an argument for the option.

    Check out the custom callback section of the docs, in particular Callback example 6: variable arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Python nested list that I'm trying to organize and eventually count number
A question that seems to have quite a few options for Python, but none
i have an issue i could use some help with, i have python list
i have a python program, this program will call a cmd(showTxt.exe) to do something,
I have a script in python that can be invoked from the command-line and
I have a python module which generates large data files which I want to
I have been given a task to create a Python file that will take
I have some python code which generates a 256^3 numpy array of data that
I currently have a python file that utilizes sys.argv[1] to accept a string at
I have renamed a python class that is part of a library. I am

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.