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 details)

Uploaded Source

File details

Details for the file invalid-0.0.1.tar.gz.

File metadata

  • Download URL: invalid-0.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for invalid-0.0.1.tar.gz
Algorithm Hash digest
SHA256 87c5e330295211a16a9b4ea698ca88a518e28faca3aa29637f4d2edb909cb14b
MD5 db5dc3fb4f92096f810c158e756e91ea
BLAKE2b-256 2eed7005370148b6679830ed237078415369733ba668e9a6a66e362879dec118

See more details on using hashes here.

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