我的 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))
运行:
$ testcli --help
$ testcli chainedgroup --help
$ testcli chainedgroup command1 --help
帮助文本按预期显示 - 只不过父函数在进程中无意中运行。单个条件检查是否 '--help'
包含在ctx.args
中应该足以解决这个问题,但是有谁知道如何/何时'--help'
通过了吗?因为有了这段代码,ctx.args
每次都是空的。
最佳答案
如果 argparse 不是一个选项,那么:
if '--help' in sys.argv:
...
关于python - 如何检测 '--help' 何时被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56420331/