GetInput

The GetInput class is the heart of the cooked_input library. Calls to GetInput objects perform the cleaning, conversion and validation of input data.

Note

Using the GetInput class is for more advanced users, Beginners can just use the convenience functions.

GetInput:

class cooked_input.GetInput(cleaners=None, convertor=None, validators=None, **options)

Class to get cleaned, converted, validated input from the command line. This is the central class used for cooked_input.

Parameters:
  • cleaners (List[Cleaner]) – list of cleaners to apply to clean the value
  • convertor (Convertor) – the convertor to apply to the cleaned value
  • validators (List[Validator]) – list of validators to apply to validate the cleaned and converted value
  • options – see below

Options:

prompt: the string to use for the prompt. For example prompt=”Enter your name”

required: True if a non-blank value is required, False if a blank response is OK.

default: the default value to use if a blank string is entered. This takes precedence over required
(i.e. a blank response will return the default value.)

default_str: the string to use for the default value. In general just set the default option.

hidden: the input typed should not be displayed. This is useful for entering passwords.

retries: the maximum number of attempts to allow before raising a MaxRetriesError exception.

error_callback: a callback function to call when an error is encountered. Defaults to print_error()

convertor_error_fmt: format string to use for convertor errors. Defaults to
DEFAULT_CONVERTOR_ERROR. Format string receives two variables - {value} the value that failed conversion, and {error_content} set by the convertor.
validator_error_fmt: format string to use for validator errors. Defaults
to DEFAULT_VALIDATOR_ERROR. Format string receives two variables - {value} the value that failed conversion, and {error_content} set by the validator.

commands: an optional dictionary of commands. See below for more details.

Commands:

GetInput optionally takes a dictionary containing commands that can be run from the input prompt. The key for each command is the string used to call the command and the value is an instance of the GetInputCommand class for the command. For intance, the following dictionary sets two different command string (/? and /help) to call a function to show help information:

{
    "/?": GetInputCommand(show_help_action)),
    "/help": GetInputCommand(show_help_action)),
}

For more information see GetInputCommand

GetInput.get_input()

Get input from the command line and return a validated response.

Returns:the cleaned, converted, validated input
Return type:Any (dependent on the value returned from the convertors)

This method prompts the user for an input, and returns the cleaned, converted, and validated input.

GetInput.process_value(value)
Parameters:value (str) – the value to process
Returns:Return a ProcessValueResponse namedtuple (valid, converted_value)
Return type:NamedTuple[bool, Any]

Run a value through cleaning, conversion, and validation. This allows the same processing used in GetInput.get_input() to be performed on a value. For instance, the same processing used for getting keyboard input can be applied to the value from a gui or web form input.

The ProcessValueResponse namedtuple has elements valid and value. If the value was successfully cleaned, converted and validated, valid is True and value is the converted and cleaned value. If not, valid is False, and value is None.