[英]How do I detect when '--help' has been called?
My Click 7.0 application has one group, having multiple commands, called by the main cli
function like so:我的 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:跑:
$ testcli --help
$ testcli chainedgroup --help
$ testcli chainedgroup command1 --help
The help-text displays as expected--except that the parent functions are inadvertently run in the process.帮助文本按预期显示——除了父函数无意中在进程中运行。 A single conditional checking to see if
'--help'
is contained in ctx.args
should be enough to solve this problem, but does anyone know how/when '--help'
is passed?单个条件检查以查看
ctx.args
是否包含'--help'
应该足以解决这个问题,但是有人知道如何/何时通过'--help'
吗? Because with this code, ctx.args
is empty every time.因为有了这段代码,
ctx.args
都是空的。
If argparse is not an option, how about:如果 argparse 不是一个选项,那么如何:
if '--help' in sys.argv:
...
click
stores the arguments passed to a command in a list. click
将传递给命令的参数存储在列表中。 The method get_os_args()
returns such list.方法
get_os_args()
返回这样的列表。 You can check if --help
is in that list to determine if the help
flag was invoked.您可以检查
--help
是否在该列表中以确定是否调用了help
标志。 Something like the following:类似于以下内容:
if '--help' in click.get_os_args():
pass
It is prebuilt - Click looks like a decorator for argparse (Hurrah for common sense).它是预先构建的 - Click 看起来像 argparse 的装饰器(常识万岁)。
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == '__main__':
hello()
So you can write所以你可以写
python cl.py --name bob
And see看看
Hello bob!你好鲍勃!
Help is already done (as it is argparse)帮助已经完成(因为它是argparse)
python cl.py --help
Usage: cl.py [OPTIONS]
Simple program that greets NAME for a total of COUNT times.
Options:
--count INTEGER Number of greetings.
--name TEXT The person to greet.
--help Show this message and exit.
Been busy only just had time to read into this.一直很忙,才有时间读到这里。
Sorry for the delay抱歉耽搁了
Why not use argparse ?为什么不使用 argparse ? It has excellent for CLI parsing.
它非常适合 CLI 解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.