停止点击(python 模块)捕获/处理键盘中断

Yul*_*lky 4 python error-handling python-3.x

我有以下代码:

import click


@click.command()
def main():
    while True:
        pass


try:
    main()
except KeyboardInterrupt:
    print('My own message!')
Run Code Online (Sandbox Code Playgroud)

当我按 Ctrl+C 退出程序时,我想打印自己的消息。但是,单击会拦截错误,这是输出:

^C
Aborted!
Run Code Online (Sandbox Code Playgroud)

如何阻止 click 处理错误?

Yul*_*lky 8

我想我已经用这段代码解决了我的问题!希望这是处理我的问题的正确方法。

import click


@click.command()
def main():
    while True:
        pass


try:
    main(standalone_mode=False)
except click.exceptions.Abort:
    print('My own message!')
Run Code Online (Sandbox Code Playgroud)