import functools


def skip_click_exit(func):
    """
    When a click command is executed programatically a SystemExit is executed.

    This decorator avoids SystemExit when execution is finished.
    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # add standalone_mode=False to command execution
        # this enables command returns
        # more info: https://click.palletsprojects.com/en/5.x/commands/?highlight=standalone_mode#command-return-values # noqa: E501
        try:
            return func(*args, standalone_mode=False, **kwargs)
        # click commands always raise a SystemExit
        # this avoid exiting the command execution in NRE
        except SystemExit:
            pass

    return wrapper