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

Download files

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

Source Distribution

cli_input_validator-0.1.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

cli_input_validator-0.1.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file cli_input_validator-0.1.3.tar.gz.

File metadata

  • Download URL: cli_input_validator-0.1.3.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

Hashes for cli_input_validator-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7ae716972b9561d7fbf016b9cadd4362a3ee65c3dd048de873f455121afde4c4
MD5 5f8f0a73df53847acdc13b24ef0e4ba8
BLAKE2b-256 9171a05ab62617fd1380861766074d94222e7bb396e91f5874b9158411a9d6ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for cli_input_validator-0.1.3.tar.gz:

Publisher: release.yml on VanPaitin/cli-input-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cli_input_validator-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for cli_input_validator-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 df2754b0fc7ec87725abd4891f98588126622f2dc130bb164260b89138ca56ba
MD5 c1ce86890608c808da6ca1e5af7cd9d7
BLAKE2b-256 9e29d1808d8adb05b28cc6d740963ff1e7747ceb8a672058a58e6a7ce6cebf6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cli_input_validator-0.1.3-py3-none-any.whl:

Publisher: release.yml on VanPaitin/cli-input-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

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