Skip to content

Command line input validator for the Python language

License

Notifications You must be signed in to change notification settings

ryan-mooore/invalid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

invalid

A simple input validator for the command line and wrapper around pick to help remove boilerplate when processing cli 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}

About

Command line input validator for the Python language

Resources

License

Stars

Watchers

Forks

Packages

No packages published