rename extension from .rst to .md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent 4b695e5 · Browse files · 2 files·+162 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+162 added−162 removed
- other:+162 added−162 removed
Provenance
- Commit
d54bbc1- Tree
e243966- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
4b695e5
2 files changed+162 added−162 removed
added:docs/prompts.md+162 added
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"])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"])