Skip to content
Myr

prompts.rst

This file

Path

docs/prompts.rst

48 lines · 1.6 KiB

Last changed by

Not known.

History of this file

Take it

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).1314Option Prompts15--------------1617Option prompts are integrated into the option interface.  See18:ref:`option-prompting` for more information.  Internally, it19automatically calls either :func:`prompt` or :func:`confirm` as necessary.2021Input Prompts22-------------2324To manually ask for user input, you can use the :func:`prompt` function.25By default, it accepts any Unicode string, but you can ask for any other26type.  For instance, you can ask for a valid integer::2728    value = click.prompt('Please enter a valid integer', type=int)2930Additionally, the type will be determined automatically if a default value is31provided.  For instance, the following will only accept floats::3233    value = click.prompt('Please enter a number', default=42.0)3435Confirmation Prompts36--------------------3738To ask if a user wants to continue with an action, the :func:`confirm`39function comes in handy.  By default, it returns the result of the prompt40as a boolean value::4142    if click.confirm('Do you want to continue?'):43        click.echo('Well done!')4445There is also the option to make the function automatically abort the46execution of the program if it does not return ``True``::4748    click.confirm('Do you want to continue?', abort=True)