Command line argument parsing library for python


License
Other
Install
pip install argument==0.3.4

Documentation

arguments

Argument parsing for python developers

Usage:

import argument
f = argument.Arguments()
#Requried arguments, first argument will be stored as "candy"
f.always("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()

if len(errors) > 0:
    print errors
    print
    print f
else:
    print arguments

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

Argument mux

import argument
a1 = argument.Arguments()
a1.always("name", help="Candy name")
a1.always("taste", help="Taste")

a2 = argument.Arguments()
a2.always("name", help="Soda name")

m = argument.Mux({'candy': a1, 'soda': a2})

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

if len(errors) > 0:
    print errors
    print
    print m
else:
    print arguments