如何让Click
在其帮助文字中显示@click.option()
的默认输入值,以便在使用--help
调用该程序时打印它?
答案 0 :(得分:7)
在定义选项时,在show_default=True
装饰器中传递click.option
。当使用--help
选项调用程序时,这将在帮助中显示默认值。
例如 -
#hello.py
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.', show_default=True)
@click.option('--name', prompt='Your name',
help='The person to greet.')
def hello(count, name):
"""<insert text that you want to display in help screen> e.g: 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()
现在,您可以通过将python hello.py --help
作为
$ python hello.py --help
Usage: hello.py [OPTIONS]
<insert text that you want to display in help screen> e.g: Simple program that greets NAME for a total of COUNT times.
Options:
--count INTEGER Number of greetings. [default: 1]
--name TEXT The person to greet.
--help Show this message and exit.
因此,您可以看到count
选项的默认值显示在程序的帮助文本中。 (参考:https://github.com/pallets/click/issues/243)