Skip to main content

A powerful one line solution for collecting CLI user input with absolutely zero boilerplate.

Project description

PyMagicPrompt

A powerful one line solution for CLI user input with absolutely zero boilerplate.

Features

  • Full lifecycle abstraction, by default looping until valid input

  • Automatic terminal cleanup for subsequent prompts/answers

  • Type inference & casting for answer submissions

  • Customizable boolean conversion for common English words

  • Built in common validators

  • Support for custom validation functions

  • Fully customizable prompt formatting & colors

  • Customizable answer sanitization & formatting

  • Obscured text for password inputs

  • Options can be specified by both kwargs and options dict

By default, prompt() will work out of the box for most use cases with zero customization.


Example Usage

Basic prompt

name = prompt("What is your name")
# > What is your name: John
print(name)  # 'John'

Usage of built in validators

age = prompt("Enter your age", numOnly=True)
# > Enter your age: 25
print(age)  # 25 (int)

Custom validators

def validate_range(x): 
    return 1 <= int(x) <= 10

num = prompt("Enter number 1-10", validators=[validate_range])
# > Enter number 1-10: 5
print(num)  # 5 (int)

Common options for multiple inputs

base = {
    "alphaOnly": True,  # Only allow letters
    "capAnswer": True,  # Capitalize the answer
    "color": "green",  # Use green color for prompt
    "suffixStr": " >> ",  # Custom suffix
    "caratStr": "! ",  # Custom carat
}

firstName = prompt("First name", options=base)
# ! First Name >> john
print(f"First name: {firstName}")  # First name: John

lastName = prompt("Last name", options=base)
# ! Last Name >> smith
print(f"Last name: {lastName}")  # Last name: Smith

Custom boolean conversion

proceed = prompt("Continue?", trueStrs=["sure", "ok", "yes"])
# > Continue?: ok
print(proceed)  # True (bool)

Parameters

Parameter Type Default Description
prompt str required The prompt text to display to the user
options PromptOptions | None None Dictionary of options that can override default parameters
loop bool True Keep prompting until valid input is received
capPrompt bool True Capitalize the first letter of the prompt
titlePrompt bool True Convert prompt to title case
carat bool True Add a carat prefix to the prompt
caratStr str "> " String to use as carat
suffix bool True Add a suffix to the prompt
suffixStr str ": " String to use as suffix
capAnswer bool False Capitalize the first letter of string answers
titleAnswer bool False Convert string answers to title case
alphaOnly bool False Only accept alphabetic input
numOnly bool False Only accept numeric input
alphaNumOnly bool False Only accept alphanumeric input
floatOnly bool False Only accept floating-point numbers
notEmpty bool True Reject empty input
castAnswer bool True Attempt to cast the answer to an appropriate type
color str "" Color name for the prompt (black, red, green, yellow, blue, magenta, cyan, white)
isPassword bool False Mask input characters
clearAfterResponse bool False Clear the prompt after receiving valid input
trueStrs tuple[str, ...] ("true", "yes", "y") Strings to interpret as True
falseStrs tuple[str, ...] ("false", "no", "n") Strings to interpret as False
pwFailMsg str "Invalid password" Message to display on invalid password input
validators list [] List of custom validation functions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magicprompt-0.2.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

magicprompt-0.2.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file magicprompt-0.2.2.tar.gz.

File metadata

  • Download URL: magicprompt-0.2.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for magicprompt-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ca628756e99e6beb415bbc3e1af507cdfe3eeeb75590f58990ee413d2a1eed2d
MD5 c65be2f9a002045e71a5fb6a5c8e3a91
BLAKE2b-256 54e84224c80ebd77f5d8050e6f890efb38369b44fee7c31e4a651f4eb721cbb0

See more details on using hashes here.

File details

Details for the file magicprompt-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: magicprompt-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for magicprompt-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 50aee4144fa003746e9bd5ad2a4e1fe1465e5402ea97a95532fd92cd7b71bf20
MD5 ffd8f406e6cb46ee9cb29067ebb52708
BLAKE2b-256 e9c1efcac46246fdd8c3da419e903dadfe74bb4f8af4f8e3b63e8bd6d2249619

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page