Skip to content

willyg302/clip.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4429559 Β· Nov 22, 2015

History

70 Commits
Apr 19, 2015
Apr 19, 2015
Nov 18, 2015
Jan 31, 2015
Dec 22, 2014
Apr 19, 2015
Jan 31, 2015
Apr 19, 2015
Dec 13, 2014
Dec 13, 2014
Nov 18, 2015
Dec 14, 2014
Jan 31, 2015
Nov 22, 2015

Repository files navigation

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