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

1

13

How do I make Click show the default input value of a @click.option() in its help text, so that it gets printed when the program is called with --help?

Marcoux answered 6/9, 2016 at 23:56 Comment(0)
M
19

Pass show_default=True in the click.option decorator when defining an option. This will display default value in the help when the program is called with --help option. For example -

#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()

Now you can see the help screen generated by running 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.

Hence you can see that the default value of the count option is displayed in the help text of the program. (Reference : https://github.com/pallets/click/issues/243)

Mosira answered 7/9, 2016 at 0:34 Comment(5)
Surely I'm not the only one who finds it curious that show_default is False by default. I can't think of a well-known CLI tool that doesn't show default values in the help text.Menace
What is the purpose of a default value? I'm asking this because I tried to put a default value in case no value is passed to the option, but it is required to pass something. @click.option('-s', default='do-oldest')Manchester
@SérgioMafra were you able to find how to pass default value if nothing is given??Condone
@Menace I agree. It shouldn't be too hard to change the default behavior of this feature. I've tried to trigger a related discussion on GitHub.Al
@Manchester you can explicitly pass whether it should be required with 'required=False'Wendt

© 2022 - 2024 — McMap. All rights reserved.