Skip to content

fourtreestech/promptpy

Repository files navigation

promptpy

A Python command-line input and validation library.

version Python Version from PEP 621 TOML coverage

promptpy prompts for and validates a range of different data types on the command line. Out of the box it supports limited character sets, integers, floats, dates, yes/no, lists and single-character commands:

from promptpy import Prompt

prompt = Prompt()
choice = prompt.integer("Pick a number", min=1, max=10, default=7)
# Prompt is displayed as:
# Pick a number (1-10) [7]:

The user will be reprompted with context-specific error messages until a valid integer is entered.

It is easy to add other types of prompt and validation by creating custom validators.

For more detail see the documentation.

Installation

(.venv) $ pip install promptpy

Examples

Options

from promptpy import Prompt

prompt = Prompt()
options = {
    's': 'to solve',
    'p': 'to play',
    'q': 'to quit',
}
choice = prompt.options(options, default='s')
# Prompt is displayed as:
# Enter S to solve, P to play, Q to quit [S]:

Yes/No

from promptpy import Prompt

prompt = Prompt()
choice = prompt.yes_no("Again", default="y")
# Prompt is displayed as:
# Again (Y/N)? [Y]:

Date

import datetime
from promptpy import Prompt

prompt = Prompt()
date = prompt.date('Enter a date (dd/mm/yyyy)', '%d/%m/%Y', default=datetime.date(2024, 1, 1))
# Prompt is displayed as:
# Enter a date (dd/mm/yyyy) [01/01/2024]:

See the Usage section of the documentation for a full list of methods, or the prompt module for details of methods and parameters.

About

A Python command line input and validation library

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors