问题
I have a click.group()
defined, with about 10 commands in it. I understand how to use a group to run code before the code in the command, but I also want to run some code AFTER each command is run. Is that possible with click?
回答1:
You can use @resultcallback decorator
@click.group()
def cli():
click.echo('Before command')
@cli.resultcallback()
def process_result(result, **kwargs):
click.echo('After command')
@cli.command()
def command():
click.echo('Command')
if __name__ == '__main__':
cli()
>> python cli.py command
>> Before command
>> Command
>> After command
来源:https://stackoverflow.com/questions/38164324/python-click-having-the-group-execute-code-after-a-command