Skip to content

SecretPrompt

Kevin Zhuang edited this page Oct 5, 2021 · 15 revisions

This page is deprecated, documentation moved to: https://inquirerpy.readthedocs.io/

An input prompt to ask user for some secret values such as password.

class SecretPrompt(InputPrompt):
    def __init__(
        self,
        message: Union[str, Callable[[SessionResult], str]],
        style: InquirerPyStyle = None,
        default: Union[str, Callable[[SessionResult], str]] = "",
        qmark: str = "?",
        vi_mode: bool = False,
        validate: Union[Validator, Callable[[str], bool]] = None,
        invalid_message: str = "Invalid input",
        transformer: Callable[[str], Any] = None,
        filter: Callable[[str], Any] = None,
        **kwargs
    ) -> None:

Example

demo

Classic Syntax (PyInquirer)
from InquirerPy import prompt
from InquirerPy.validator import PasswordValidator

original_password = "InquirerPy45@"

questions = [
    {
        "type": "password",
        "message": "Old password:",
        "transformer": lambda _: "[hidden]",
        "validate": lambda text: text == original_password,
        "invalid_message": "Wrong password",
    },
    {
        "type": "password",
        "message": "New password:",
        "name": "new_password",
        "validate": PasswordValidator(
            length=8, cap=True, special=True, number=True
        ),
        "transformer": lambda _: "[hidden]",
    },
    {"type": "confirm", "message": "Confirm?", "default": True},
]
result = prompt(questions)
Alternate Syntax
from InquirerPy import inquirer
from InquirerPy.validator import PasswordValidator

original_password = "InquirerPy45@"

old_password = inquirer.secret(
    message="Old password:",
    transformer=lambda _: "[hidden]",
    validate=lambda text: text == original_password,
    invalid_message="Wrong password",
).execute()
new_password = inquirer.secret(
    message="New password:",
    validate=PasswordValidator(length=8, cap=True, special=True, number=True),
    transformer=lambda _: "[hidden]",
).execute()
confirm = inquirer.confirm(message="Confirm?", default=True).execute()

Parameters

message: Union[Callable[[SessionResult], str], str]

REQUIRED

The question message to display/ask the user.

When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer), put a dummy parameter (_) in your function.

from InquirerPy import inquirer

def get_message(_) -> str:
    message = "Name:"
    # logic ...
    return message

result = inquirer.secret(message=get_message)

style: InquirerPy

An InquirerPyStyle instance. Use get_style to retrieve an instance, reference Style documentation for more information.

If you are suing classic syntax (i.e. style), there's no need to provide this value since prompt already sets the style, unless you would like to apply different style for different question.

vi_mode: bool

Enable vim keybindings for the secret prompt. Its exactly the same as if you enable vi_mode in bash/readline. Checkout Keybindings documentation for more information.

If you are suing classic syntax (i.e. prompt), there's no need to provide this value since prompt already sets sets this value, unless you would like to apply vi_mode to specific questions.

default: Union[Callable[[SessionResult], str], str]

Set the default value of the secret prompt.

When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer), put a dummy parameter (_) in your function.

qmark: str

The question mark symbol to display in front of the question.

validate: Union[Callable[[str], bool], Validator]

Provide the validator for this question. Checkout Validator documentation for full details.

invalid_message: str

Configure the error message to display to the user when input does not meet compliance.

If the validate parameter is a Validator instance, then skip this parameter.

transformer: Callable[[str], Any]

A callable to transform the result. This is visual effect only, meaning it doesn't affect the returned result, it only changes the result displayed in the prompt.

Note: filter function won't affect the answer passed into transformer, transformer actually run before the filter function.

result = inquirer.secret(message="Password:", default="InquirerPy")  # UI -> ? Password: **********
result = inquirer.secret(
    message="Password:",
    default="InquirerPy",
    transformer=lambda _: "[hidden]",
)  # UI -> ? Password: [hidden]

filter: Callable[[str], Any]

A callable to filter the result. Different than the transformer, this affects the actual returned result but doesn't affect the visible prompt content.

result = inquirer.secret(message="Password:", default="InquirerPy")  # result = "InquirerPy"
result = inquirer.secret(
    message="Password:",
    default="Kevin Zhuang",
    filter=lambda result: result*2
)  # result = "InquirerPyInquirerPy"