Share
Embed
Intro to Docopt
If you've ever struggled with argparse--and let's be honest, I know you have--then you might be interested in docopt. docopt is a simpler way to write an argument parser, by not writing the argument parser at all.
Bill Israel
January 22, 2015
Transcript
28 slides
-
$ python boo-argparse.py -h usage: boo-argparse.py [-h] [--sum] N [N
...] Process some integers. positional arguments: N an integer for the accumulator optional arguments: -h, --help show this help message and exit --sum sum the integers (default: find the max) -
# boo-argparse.py import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers',
metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print args.accumulate(args.integers) -
Naval Fate. Usage: naval_fate ship new <name>... naval_fate ship <name>
move <x> <y> [--speed=<kn>] naval_fate ship shoot <x> <y> naval_fate mine (set|remove) <x> <y> [--moored|--drifting] naval_fate -h | --help naval_fate --version Options: -h --help Show this screen. --version Show version. --speed=<kn> Speed in knots [default: 10]. --moored Moored (anchored) mine. --drifting Drifting mine. -
$ python yay-docopt.py -h usage: yay-docopt.py [--sum] N [N ...]
Process some integers. Arguments: N an integer for the accumulator Options: -h, --help show this help message and exit --version show version information --sum sum the integers -
# yay-docopt.py import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers',
metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser.parse_args() print args.accumulate(args.integers) -
boom - a simple CLI key-value store Usage: boom <key>
boom get <key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> Commands: add <key> <contents> Adds <key> with value <contents> get <key> Displays the value of <key> delete <key> Deletes <key> and its value find <term> Find keys matching <term> Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action -
BOOM.PY """ boom - a simple CLI key-value store Usage:
boom <key> boom get <key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> Commands: add <key> <contents> Adds <key> with value <contents> get <key> Displays the value of <key> delete <key> Deletes <key> and its value find <term> Find keys matching <term> Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """ -
BOOM.PY """ boom - a simple CLI key-value store Usage:
boom <key> boom get <key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> Commands: add <key> <contents> Adds <key> with value <contents> get <key> Displays the value of <key> delete <key> Deletes <key> and its value find <term> Find keys matching <term> Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """ from docopt import docopt -
""" boom - a simple CLI key-value store Usage: boom
<key> boom get <key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> Commands: add <key> <contents> Adds <key> with value <contents> get <key> Displays the value of <key> delete <key> Deletes <key> and its value find <term> Find keys matching <term> Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version='boom.py 1.0') print(arguments) BOOM.PY -
python boom.py add -f abc Usage: boom <key> boom get
<key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> {'--force': True, '<contents>': None, '<key>': 'abc', '<term>': None, 'add': True, 'delete': False, 'find': False, 'get': False} $> -
python boom.py add abc xyz Usage: boom <key> boom get
<key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> {'--force': False, '<contents>': 'xyz', '<key>': 'abc', '<term>': None, 'add': True, 'delete': False, 'find': False, 'get': False} $> -
python boom.py get abc xyz Usage: boom <key> boom get
<key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> $> -
BOOM.PY """ boom - a simple CLI key-value store Usage:
boom <key> boom get <key> boom add [-f|--force] <key> [<contents>] boom delete [-f|--force] <key> boom find <term> Commands: add <key> <contents> Adds <key> with value <contents> get <key> Displays the value of <key> delete <key> Deletes <key> and its value find <term> Find keys matching <term> Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version='boom.py 1.0') print(arguments) -
what can’t it do? no validation/type safety no secondary parsing
spotty error messages