cli-input-validator
Small, reusable helpers for validating interactive command-line input in Python. The package repeatedly prompts until the user enters a valid value, while letting you control the validation rules, error messages, and input function.
Installation
pip install cli-input-validator
Quick start
Prompt until a user enters one of the allowed choices:
from cli_input_validator import get_valid_choice
answer = get_valid_choice(["yes", "no"], "Continue? ")
get_valid_choice() is case-insensitive by default, so YES, Yes, and
yes are all accepted. It returns the value exactly as the user entered it.
Custom validators
A validator receives the user's input and returns a tuple containing:
- A Boolean indicating whether the input is valid.
- An error message to use as the next prompt when the input is invalid.
Use the exported VALID result for successful validation:
from cli_input_validator import VALID, get_validated_input
def positive_number(value):
if value.isdigit() and int(value) > 0:
return VALID
return False, "Enter a positive whole number: "
get_positive_number = get_validated_input(positive_number)
number = get_positive_number("Number: ")
If the user enters zero, the validator's error message becomes the next
prompt. Validation continues until the validator returns (True, None).
The accepted value is returned as a string.
Each validator controls its own error message, so different rules can provide specific guidance:
def username_validator(value):
if len(value) < 3:
return False, "Username must contain at least 3 characters: "
if not value.isalnum():
return False, "Username must contain only letters and numbers: "
return True, None
Custom input functions
By default, prompts are read with Python's built-in input(). You can pass any
callable that accepts a prompt and returns a string. For example, use
getpass.getpass when the entered value should not be displayed:
from getpass import getpass
from cli_input_validator import VALID, get_validated_input
def password_validator(value):
if len(value) >= 8:
return VALID
return False, "Password must contain at least 8 characters: "
password = get_validated_input(
password_validator,
input_function=getpass,
)("Password: ")
Third-party prompt functions work the same way. For example, after installing
maskpass, its masked askpass() function can be supplied directly:
from maskpass import askpass
from cli_input_validator import get_validated_input
password = get_validated_input(
password_validator,
input_function=askpass,
)("Password: ")
maskpass is optional and is not installed with cli-input-validator.
Valid choices
get_valid_choice() accepts any iterable of strings and generates its own
error prompt from the available choices:
from cli_input_validator import get_valid_choice
difficulty = get_valid_choice(
["easy", "medium", "hard"],
"Difficulty: ",
)
Matching is case-insensitive by default. Set case_sensitive=True when letter
case is significant:
confirmation = get_valid_choice(
["YES", "NO"],
"Type YES or NO: ",
case_sensitive=True,
)
A custom input function can also be supplied:
answer = get_valid_choice(
["yes", "no"],
"Continue? ",
input_function=my_prompt_function,
)
Passing an empty collection of choices raises ValueError.
Valid names
get_valid_name() prompts until the user enters a non-empty name containing
only letters, spaces, or hyphens:
from cli_input_validator import get_valid_name
name = get_valid_name("Name: ")
It also accepts a custom input function:
name = get_valid_name("Name: ", input_function=my_prompt_function)
Use name_validator() directly when you need to validate a value without
prompting:
from cli_input_validator import name_validator
valid, error = name_validator("Anne-Marie")
To use different name rules or a custom error message, create your own
validator and pass it to get_validated_input().
API overview
get_validated_input(is_valid=None, input_function=None)creates a reusable prompting function from a validator.get_valid_choice(choices, prompt="", case_sensitive=False, input_function=None)prompts for one of a collection of values.get_valid_name(prompt, input_function=None)prompts for a supported name.name_validator(name)validates a name without prompting.VALIDis the convenience result(True, None).
Development
python -m pip install -e ".[test]"
pytest
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cli_input_validator-0.1.1.tar.gz.
File metadata
- Download URL: cli_input_validator-0.1.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3cfc7cd70e4140b407c602ef8242fc0ad726014ad7cdea7952c85a065e11a6e
|
|
| MD5 |
1458cc8d7e5d19c5bbeb9e786d5b2678
|
|
| BLAKE2b-256 |
334c227704bcc197bc5322a0f54a7c9173679aa674ff4707744e5f4b09ba2718
|
Provenance
The following attestation bundles were made for cli_input_validator-0.1.1.tar.gz:
Publisher:
release.yml on VanPaitin/cli-input-validator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cli_input_validator-0.1.1.tar.gz -
Subject digest:
e3cfc7cd70e4140b407c602ef8242fc0ad726014ad7cdea7952c85a065e11a6e - Sigstore transparency entry: 2595903294
- Sigstore integration time:
-
Permalink:
VanPaitin/cli-input-validator@6e0912c9135004aa050123169250bf41c4d06bd9 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/VanPaitin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6e0912c9135004aa050123169250bf41c4d06bd9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cli_input_validator-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cli_input_validator-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37523370aa0ea073ccec0aac815d981a973645f0c8ae58473a66fdbf670474b4
|
|
| MD5 |
6f04467efb2a702360bf2484f6a628f6
|
|
| BLAKE2b-256 |
3a61d35ccbafa708fa216503f7f4e11bf3d9a2ef21673f5f37efbd9b066529c5
|
Provenance
The following attestation bundles were made for cli_input_validator-0.1.1-py3-none-any.whl:
Publisher:
release.yml on VanPaitin/cli-input-validator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cli_input_validator-0.1.1-py3-none-any.whl -
Subject digest:
37523370aa0ea073ccec0aac815d981a973645f0c8ae58473a66fdbf670474b4 - Sigstore transparency entry: 2595903372
- Sigstore integration time:
-
Permalink:
VanPaitin/cli-input-validator@6e0912c9135004aa050123169250bf41c4d06bd9 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/VanPaitin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@6e0912c9135004aa050123169250bf41c4d06bd9 -
Trigger Event:
push
-
Statement type: