Move Prompts doc from reST to MyST
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent d54bbc1 · Browse files · 1 file·+79 added−65 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+79 added−65 removed
- other:+79 added−65 removed
Provenance
- Commit
e2cffec- Tree
673e63e- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
d54bbc1
1 file changed+79 added−65 removed
modified:docs/prompts.md+79 added-65 removed
121243456786781011129101114151612131415161718181920202121222324232425262728262728293031323334355 unmodified lines41424344454647484950515253544 unmodified lines5960616263646263656667656667686970697071727273747374757677787980817879828384858687888990838485869192939495969798991004 unmodified lines1051061071081091101021031111121131051061141151081091101161171181191201211221231241251151161261271281291301311321331341201211351361231241251261371381391401281291301311411421431441331451461471351481491501511521531541551561371381391401411421431441571581591601611621638 unmodified lines172173174175176User Input Prompts==================# User Input Prompts
.. currentmodule:: click```{currentmodule} click```
Click supports prompts in two different places. The first is automatedprompts when the parameter handling happens, and the second is to ask forprompts at a later point independently.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 forvalid input according to a type, or the :func:`confirm` function, which asksfor confirmation (yes/no).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: 2 :local:```{contents}---depth: 2local: true---```
.. _option-prompting:(option-prompting)=
Option Prompts--------------## Option Prompts
Option prompts are integrated into the option interface. Internally, itautomatically calls either :func:`prompt` or :func:`confirm` as necessary.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 input instead. This can be implemented withClick by defining a prompt string.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)5 unmodified lines.. 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')4 unmodified lines
.. click:run::
invoke(hello, input=['John'])```
It is advised that prompt not be used in conjunction with the multipleflag set to True. Instead, prompt in the function interactively.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 passedthrough the command line. To turn this behavior off, see:ref:`optional-value`.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-------------## Input Prompts
To manually ask for user input, you can use the :func:`prompt` function.By default, it accepts any Unicode string, but you can ask for any othertype. For instance, you can ask for a valid integer::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:
```{eval-rst}.. click:example::
value = click.prompt('Please enter a valid integer', type=int)```
Additionally, the type will be determined automatically if a default value isprovided. For instance, the following will only accept floats::Additionally, the type will be determined automatically if a default value is provided. For instance, the following willonly accept floats:
```{eval-rst}.. click:example::
value = 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 the## 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")4 unmodified lines
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 notgiven, but it will also prompt if only the flag is given.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--------------------## Confirmation Prompts
To ask if a user wants to continue with an action, the :func:`confirm`function comes in handy. By default, it returns the result of the promptas a boolean value::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:
```{eval-rst}.. click:example::
if click.confirm('Do you want to continue?'): click.echo('Well done!')```
There is also the option to make the function automatically abort theexecution of the program if it does not return ``True``::There is also the option to make the function automatically abort the execution of the program if it does not return`True`:
```{eval-rst}.. click:example::
click.confirm('Do you want to continue?', abort=True)```
Dynamic Defaults for Prompts----------------------------## Dynamic Defaults for Prompts
The ``auto_envvar_prefix`` and ``default_map`` options for the contextallow the program to read option values from the environment or aconfiguration file. However, this overrides the prompting mechanism, sothat the user does not get the option to change the value interactively.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 beprompted if the option isn't specified on the command line, you can do soby supplying a callable as the default value. For example, to get a defaultfrom the environment: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:
.. code-block:: python```pythonimport os
import os@click.command()@click.option( "--username", prompt=True, default=lambda: os.environ.get("USER", ""))def hello(username): click.echo(f"Hello, {username}!")```
@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
8 unmodified lines
.. click:run::
invoke(hello, args=["--help"])```No newline at end of file