9

如何在其帮助文本中Click显示 a 的默认输入值@click.option(),以便在调用程序时打印它--help

4

1 回答 1

12

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现在您可以看到运行as生成的帮助屏幕

$ 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 )

于 2016-09-07T00:34:44.123 回答