docopt: Declarative option parsing

Nice little library for declarative CLI parsing in Python:docopt.

At work we use Python quite a bit, but often it’s not the huge softwarepackages but little scripts and tools that are most used. Standardlibraries like argparse provide a lot of functionality, but still Ifound using it a little repetitive.

A library to support CLI definition for the 80% case quickly and easilyis a very welcome addition here.

Example source:

#!/usr/bin/python"""fubar: Tool for foo'ing my barsUsage:  fubar [-b | -f LEVEL] [--quux QUUX] ...Options:  -f --foo=LEVEL      Fooness level  -b --bar            Dont foo, rather bar  --quux QUUX         Also quux"""import sysfrom docopt import docoptarguments = docopt(__doc__, argv=sys.argv[1:], help=True, version=None)print arguments

Calling it:

$ ./fubarUsage:  fubar [-b | -f LEVEL] [--quux QUUX] ...$ ./fubar -f 12 a b c{'--bar': False, '--foo': '12', '--quux': None, '': ['a', 'b', 'c']}

Many more examples are at the website.

I wouldn’t use docopt for complex interfaces with very specificrequirements though (it tends to be a little inflexible at times), forme it really shines when I need to put something up quickly.

 · 
peter