prompts.md
This file
Path
docs/prompts.md
168 lines · 4.5 KiB · Markdown
Last changed by
Move Prompts doc from reST to MyST (#3034)Rowlando13before adoption5b0b7b4
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169# User Input Prompts
```{currentmodule} click```
Click supports prompts in two different places. The first is automated prompts when the parameter handling happens, andthe second is to ask for prompts at a later point independently.
This can be accomplished with the {func}`prompt` function, which asks for valid input according to a type, or the{func}`confirm` function, which asks for confirmation (yes/no).
```{contents}---depth: 2local: true---```
(option-prompting)=
## Option Prompts
Option prompts are integrated into the option interface. Internally, it automatically calls either {func}`prompt` or{func}`confirm` as necessary.
In some cases, you want parameters that can be provided from the command line, but if not provided, ask for user inputinstead. This can be implemented with Click by defining a prompt string.
Example:
```{eval-rst}.. click:example::
@click.command() @click.option('--name', prompt=True) def hello(name): click.echo(f"Hello {name}!")
And what it looks like:
.. click:run::
invoke(hello, args=['--name=John']) invoke(hello, input=['John'])```
If you are not happy with the default prompt string, you can ask fora different one:
```{eval-rst}.. click:example::
@click.command() @click.option('--name', prompt='Your name please') def hello(name): click.echo(f"Hello {name}!")
What it looks like:
.. click:run::
invoke(hello, input=['John'])```
It is advised that prompt not be used in conjunction with the multiple flag set to True. Instead, prompt in the functioninteractively.
By default, the user will be prompted for an input if one was not passed through the command line. To turn this behavioroff, see {ref}`optional-value`.
## Input Prompts
To manually ask for user input, you can use the {func}`prompt` function. By default, it accepts any Unicode string, butyou can ask for any other type. For instance, you can ask for a valid integer:
```pythonvalue = click.prompt('Please enter a valid integer', type=int)```
Additionally, the type will be determined automatically if a default value is provided. For instance, the following willonly accept floats:
```pythonvalue = click.prompt('Please enter a number', default=42.0)```
## Optional Prompts
If the option has `prompt` enabled, then setting `prompt_required=False` tells Click to only show the prompt if theoption's flag is given, instead of if the option is not provided at all.
```{eval-rst}.. click:example::
@click.command() @click.option('--name', prompt=True, prompt_required=False, default="Default") def hello(name): click.echo(f"Hello {name}!")
.. click:run::
invoke(hello) invoke(hello, args=["--name", "Value"]) invoke(hello, args=["--name"], input="Prompt")```
If `required=True`, then the option will still prompt if it is not given, but it will also prompt if only the flag isgiven.
## Confirmation Prompts
To ask if a user wants to continue with an action, the {func}`confirm` function comes in handy. By default, it returnsthe result of the prompt as a boolean value:
```pythonif click.confirm('Do you want to continue?'): click.echo('Well done!')```
There is also the option to make the function automatically abort the execution of the program if it does not return`True`:
```pythonclick.confirm('Do you want to continue?', abort=True)```
## Dynamic Defaults for Prompts
The `auto_envvar_prefix` and `default_map` options for the context allow the program to read option values from theenvironment or a configuration file. However, this overrides the prompting mechanism, so that the user does not get theoption to change the value interactively.
If you want to let the user configure the default value, but still be prompted if the option isn't specified on thecommand line, you can do so by supplying a callable as the default value. For example, to get a default from theenvironment:
```pythonimport os
@click.command()@click.option( "--username", prompt=True, default=lambda: os.environ.get("USER", ""))def hello(username): click.echo(f"Hello, {username}!")```
To describe what the default value will be, set it in ``show_default``.
```{eval-rst}.. click:example::
import os
@click.command() @click.option( "--username", prompt=True, default=lambda: os.environ.get("USER", ""), show_default="current user" ) def hello(username): click.echo(f"Hello, {username}!")
.. click:run::
invoke(hello, args=["--help"])```