Skip to main content

Argument parsing for python developers

Usage:

import argument
f = argument.Arguments()
#Requried arguments, first argument will be stored as "candy"
f.require("candy", help="Candy name")

#optional unnamed value
f.maybe("soda")

#optional value, set a default, can be changed by adding: --num=30, or -n=30
f.option("num",
    25,
    help="How many pieces?",
    abbr="n"
    )
#add a switch, a flag with no argument
f.switch("reverse",
    help="Reverse ordering",
    abbr="r"
)
f.switch("unwrap", help="unwrapcandy", abbr="u")

#Process data before saving it
f.process("candy", lambda x: x.upper())

#Parse num as integer
f.process("num", lambda x: int(x))

f.validate("num", lambda x: x > 10)

#get data
arguments, errors = f.parse()

Example:

python tests/demo.py bubblegum

{‘num’: 25, ‘soda’: None, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}

python demo.py bubblegum cubacola

{‘num’: 25, ‘soda’: ‘cubacola’, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}

python tests/demo.py bubblegum -r -n=123 –unwrap

{‘num’: 123, ‘soda’: None, ‘reverse’: True, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: True}

python tests/demo.py bubblegum –n=5

{‘num’: 5, ‘soda’: None, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}

Autogenerated help:

print(f) ``` Usage: demo.py [OPTIONS] CANDY

Required arguments: CANDY Candy name

Optional arguments: SODA N/A

Options: -n –num=25 How many pieces?

Switches: -r –reverse Reverse ordering -u –unwrap unwrapcandy ```

Release files for argument 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argument 1.4.0
File Size Uploaded
argument-1.4.0.tar.gz 4.6 kB Details

Release files / argument-1.4.0.tar.gz

Download URL argument-1.4.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
84c6a3bc874043d99620f371d2ba8b1ff31364df36781238dd321fe30c0b5b18
BLAKE2b-256 checksum
How to use checksums
6db8a8e92345fa2a9e98460909defe876899807a8854575c99c5d0595d967a74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.4.0 This release

1 release file

1.3.4

1 release file

0.3.4

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

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