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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:42:34+00:00 2026-05-17T19:42:34+00:00

HI, guys. I am using cmd and optparse to develop a CLI.py for a

  • 0

HI, guys.

I am using cmd and optparse to develop a CLI.py for a collection of already-functional classes (CDContainer, CD, etc.). The following are some parts of the code. I have a problem here. when there are exceptions(wrong input type or missing values), the optparse will exit the whole program instead of the specific command method.

import cmd
class CLI(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[0:])
        cd_obj= CD()
        cd_obj.addCD(options.track_number, options.cd_name)

Under “>python”, if I type CLI.py,
then I will have (Cmd), so I could type command like “(Cmd)addcd -t 3 -n thriller”.
but if I type “addcd -t r -n 3”, then optparse will terminate the whole CLI.py and exit.
This is not good for me. I want to remind the user for each method, instead of terminating the whole program.

however, the optparse documentation says “the whole program exits”. so I could not use optparse “blindly”. what can I do?

  • 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-17T19:42:35+00:00Added an answer on May 17, 2026 at 7:42 pm

    The optparse documentation says this:

    If optparse‘s default error-handling behaviour does not suit your needs, you’ll need to subclass OptionParser and override its exit() and/or error() methods.

    Ideally you’d define a new type of exception, subclass optparse, raise the exception in the exit() or error() method that you’ve overridden, and then catch it and deal with it as needed.

    You can cheat, though. If you want the error message printed but just don’t want the program to exit, then you can catch the SystemExit exception to catch where optparse is trying to exit and stop it.

    So, for example:

    try:    
        (options, positional_args) = parser.parse_args(args[0:])
    except SystemExit:
        return
    
    cd_obj= CD()
    cd_obj.addCD(options.track_number, options.cd_name)
    

    or to override the method:

    import optparse
    
    class OptionParsingError(RuntimeError):
        def __init__(self, msg):
            self.msg = msg
    
    class OptionParsingExit(Exception):
        def __init__(self, status, msg):
            self.msg = msg
            self.status = status
    
    class ModifiedOptionParser(optparse.OptionParser):
        def error(self, msg):
            raise OptionParsingError(msg)
    
        def exit(self, status=0, msg=None):
            raise OptionParsingExit(status, msg)
    

    and then:

    try:
        parser = ModifiedOptionParser()
        parser.add_option("-t", "--track", dest="track_number", type="int",
            help="track number")
        (options, positional_args) = parser.parse_args(args[0:])
    except OptionParsingError, e:
        print 'There was a parsing error: %s' % e.msg
        return
    except OptionParsingExit, e:
        print 'The option parser exited with message %s and result code %s' % (e.msg, e.status)
        return
    
    cd_obj= CD()
    cd_obj.addCD(options.track_number, options.cd_name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What do you guys think about this for a generic singleton? using System; using
Ok guys just a small game: I have some specifications for a project. At
Guys, can someone give me a brief run through of how to change the
Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
Hi Guys could you please help me refactor this so that it is sensibly
Ok guys and gals, here is my problem: I've built a custom control that
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
Hi Guys can you please help me with this error? What is it? Server
You guys were very helpful yesterday. I am still a bit confused here though.
Hi guys anyone know what the helll is going on here? ERROR SNIPPET: Loading

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.