PromptUtils

class consolemenu.prompt_utils.PromptUtils(screen, prompt_formatter=None)[source]

Utility class with various routines for prompting for user input.

Creates a new instance of ConsoleUtils with the specified console. If no console was specified, creates a new default console using the ConsoleFactory.

Parameters:
clear()[source]

Clear the screen.

confirm_answer(answer, message=None)[source]

Prompts the user to confirm a question with a yes/no prompt. If no message is specified, the default message is: “You entered {}. Is this correct?”

Parameters:
  • answer (str) – The answer to confirm.
  • message (str, optional) – Optional message if a different confirmation prompt is desired.
Returns:

True if the user confirmed Yes, or False if user specified No.

Return type:

bool

enter_to_continue(message=None)[source]

A console prompt to ask the user to ‘Press [Enter] to continue’.

Parameters:message (str, optional) – A message to display in place of the default.
input(prompt=None, default=None, validators=None, enable_quit=False, quit_string='q', quit_message='(enter q to Quit)')[source]

Generic prompt the user for input.

Parameters:
  • prompt (str) – The message to prompt the user.
  • default (str, optional) – The default value to suggest as an answer.
  • validators (BaseValidator, optional) – The list of validators to perform input validation.
  • enable_quit (bool, optional) – Specifies whether the user can cancel out of the input prompt.
  • quit_string (str, optional) – The string which the user must input in order to quit.
  • quit_message (str, optional) – The message to explain how to quit.
Returns:

an InputResult tuple.

Return type:

InputResult

input_password(message=None)[source]

Prompt the user for a password or other confidential data.

This is equivalent to the input() method, but does not echo inputted characters to the screen.

Parameters:message (str) – The prompt message.
Returns:The password provided by the user.
Return type:str
printf(*args)[source]

Prints the specified arguments to the screen.

Parameters:*args – Variable length argument list.
println(*args)[source]

Prints the specified arguments to the screen, followed by a newline character.

Parameters:*args – Variable length argument list.
prompt_and_confirm_password(message)[source]

Prompt for a password using the given message, then prompt a second time for a confirmation password, and verify both provided passwords match. If the passwords do not match, an error is displayed, “Passwords do not match”, and the user must input both passwords again.

Parameters:message (str) – The prompt message.
Returns:The password.
Return type:str
prompt_for_bilateral_choice(prompt, option1, option2)[source]

Prompt the user for a response that must be one of the two supplied choices.

NOTE: The user input verification is case-insensitive, but will return the original case provided by the given options.

Parameters:
  • prompt (str) – The prompt to present the choices to the user.
  • option1 (str) – The first option.
  • option2 (str) – The second option.
Returns:

The choice selected by the user.

Return type:

str

prompt_for_numbered_choice(choices, title=None, prompt='>')[source]

Displays a numbered vertical list of choices from the provided list of strings.

Parameters:
  • choices (list of str) – The list of choices to display.
  • title (str, optional) – Optional title to display above the numbered list.
  • prompt (str) – The prompt string. Default is “>”.
Returns:

The index of selected choice.

Return type:

int

prompt_for_trilateral_choice(prompt, option1, option2, option3)[source]

Prompt the user for a response that must be one of the three supplied choices.

NOTE: The user input verification is case-insensitive, but will return the original case provided by the given options.

Parameters:
  • prompt (str) – The prompt to present the choices to the user.
  • option1 (str) – The first option.
  • option2 (str) – The second option.
  • option3 (str) – The third option.
Returns:

The choice selected by the user.

Return type:

str

prompt_for_yes_or_no(prompt)[source]

Prompts the user with the specified question, and expects a yes (y) or no (n) response, returning a boolean value representing the user’s answer.

Parameters:prompt (str) – The prompt to display to the user.
Returns:True for yes, False for no.
Return type:bool
screen

The Screen instance.

Type:consolemenu.screen.Screen
validate_input(input_string, validators)[source]

Validate the given input string against the specified list of validators.

Parameters:
  • input_string (str) – The input string to verify.
  • validators (list of BaseValidator) – The list of validators.
Returns:

The validation result. True if the input is valid; False otherwise.

Return type:

bool

Raises:

InvalidValidator – If the list of validators contains an invalid BaseValidator class.