Skip to main content

Simple command line input validator

Project description

invalid

A small Python library and wrapper around pick to help remove the boilerplate around validating and processing command line input.

Installation

pip install invalid

Usage

>>> from invalid import prompt

Primitives

>>> age_prompt = prompt.Int("age")
>>> age_prompt.prompt()
Enter age: s
Invalid age 's'
Enter age: 28
28

Lists

>>> fruit_prompt = prompt.List(
...    "fruit",
...    ["apple", "banana", "orange"]
    )
>>> fruit_prompt.prompt()
Enter fruit:
-> apple
   banana
   orange
'apple'

ID to name mapping

>>> fruit_prompt = prompt.List(
...     "fruit",
...     {
...         "A crunchy apple": "apple",
...         "A sweet banana": "banana",
...         "A juicy orange": "orange"
...     }
... )
>>> fruit_prompt.prompt()
Enter fruit:
-> A crunchy apple
   A sweet banana
   A juicy orange
'apple'

Custom validation

>>> postcode_prompt = prompt.Text(
...     "postcode",
...     validate=lambda text: len(text) <= 5 and text.isnumeric()
... )
Enter postcode: 392838
Invalid postcode '392838'
Enter postcode: 0773
'0773'

Forms

>>> form = prompt.Form({
...     "name": prompt.Text("your full name"),
...     "dob": prompt.Text("your date of birth"),
...     "number": prompt.Text(
...         "your favourite number between 1 - 10",
...         validate=lambda num: 1 <= num <= 10
...     )
... })
>>> form.execute()
Enter your full name: John Doe
Enter your date of birth: 2 July 1987
Enter your favourite number between 1 - 10: 3
{'name': 'John Doe', 'date': '1987-07-02', 'number': 3}

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

invalid-0.0.1.tar.gz (3.6 kB view hashes)

Uploaded Source

Supported by

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