Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 284
Manu Avatar asked Sep 06 '16 23:09

Manu


People also ask

How to get default value of dropdown in JavaScript?

var selectbox_defaultOption = document. querySelector("#some_dropdown option[selected]");

How to pass default value in HTML?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option.

What is Click option in Python?

Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It's the “Command Line Interface Creation Kit”. It's highly configurable but comes with sensible defaults out of the box.


1 Answers

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)

like image 56
Abhishek Kedia Avatar answered Nov 04 '22 07:11

Abhishek Kedia