Embeddable, composable command line interface parsing
clip is just a pip install clip.py
away.
This example is just to whet your appetite. For a more in-depth guide to using clip, please see the docs.
import clip
app = clip.App()
@app.main(description='A very unhelpful shopping list CLI program')
def shopping():
pass
@shopping.subcommand(description='Add an item to the list')
@clip.arg('item', required=True)
@clip.opt('-q', '--quantity', default=1, help='How many of the item to get')
def add(item, quantity):
clip.echo('Added "{} - {}" to the list'.format(item, quantity))
@shopping.subcommand(description='See all items on the list')
@clip.flag('--sorted', help='View items in alphabetical order')
def view(sorted):
clip.echo('This is your {}sorted list'.format('' if sorted else 'un'))
if __name__ == '__main__':
try:
app.run()
except clip.ClipExit:
pass
If you save the above code in a file called shopping.py
, you can then do the following:
$ python shopping.py -h
shopping: A very unhelpful shopping list CLI program
Usage: shopping {{options}} {{subcommand}}
Options:
-h, --help Show this help message and exit
Subcommands:
add Add an item to the list
view See all items on the list
$ python shopping.py add -h
shopping add: Add an item to the list
Usage: add {{arguments}} {{options}}
Arguments:
item [text]
Options:
-h, --help Show this help message and exit
-q, --quantity [int] How many of the item to get (default: 1)
$ python shopping.py add
Error: Missing parameter "item".
$ python shopping.py add cookies -q 10
Added "cookies - 10" to the list
$ python shopping.py view
This is your unsorted list
$ python shopping.py view --sorted
This is your sorted list
Call tests with python setup.py test
.