Python prompt Package

Prompt and verify user input on the command line.

The project was initiated by Stefan Fischer.

Note

This project is still a work in progress.

Documentation

Examples

  1. Ask for a floating point number:
    >>> prompt.real()
    ? 0.02
    0.02
    
  2. The prompt may be customized:
    >>> prompt.integer(prompt="[0-9]+ ")
    [0-9]+ 42
    42
    
  1. You can allow empty responses:
    >>> prompt.string(empty=True)
    ? <Enter>
    None
    
  2. Ask for a password:
    >>> promp.secret(prompt="Enter your password: ")
    Enter your password:
    '$uper$ecretP@ssw0rd'
    
  1. Invalid responses will be rejected:
    >>> prompt.email()
    ? Bob.Miller
    ? bob@miller.foo
    'bob@miller.foo'
    

API

Prompt and verify user input on the command line.

The project was initiated by Stefan Fischer.

Note

This project is still a work in progress.

prompt.PROMPT = '? '

Prompt that will be shown by default.

prompt.RE_EMAIL_SIMPLE = <_sre.SRE_Pattern object>

Regular expression for email addresses.

prompt.character(prompt=None, empty=False)[source]

Prompt a single character.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
Returns:

A str if the user entered a single-character, non-empty string. None if the user pressed only Enter and empty was True.

Return type:

str or None

prompt.email(prompt=None, empty=False, mode='simple')[source]

Prompt an email address.

This check is based on a simple regular expression and does not verify whether an email actually exists.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
  • mode ({'simple'}, optional) – ‘simple’ will use a simple regular expression. No other mode is implemented yet.
Returns:

A str if the user entered a likely email address. None if the user pressed only Enter and empty was True.

Return type:

str or None

prompt.integer(prompt=None, empty=False)[source]

Prompt an integer.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
Returns:

An int if the user entered a valid integer. None if the user pressed only Enter and empty was True.

Return type:

int or None

prompt.real(prompt=None, empty=False)[source]

Prompt a real number.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
Returns:

A float if the user entered a valid real number. None if the user pressed only Enter and empty was True.

Return type:

float or None

prompt.regex(pattern, prompt=None, empty=False, flags=0)[source]

Prompt a string that matches a regular expression.

Parameters:
  • pattern (str) – A regular expression that must be matched.
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
  • flags (int, optional) – Flags that will be passed to re.match.
Returns:

A match object if the user entered a matching string. None if the user pressed only Enter and empty was True.

Return type:

Match or None

See also

re.match()

prompt.secret(prompt=None, empty=False)[source]

Prompt a string without echoing.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
Returns:

A str if the user entered a non-empty string. None if the user pressed only Enter and empty was True.

Return type:

str or None

Raises:

getpass.GetPassWarning – If echo free input is unavailable.

See also

getpass.getpass()

prompt.string(prompt=None, empty=False)[source]

Prompt a string.

Parameters:
  • prompt (str, optional) – Use an alternative prompt.
  • empty (bool, optional) – Allow an empty response.
Returns:

A str if the user entered a non-empty string. None if the user pressed only Enter and empty was True.

Return type:

str or None