Better handling of KeyboardInterrupt in cmd.Cmd command line interpreter

前端 未结 8 558
感情败类
感情败类 2021-02-04 11:00

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:

         


        
8条回答
  •  面向向阳花
    2021-02-04 11:11

    You can check out the signal module: http://docs.python.org/library/signal.html

    import signal
    
    oldSignal = None
    
    def handler(signum, frame):
        global oldSignal
        if signum == 2:
            print "ctrl+c!!!!"
        else:
            oldSignal()
    
    oldSignal = signal.signal(signal.SIGINT, handler)
    

提交回复
热议问题