阻止click(Python模块)捕获/处理KeyboardInterrupt

4

I have the following code:

import click


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


try:
    main()
except KeyboardInterrupt:
    print('My own message!')

当我按下 Ctrl+C 退出程序时,我想打印自己的信息。但是,点击会拦截错误,这是输出结果:
^C
Aborted!

我该如何阻止 click 处理错误?

1个回答

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


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


try:
    main(standalone_mode=False)
except click.exceptions.Abort:
    print('My own message!')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接