Embeddable, composable [c]ommand [l]ine [i]nterface [p]arsing
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
examples
tests
.gitignore
.travis.yml
LICENSE
MANIFEST.in
README.md
clip-logo.png
clip-logo.svg
clip.py
mkdocs.yml
setup.cfg
setup.py

README.md

clip.py


Travis docs license

Embeddable, composable command line interface parsing

Installing

clip is just a pip install clip.py away.

Basic Example

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

Testing

Call tests with python setup.py test.

Credits

  • Aaargh: Some parsing logic
  • Click: Decorator systems, parameter features
  • docopt: Help text formatting