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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:00:37+00:00 2026-05-17T21:00:37+00:00

my python version is 2.4.3. Now I am developing a CLI with cmd module

  • 0

my python version is 2.4.3.

Now I am developing a CLI with cmd module from python for a CD player. I have some classes like CDContainer (with method like addCD, removeCD, etc), CD (with method like play, stop, pause). Now, I want to add some options for the commands and also if the options inputs are not correct, the CLI could return proper information about either wrong input type or wrong values. e.g., I want to have “addcd –track 3 –cdname thriller”. what I am doing now is to get all the arguments via , split it, and assign it to the relevant variables, as follows.

my question is in python, is there some module that is handy for my case to parse and analyse the options or arguments ?

REVISION: OK, I edit it, thanks to gclj5 comments.

import cmd
class CDContainerCLI(cmd.Cmd):

    def do_addcd(self, line):
        args=line.split()
        parser = OptionParser()
        parser.add_option("-t", "--track", dest="track_number", type="int",
            help="track number")
        parser.add_option("-n", "--cdname", dest="cd_name", type="string",
            help="CD name")
        (options, positional_args) = parser.parse_args(args)
        cd_obj= CD()
        cd_obj.addCD(options.track_number, options.cd_name)

If possible, could you write some code samples, just to show how to do it?

Thank you very much!!

  • 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-17T21:00:38+00:00Added an answer on May 17, 2026 at 9:00 pm

    Depending on your Python version, you should take a look at either optparse (since version 2.3, deprecated since version 2.7) or argparse (since version 2.7).

    Some sample code using optparse (line is the string you read from stdin in your CLI):

    from optparse import OptionParser
    
    args = line.split()
    
    parser = OptionParser()
    parser.add_option("-t", "--track", dest="track_number", type="int",
                      help="track number")
    parser.add_option("-n", "--cdname", dest="cd_name", type="string",
                      help="CD name")
    
    # args[0] contains the actual command ("addcd" in this example).
    (options, positional_args) = add_cd_parser.parse_args(args[1:])
    
    if options.track_number != None and options.cd_name != None:
        cd_obj= CD()
        cd_obj.addCD(options.track_number, options.cd_name)
        print "add CD (track %d, name %s)" % (options.track_number, options.cd_name)
    

    This parser only handles your “addcd” command. For more commands you could use several OptionParser objects in a dictionary with the command name as the key, for instance. You could parse the options like this then:

    (options, args) = parsers[args[0]].parse_args(args[1:])
    

    Take a look at the documentation for optparse for more information. It’s very easy to output usage information, for instance. There is also a tutorial available.

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

Sidebar

Related Questions

I'd like to know what versions of Python and wxPython correspond to each version
I have a script, which contains some packages/modules. Now, the development and the production
If I was going to start an open source project using Python what version
I want to use the macports version of python instead of the one that
what are your advices on building a very minimalistic version of Python(2.x) for my
Is there a standard way to associate version string with a Python package in
Does beautiful soup work with iron python? If so with which version of iron
Python uses the reference count method to handle object life time. So an object
I am trying to upgrade a small C module to work with Python 3.x
I have been trying for several hours now to get sorl-thumbnail working, but it

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.