--help 选项中 Click.option 的类型和默认输入值

Type and default input value of a Click.option in --help option

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

在定义选项时在 click.option 装饰器中传递 show_default=True。当使用 --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