Python Click: Having the group execute code AFTER a command

孤街浪徒 提交于 2020-05-26 10:49:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!