Stop click (python module) from catching/handling KeyboardInterrupt

Viewed 702

I have the following code:

import click


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


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

When I press Ctrl+C to exit the program, I want to print my own message. However, click intercepts the error and this is the output:

^C
Aborted!

How can I stop click from handling the errors?

1 Answers

I think I've solved my problem with this code! Hopefully, it's the correct way to handle my problem.

import click


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


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