Skip to content
Myr

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

2 files+162 added−162 removed

added:docs/prompts.md+162 added
1User Input Prompts2==================34.. currentmodule:: click56Click supports prompts in two different places. The first is automated7prompts when the parameter handling happens, and the second is to ask for8prompts at a later point independently.910This can be accomplished with the :func:`prompt` function, which asks for11valid input according to a type, or the :func:`confirm` function, which asks12for confirmation (yes/no).1314.. contents::15 :depth: 216 :local:1718.. _option-prompting:1920Option Prompts21--------------2223Option prompts are integrated into the option interface. Internally, it24automatically calls either :func:`prompt` or :func:`confirm` as necessary.2526In some cases, you want parameters that can be provided from the command line,27but if not provided, ask for user input instead. This can be implemented with28Click by defining a prompt string.2930Example:3132.. click:example::3334 @click.command()35 @click.option('--name', prompt=True)36 def hello(name):37 click.echo(f"Hello {name}!")3839And what it looks like:4041.. click:run::4243 invoke(hello, args=['--name=John'])44 invoke(hello, input=['John'])4546If you are not happy with the default prompt string, you can ask for47a different one:4849.. click:example::5051 @click.command()52 @click.option('--name', prompt='Your name please')53 def hello(name):54 click.echo(f"Hello {name}!")5556What it looks like:5758.. click:run::5960 invoke(hello, input=['John'])6162It is advised that prompt not be used in conjunction with the multiple63flag set to True. Instead, prompt in the function interactively.6465By default, the user will be prompted for an input if one was not passed66through the command line. To turn this behavior off, see67:ref:`optional-value`.6869Input Prompts70-------------7172To manually ask for user input, you can use the :func:`prompt` function.73By default, it accepts any Unicode string, but you can ask for any other74type. For instance, you can ask for a valid integer::7576 value = click.prompt('Please enter a valid integer', type=int)7778Additionally, the type will be determined automatically if a default value is79provided. For instance, the following will only accept floats::8081 value = click.prompt('Please enter a number', default=42.0)8283Optional Prompts84------------------85If the option has ``prompt`` enabled, then setting86``prompt_required=False`` tells Click to only show the prompt if the87option's flag is given, instead of if the option is not provided at all.8889.. click:example::9091 @click.command()92 @click.option('--name', prompt=True, prompt_required=False, default="Default")93 def hello(name):94 click.echo(f"Hello {name}!")9596.. click:run::9798 invoke(hello)99 invoke(hello, args=["--name", "Value"])100 invoke(hello, args=["--name"], input="Prompt")101102If ``required=True``, then the option will still prompt if it is not103given, but it will also prompt if only the flag is given.104105Confirmation Prompts106--------------------107108To ask if a user wants to continue with an action, the :func:`confirm`109function comes in handy. By default, it returns the result of the prompt110as a boolean value::111112 if click.confirm('Do you want to continue?'):113 click.echo('Well done!')114115There is also the option to make the function automatically abort the116execution of the program if it does not return ``True``::117118 click.confirm('Do you want to continue?', abort=True)119120Dynamic Defaults for Prompts121----------------------------122123The ``auto_envvar_prefix`` and ``default_map`` options for the context124allow the program to read option values from the environment or a125configuration file. However, this overrides the prompting mechanism, so126that the user does not get the option to change the value interactively.127128If you want to let the user configure the default value, but still be129prompted if the option isn't specified on the command line, you can do so130by supplying a callable as the default value. For example, to get a default131from the environment:132133.. code-block:: python134135 import os136137 @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}!")144145To describe what the default value will be, set it in ``show_default``.146147.. click:example::148149 import os150151 @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}!")159160.. click:run::161162 invoke(hello, args=["--help"])
deleted:docs/prompts.rst-162 removed
1User Input Prompts2==================34.. currentmodule:: click56Click supports prompts in two different places. The first is automated7prompts when the parameter handling happens, and the second is to ask for8prompts at a later point independently.910This can be accomplished with the :func:`prompt` function, which asks for11valid input according to a type, or the :func:`confirm` function, which asks12for confirmation (yes/no).1314.. contents::15 :depth: 216 :local:1718.. _option-prompting:1920Option Prompts21--------------2223Option prompts are integrated into the option interface. Internally, it24automatically calls either :func:`prompt` or :func:`confirm` as necessary.2526In some cases, you want parameters that can be provided from the command line,27but if not provided, ask for user input instead. This can be implemented with28Click by defining a prompt string.2930Example:3132.. click:example::3334 @click.command()35 @click.option('--name', prompt=True)36 def hello(name):37 click.echo(f"Hello {name}!")3839And what it looks like:4041.. click:run::4243 invoke(hello, args=['--name=John'])44 invoke(hello, input=['John'])4546If you are not happy with the default prompt string, you can ask for47a different one:4849.. click:example::5051 @click.command()52 @click.option('--name', prompt='Your name please')53 def hello(name):54 click.echo(f"Hello {name}!")5556What it looks like:5758.. click:run::5960 invoke(hello, input=['John'])6162It is advised that prompt not be used in conjunction with the multiple63flag set to True. Instead, prompt in the function interactively.6465By default, the user will be prompted for an input if one was not passed66through the command line. To turn this behavior off, see67:ref:`optional-value`.6869Input Prompts70-------------7172To manually ask for user input, you can use the :func:`prompt` function.73By default, it accepts any Unicode string, but you can ask for any other74type. For instance, you can ask for a valid integer::7576 value = click.prompt('Please enter a valid integer', type=int)7778Additionally, the type will be determined automatically if a default value is79provided. For instance, the following will only accept floats::8081 value = click.prompt('Please enter a number', default=42.0)8283Optional Prompts84------------------85If the option has ``prompt`` enabled, then setting86``prompt_required=False`` tells Click to only show the prompt if the87option's flag is given, instead of if the option is not provided at all.8889.. click:example::9091 @click.command()92 @click.option('--name', prompt=True, prompt_required=False, default="Default")93 def hello(name):94 click.echo(f"Hello {name}!")9596.. click:run::9798 invoke(hello)99 invoke(hello, args=["--name", "Value"])100 invoke(hello, args=["--name"], input="Prompt")101102If ``required=True``, then the option will still prompt if it is not103given, but it will also prompt if only the flag is given.104105Confirmation Prompts106--------------------107108To ask if a user wants to continue with an action, the :func:`confirm`109function comes in handy. By default, it returns the result of the prompt110as a boolean value::111112 if click.confirm('Do you want to continue?'):113 click.echo('Well done!')114115There is also the option to make the function automatically abort the116execution of the program if it does not return ``True``::117118 click.confirm('Do you want to continue?', abort=True)119120Dynamic Defaults for Prompts121----------------------------122123The ``auto_envvar_prefix`` and ``default_map`` options for the context124allow the program to read option values from the environment or a125configuration file. However, this overrides the prompting mechanism, so126that the user does not get the option to change the value interactively.127128If you want to let the user configure the default value, but still be129prompted if the option isn't specified on the command line, you can do so130by supplying a callable as the default value. For example, to get a default131from the environment:132133.. code-block:: python134135 import os136137 @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}!")144145To describe what the default value will be, set it in ``show_default``.146147.. click:example::148149 import os150151 @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}!")159160.. click:run::161162 invoke(hello, args=["--help"])