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

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.2.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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cli_input_validator-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 8c8ae274e28cf4f396cbc3742b1c1cf1769d6eec0193aabc1fc0dc531b39a8cb
MD5 027b61a767e2eb75da85198bd94e5b72
BLAKE2b-256 304153bd690896f8b3739e469e3dd2626826af5f0d0a2035d434a02d447fc9e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cli_input_validator-0.1.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for cli_input_validator-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 96003b748e8e7035e853ec69a167a8954096fe260af9ed63b62f748e4ae8147f
MD5 b028f41a879a7869489fbc345e80deba
BLAKE2b-256 ff9d21b3c9b89d9ddcd5e6ccc9d28b0fd1a331cf03bad63163cbb7ce0cd97fd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for cli_input_validator-0.1.2-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

0.1.3

2 files

This release

0.1.2 This release

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