Move Prompts doc from reST to MyST (#3034)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 701b313, bb11331 · Browse files · 2 files·+168 added−162 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+168 added−162 removed
- other:+168 added−162 removed
Provenance
- Commit
5b0b7b4- Tree
f474861- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
701b313- Parent 2
bb11331
2 files changed+168 added−162 removed
added:docs/prompts.md+168 added
1+# User Input Prompts2+3+```{currentmodule} click4+```5+6+Click supports prompts in two different places. The first is automated prompts when the parameter handling happens, and7+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 the10+{func}`confirm` function, which asks for confirmation (yes/no).11+12+```{contents}13+---14+depth: 215+local: true16+---17+```18+19+(option-prompting)=20+21+## Option Prompts22+23+Option prompts are integrated into the option interface. Internally, it automatically calls either {func}`prompt` or24+{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 input27+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 for48+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 function66+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 behavior69+off, see {ref}`optional-value`.70+71+## Input Prompts72+73+To manually ask for user input, you can use the {func}`prompt` function. By default, it accepts any Unicode string, but74+you can ask for any other type. For instance, you can ask for a valid integer:75+76+```python77+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 will81+only accept floats:82+83+```python84+value = click.prompt('Please enter a number', default=42.0)85+```86+87+## Optional Prompts88+89+If the option has `prompt` enabled, then setting `prompt_required=False` tells Click to only show the prompt if the90+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 is108+given.109+110+## Confirmation Prompts111+112+To ask if a user wants to continue with an action, the {func}`confirm` function comes in handy. By default, it returns113+the result of the prompt as a boolean value:114+115+```python116+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 return121+`True`:122+123+```python124+click.confirm('Do you want to continue?', abort=True)125+```126+127+## Dynamic Defaults for Prompts128+129+The `auto_envvar_prefix` and `default_map` options for the context allow the program to read option values from the130+environment or a configuration file. However, this overrides the prompting mechanism, so that the user does not get the131+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 the134+command line, you can do so by supplying a callable as the default value. For example, to get a default from the135+environment:136+137+```python138+import os139+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 os155+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+```deleted:docs/prompts.rst-162 removed
1-User Input Prompts2-==================3-4-.. currentmodule:: click5-6-Click supports prompts in two different places. The first is automated7-prompts when the parameter handling happens, and the second is to ask for8-prompts at a later point independently.9-10-This can be accomplished with the :func:`prompt` function, which asks for11-valid input according to a type, or the :func:`confirm` function, which asks12-for confirmation (yes/no).13-14-.. contents::15- :depth: 216- :local:17-18-.. _option-prompting:19-20-Option Prompts21---------------22-23-Option prompts are integrated into the option interface. Internally, it24-automatically calls either :func:`prompt` or :func:`confirm` as necessary.25-26-In some cases, you want parameters that can be provided from the command line,27-but if not provided, ask for user input instead. This can be implemented with28-Click by defining a prompt string.29-30-Example:31-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-If you are not happy with the default prompt string, you can ask for47-a different one:48-49-.. click:example::50-51- @click.command()52- @click.option('--name', prompt='Your name please')53- def hello(name):54- click.echo(f"Hello {name}!")55-56-What it looks like:57-58-.. click:run::59-60- invoke(hello, input=['John'])61-62-It is advised that prompt not be used in conjunction with the multiple63-flag set to True. Instead, prompt in the function interactively.64-65-By default, the user will be prompted for an input if one was not passed66-through the command line. To turn this behavior off, see67-:ref:`optional-value`.68-69-Input Prompts70--------------71-72-To manually ask for user input, you can use the :func:`prompt` function.73-By default, it accepts any Unicode string, but you can ask for any other74-type. For instance, you can ask for a valid integer::75-76- value = click.prompt('Please enter a valid integer', type=int)77-78-Additionally, the type will be determined automatically if a default value is79-provided. For instance, the following will only accept floats::80-81- value = click.prompt('Please enter a number', default=42.0)82-83-Optional Prompts84-------------------85-If the option has ``prompt`` enabled, then setting86-``prompt_required=False`` tells Click to only show the prompt if the87-option's flag is given, instead of if the option is not provided at all.88-89-.. click:example::90-91- @click.command()92- @click.option('--name', prompt=True, prompt_required=False, default="Default")93- def hello(name):94- click.echo(f"Hello {name}!")95-96-.. click:run::97-98- invoke(hello)99- invoke(hello, args=["--name", "Value"])100- invoke(hello, args=["--name"], input="Prompt")101-102-If ``required=True``, then the option will still prompt if it is not103-given, but it will also prompt if only the flag is given.104-105-Confirmation Prompts106---------------------107-108-To ask if a user wants to continue with an action, the :func:`confirm`109-function comes in handy. By default, it returns the result of the prompt110-as a boolean value::111-112- if click.confirm('Do you want to continue?'):113- click.echo('Well done!')114-115-There is also the option to make the function automatically abort the116-execution of the program if it does not return ``True``::117-118- click.confirm('Do you want to continue?', abort=True)119-120-Dynamic Defaults for Prompts121-----------------------------122-123-The ``auto_envvar_prefix`` and ``default_map`` options for the context124-allow the program to read option values from the environment or a125-configuration file. However, this overrides the prompting mechanism, so126-that the user does not get the option to change the value interactively.127-128-If you want to let the user configure the default value, but still be129-prompted if the option isn't specified on the command line, you can do so130-by supplying a callable as the default value. For example, to get a default131-from the environment:132-133-.. code-block:: python134-135- import os136-137- @click.command()138- @click.option(139- "--username", prompt=True,140- default=lambda: os.environ.get("USER", "")141- )142- def hello(username):143- click.echo(f"Hello, {username}!")144-145-To describe what the default value will be, set it in ``show_default``.146-147-.. click:example::148-149- import os150-151- @click.command()152- @click.option(153- "--username", prompt=True,154- default=lambda: os.environ.get("USER", ""),155- show_default="current user"156- )157- def hello(username):158- click.echo(f"Hello, {username}!")159-160-.. click:run::161-162- invoke(hello, args=["--help"])