Better handling of KeyboardInterrupt in cmd.Cmd command line interpreter

前端 未结 8 534
感情败类
感情败类 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:17

    I prefer the signal method, but with just pass. Don't really care about prompting the user with anything in my shell environment.

    import signal
    
    def handler(signum, frame):
        pass
    
    signal.signal(signal.SIGINT, handler)
    

提交回复
热议问题