Skip to main content

cli-input-validator

CI

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

Add it to a project managed by uv:

uv add cli-input-validator

Or install it with pip:

python -m 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.
  • VALID is the convenience result (True, None).

Development

python -m pip install -e ".[test]"
pytest

Metadata

Release files for cli-input-validator 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cli-input-validator 0.1.3
File Size Uploaded Provenance Actions
cli_input_validator-0.1.3.tar.gz 5.5 kB With provenance Details for cli_input_validator-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cli-input-validator 0.1.3
File Interpreter ABI Platform Actions
cli_input_validator-0.1.3-py3-none-any.whl Python 3 none any Details for cli_input_validator-0.1.3-py3-none-any.whl

Total release size: 11.2 kB

Release files / cli_input_validator-0.1.3.tar.gz

Download URL cli_input_validator-0.1.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7ae716972b9561d7fbf016b9cadd4362a3ee65c3dd048de873f455121afde4c4
BLAKE2b-256 checksum
How to use checksums
9171a05ab62617fd1380861766074d94222e7bb396e91f5874b9158411a9d6ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Release files / cli_input_validator-0.1.3-py3-none-any.whl

Download URL cli_input_validator-0.1.3-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df2754b0fc7ec87725abd4891f98588126622f2dc130bb164260b89138ca56ba
BLAKE2b-256 checksum
How to use checksums
9e29d1808d8adb05b28cc6d740963ff1e7747ceb8a672058a58e6a7ce6cebf6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

Security for cli-input-validator 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cli_input_validator-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page