How can I get a callback to be run even on the event of an error in the python click framework?

89 Views Asked by At

I have a piece of code which I wish to always run at the end of a command execution, regardless of whether the command succeeded or raised an exception. I thought callbacks may be used for this, but it does not seem to be the case. How does one go about achieving this feature?

import click

@click.group()
def cli() -> None:
    """lorem ipsum"""
    print("whatever")


@click.command()
def command1() -> None:
    raise Exception"blah")


@click.command()
def command2() -> None:
    print("hi")


@cli.result_callback()
def process_result(*args: str, **kwargs: int) -> None:
    print("this should always appear")


cli.add_command(command1)
cli.add_command(command2)
0

There are 0 best solutions below