Noo*_*bot 2 python command-line-interface python-click
我的 Click 7.0 应用程序有一组,有多个命令,由主cli函数调用,如下所示:
import click
@click.group()
@click.pass_context
def cli(ctx):
"This is cli helptext"
click.echo('cli called')
click.echo('cli args: {0}'.format(ctx.args))
@cli.group(chain=True)
@click.option('-r', '--repeat', default=1, type=click.INT, help='repeat helptext')
@click.pass_context
def chainedgroup(ctx, repeat):
"This is chainedgroup helptext"
for _ in range(repeat):
click.echo('chainedgroup called')
click.echo('chainedgroup args: {0}'.format(ctx.args))
@chainedgroup.command()
@click.pass_context
def command1(ctx):
"This is command1 helptext"
print('command1 called')
print('command1 args: {0}'.format(ctx.args))
@chainedgroup.command()
@click.pass_context
def command2(ctx):
"This is command2 helptext"
print('command2 called')
print('command2 args: {0}'.format(ctx.args))
Run Code Online (Sandbox Code Playgroud)
跑:
$ testcli --help
$ testcli chainedgroup --help
$ testcli chainedgroup command1 --help
Run Code Online (Sandbox Code Playgroud)
帮助文本按预期显示——除了父函数无意中在进程中运行。单个条件检查以查看是否'--help'包含在中ctx.args应该足以解决这个问题,但是有没有人知道如何/何时'--help'通过?因为有了这个代码,ctx.args每次都是空的。