secret

A text prompt which transforms the input to asterisks while typing.

Example

demo

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

original_password = "InquirerPy45@"


def main():
    questions = [
        {
            "type": "password",
            "message": "Old password:",
            "transformer": lambda _: "[hidden]",
            "validate": lambda text: text == original_password,
            "invalid_message": "Wrong password",
            "long_instruction": "Original password: InquirerPy45@",
        },
        {
            "type": "password",
            "message": "New password:",
            "name": "new_password",
            "validate": PasswordValidator(
                length=8, cap=True, special=True, number=True
            ),
            "transformer": lambda _: "[hidden]",
            "long_instruction": "Password require length of 8, 1 cap char, 1 special char and 1 number char.",
        },
        {"type": "confirm", "message": "Confirm?", "default": True},
    ]
    result = prompt(questions)


if __name__ == "__main__":
    main()
Alternate Syntax
from InquirerPy import inquirer
from InquirerPy.validator import PasswordValidator

original_password = "InquirerPy45@"


def main():
    old_password = inquirer.secret(
        message="Old password:",
        transformer=lambda _: "[hidden]",
        validate=lambda text: text == original_password,
        invalid_message="Wrong password",
        instruction="(abc)",
        long_instruction="Original password: InquirerPy45@",
    ).execute()
    new_password = inquirer.secret(
        message="New password:",
        validate=PasswordValidator(length=8, cap=True, special=True, number=True),
        transformer=lambda _: "[hidden]",
        long_instruction="Password require length of 8, 1 cap char, 1 special char and 1 number char.",
    ).execute()
    confirm = inquirer.confirm(message="Confirm?", default=True).execute()


if __name__ == "__main__":
    main()

Keybindings

See also

keybindings

{
    "answer": [{"key": "enter"}],   # answer the prompt
    "interrupt": [{"key": "c-c"}],  # raise KeyboardInterrupt
    "skip": [{"key": "c-z"}],   # skip the prompt
}

There are no additional keybindings created for this prompt besides the default keybindings and input buffer keybindings.

Password Validation

Reference

class InquirerPy.prompts.secret.SecretPrompt(message, style=None, default='', qmark='?', amark='?', instruction='', long_instruction='', vi_mode=False, validate=None, invalid_message='Invalid input', transformer=None, filter=None, keybindings=None, wrap_lines=True, raise_keyboard_interrupt=True, mandatory=True, mandatory_message='Mandatory prompt', session_result=None, input=None, output=None)[source]

Create a text prompt which transforms the input to asterisks while typing.

A wrapper class around PromptSession.

Parameters
  • message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message documentation for more details.

  • style (Optional[InquirerPy.utils.InquirerPyStyle]) – An InquirerPyStyle instance. Refer to Style documentation for more details.

  • vi_mode (bool) – Use vim keybinding for the prompt. Refer to keybindings documentation for more details.

  • default (Union[Any, Callable[[InquirerPySessionResult], Any]]) – Set the default text value of the prompt. Refer to default documentation for more details.

  • qmark (str) – Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.

  • amark (str) – Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.

  • instruction (str) – Short instruction to display next to the question.

  • long_instruction (str) – Long instructions to display at the bottom of the prompt.

  • validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. Refer to Validator documentation for more details.

  • invalid_message (str) – Error message to display when user input is invalid. Refer to Validator documentation for more details.

  • transformer (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the value that gets printed to the terminal. Different than filter parameter, this is only visual effect and won’t affect the actual value returned by execute(). Refer to transformer documentation for more details.

  • filter (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the result. This affects the actual value returned by execute(). Refer to filter documentation for more details.

  • keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.

  • wrap_lines (bool) – Soft wrap question lines when question exceeds the terminal width.

  • raise_keyboard_interrupt (bool) – Raise the KeyboardInterrupt exception when ctrl-c is pressed. If false, the result will be None and the question is skiped.

  • mandatory (bool) – Indicate if the prompt is mandatory. If True, then the question cannot be skipped.

  • mandatory_message (str) – Error message to show when user attempts to skip mandatory prompt.

  • session_result (Optional[Dict[Union[str, int], Optional[Union[str, bool, List[Any]]]]]) – Used internally for Classic Syntax (PyInquirer).

  • input (Optional[Input]) – Used internally and will be removed in future updates.

  • output (Optional[Output]) – Used internally and will be removed in future updates.

Return type

None

Examples

>>> from InquirerPy import inquirer
>>> result = inquirer.secret(message="Password:").execute()
>>> print(f"Password: {result}")
Password: asdf123