|
| 1 | +# User Input Prompts |
| 2 | + |
| 3 | +```{currentmodule} click |
| 4 | +``` |
| 5 | + |
| 6 | +Click supports prompts in two different places. The first is automated prompts when the parameter handling happens, and |
| 7 | +the second is to ask for prompts at a later point independently. |
| 8 | + |
| 9 | +This can be accomplished with the {func}`prompt` function, which asks for valid input according to a type, or the |
| 10 | +{func}`confirm` function, which asks for confirmation (yes/no). |
| 11 | + |
| 12 | +```{contents} |
| 13 | +--- |
| 14 | +depth: 2 |
| 15 | +local: true |
| 16 | +--- |
| 17 | +``` |
| 18 | + |
| 19 | +(option-prompting)= |
| 20 | + |
| 21 | +## Option Prompts |
| 22 | + |
| 23 | +Option prompts are integrated into the option interface. Internally, it automatically calls either {func}`prompt` or |
| 24 | +{func}`confirm` as necessary. |
| 25 | + |
| 26 | +In some cases, you want parameters that can be provided from the command line, but if not provided, ask for user input |
| 27 | +instead. This can be implemented with Click by defining a prompt string. |
| 28 | + |
| 29 | +Example: |
| 30 | + |
| 31 | +```{eval-rst} |
| 32 | +.. click:example:: |
| 33 | +
|
| 34 | + @click.command() |
| 35 | + @click.option('--name', prompt=True) |
| 36 | + def hello(name): |
| 37 | + click.echo(f"Hello {name}!") |
| 38 | +
|
| 39 | +And what it looks like: |
| 40 | +
|
| 41 | +.. click:run:: |
| 42 | +
|
| 43 | + invoke(hello, args=['--name=John']) |
| 44 | + invoke(hello, input=['John']) |
| 45 | +``` |
| 46 | + |
| 47 | +If you are not happy with the default prompt string, you can ask for |
| 48 | +a different one: |
| 49 | + |
| 50 | +```{eval-rst} |
| 51 | +.. click:example:: |
| 52 | +
|
| 53 | + @click.command() |
| 54 | + @click.option('--name', prompt='Your name please') |
| 55 | + def hello(name): |
| 56 | + click.echo(f"Hello {name}!") |
| 57 | +
|
| 58 | +What it looks like: |
| 59 | +
|
| 60 | +.. click:run:: |
| 61 | +
|
| 62 | + invoke(hello, input=['John']) |
| 63 | +``` |
| 64 | + |
| 65 | +It is advised that prompt not be used in conjunction with the multiple flag set to True. Instead, prompt in the function |
| 66 | +interactively. |
| 67 | + |
| 68 | +By default, the user will be prompted for an input if one was not passed through the command line. To turn this behavior |
| 69 | +off, see {ref}`optional-value`. |
| 70 | + |
| 71 | +## Input Prompts |
| 72 | + |
| 73 | +To manually ask for user input, you can use the {func}`prompt` function. By default, it accepts any Unicode string, but |
| 74 | +you can ask for any other type. For instance, you can ask for a valid integer: |
| 75 | + |
| 76 | +```python |
| 77 | +value = click.prompt('Please enter a valid integer', type=int) |
| 78 | +``` |
| 79 | + |
| 80 | +Additionally, the type will be determined automatically if a default value is provided. For instance, the following will |
| 81 | +only accept floats: |
| 82 | + |
| 83 | +```python |
| 84 | +value = click.prompt('Please enter a number', default=42.0) |
| 85 | +``` |
| 86 | + |
| 87 | +## Optional Prompts |
| 88 | + |
| 89 | +If the option has `prompt` enabled, then setting `prompt_required=False` tells Click to only show the prompt if the |
| 90 | +option's flag is given, instead of if the option is not provided at all. |
| 91 | + |
| 92 | +```{eval-rst} |
| 93 | +.. click:example:: |
| 94 | +
|
| 95 | + @click.command() |
| 96 | + @click.option('--name', prompt=True, prompt_required=False, default="Default") |
| 97 | + def hello(name): |
| 98 | + click.echo(f"Hello {name}!") |
| 99 | +
|
| 100 | +.. click:run:: |
| 101 | +
|
| 102 | + invoke(hello) |
| 103 | + invoke(hello, args=["--name", "Value"]) |
| 104 | + invoke(hello, args=["--name"], input="Prompt") |
| 105 | +``` |
| 106 | + |
| 107 | +If `required=True`, then the option will still prompt if it is not given, but it will also prompt if only the flag is |
| 108 | +given. |
| 109 | + |
| 110 | +## Confirmation Prompts |
| 111 | + |
| 112 | +To ask if a user wants to continue with an action, the {func}`confirm` function comes in handy. By default, it returns |
| 113 | +the result of the prompt as a boolean value: |
| 114 | + |
| 115 | +```python |
| 116 | +if click.confirm('Do you want to continue?'): |
| 117 | + click.echo('Well done!') |
| 118 | +``` |
| 119 | + |
| 120 | +There is also the option to make the function automatically abort the execution of the program if it does not return |
| 121 | +`True`: |
| 122 | + |
| 123 | +```python |
| 124 | +click.confirm('Do you want to continue?', abort=True) |
| 125 | +``` |
| 126 | + |
| 127 | +## Dynamic Defaults for Prompts |
| 128 | + |
| 129 | +The `auto_envvar_prefix` and `default_map` options for the context allow the program to read option values from the |
| 130 | +environment or a configuration file. However, this overrides the prompting mechanism, so that the user does not get the |
| 131 | +option to change the value interactively. |
| 132 | + |
| 133 | +If you want to let the user configure the default value, but still be prompted if the option isn't specified on the |
| 134 | +command line, you can do so by supplying a callable as the default value. For example, to get a default from the |
| 135 | +environment: |
| 136 | + |
| 137 | +```python |
| 138 | +import os |
| 139 | + |
| 140 | +@click.command() |
| 141 | +@click.option( |
| 142 | + "--username", prompt=True, |
| 143 | + default=lambda: os.environ.get("USER", "") |
| 144 | +) |
| 145 | +def hello(username): |
| 146 | + click.echo(f"Hello, {username}!") |
| 147 | +``` |
| 148 | + |
| 149 | +To describe what the default value will be, set it in ``show_default``. |
| 150 | + |
| 151 | +```{eval-rst} |
| 152 | +.. click:example:: |
| 153 | +
|
| 154 | + import os |
| 155 | +
|
| 156 | + @click.command() |
| 157 | + @click.option( |
| 158 | + "--username", prompt=True, |
| 159 | + default=lambda: os.environ.get("USER", ""), |
| 160 | + show_default="current user" |
| 161 | + ) |
| 162 | + def hello(username): |
| 163 | + click.echo(f"Hello, {username}!") |
| 164 | +
|
| 165 | +.. click:run:: |
| 166 | +
|
| 167 | + invoke(hello, args=["--help"]) |
| 168 | +``` |
0 commit comments