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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:38:40+00:00 2026-06-14T08:38:40+00:00

While using python’s cmd.Cmd to create a custom CLI, how do I tell the

  • 0

While using python’s cmd.Cmd to create a custom CLI, how do I tell the handler to abort the current line and give me a new prompt?

Here is a minimal example:

# console_min.py
# run: 'python console_min.py'

import cmd, signal

class Console(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "[test] "
        signal.signal(signal.SIGINT, handler)

    def do_exit(self, args):
        return -1

    def do_EOF(self, args):
        return self.do_exit(args)

    def preloop(self):
        cmd.Cmd.preloop(self)
        self._hist    = []
        self._locals  = {}
        self._globals = {}

    def postloop(self):
        cmd.Cmd.postloop(self)
        print "Exiting ..."

    def precmd(self, line):
        self._hist += [ line.strip() ]
        return line

    def postcmd(self, stop, line):
        return stop

    def emptyline(self):
        return cmd.Cmd.emptyline(self)

    def handler(self, signum, frame):
        # self.emptyline() does not work here
        # return cmd.Cmd.emptyline(self) does not work here
        print "caught ctrl+c, press return to continue"

if __name__ == '__main__':
    console = Console()
    console.cmdloop()

Further help is greatly appreciated.

Original Question and more details:
[Currently the suggestions below have been integrated into this question — still searching for an answer. Updated to fix an error.]

I’ve since tried moving the handler to a function outside the loop to see if it were more flexible, but it does not appear to be.

I am using python’s cmd.Cmd module to create my own command line interpreter to manage interaction with some software. I often press ctrl+c expecting popular shell-like behavior of returning a new prompt without acting on whatever command was typed. However, it just exits. I’ve tried to catch KeyboardInterrupt exceptions at various points in the code (preloop, etc.) to no avail. I’ve read a bit on sigints but don’t quite know how that fits in here.

UPDATE: From suggestions below, I tried to implement signal and was able to do so such that ctrl+c now doesn’t exit the CLI but does print the message. However, my new issue is that I cannot seem to tell the handler function (see below) to do much beside print. I would like ctrl+c to basically abort the current line and give me a new prompt.

  • 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-14T08:38:41+00:00Added an answer on June 14, 2026 at 8:38 am

    I’m currently working on a creation of a Shell by using the Cmd module. I’ve been confronted with the same issue, and I found a solution.

    Here is the code:

    class Shell(Cmd, object)
    ...
        def cmdloop(self, intro=None):
            print(self.intro)
            while True:
                try:
                    super(Shell, self).cmdloop(intro="")
                    break
                except KeyboardInterrupt:
                    print("^C")
    ...
    

    Now you have a proper KeyboardInterrupt (aka CTRL-C) handler within the shell.

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

Sidebar

Related Questions

I'm very new to python and I am stuck trying to create module while
While using regex to help solve a problem in the Python Challenge , I
Writing a python program, and I came up with this error while using the
While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend
I've been using virtualenv lately while developing in python. I like the idea of
While using perl debugger, is there any way to step out of the current
I am using python 2.7 and MySQLdb, while doing import MySQLdb its throwing below
I wrote a scraper using python a while back, and it worked fine in
I've been using Python for quite a while now, and I'm still unsure as
I have been using a python script for a long while and all of

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.