Slide 1

Slide 1 text

docopt http://docopt.org/

Slide 2

Slide 2 text

bill israel @epochblue

Slide 3

Slide 3 text

a typical example

Slide 4

Slide 4 text

$ 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)

Slide 5

Slide 5 text

# 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)

Slide 6

Slide 6 text

argument parsing usage ARGPARSE

Slide 7

Slide 7 text

usage statements

Slide 8

Slide 8 text

Naval Fate. Usage: naval_fate ship new ... naval_fate ship move [--speed=] naval_fate ship shoot naval_fate mine (set|remove) [--moored|--drifting] naval_fate -h | --help naval_fate --version Options: -h --help Show this screen. --version Show version. --speed= Speed in knots [default: 10]. --moored Moored (anchored) mine. --drifting Drifting mine.

Slide 9

Slide 9 text

usage argument parsing DOCOPT

Slide 10

Slide 10 text

$ 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

Slide 11

Slide 11 text

# 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)

Slide 12

Slide 12 text

example: boom

Slide 13

Slide 13 text

requirements simple key/value store four commands no command = get

Slide 14

Slide 14 text

boom - a simple CLI key-value store Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find Commands: add Adds with value get Displays the value of delete Deletes and its value find Find keys matching Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action

Slide 15

Slide 15 text

BOOM.PY """ boom - a simple CLI key-value store Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find Commands: add Adds with value get Displays the value of delete Deletes and its value find Find keys matching Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """

Slide 16

Slide 16 text

BOOM.PY """ boom - a simple CLI key-value store Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find Commands: add Adds with value get Displays the value of delete Deletes and its value find Find keys matching Options: -h, --help Print this message --version Print version information -f, --force Don't confirm the action """ from docopt import docopt

Slide 17

Slide 17 text

""" boom - a simple CLI key-value store Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find Commands: add Adds with value get Displays the value of delete Deletes and its value find Find keys matching 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

Slide 18

Slide 18 text

python boom.py add -f abc Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find {'--force': True, '': None, '': 'abc', '': None, 'add': True, 'delete': False, 'find': False, 'get': False} $>

Slide 19

Slide 19 text

python boom.py add abc xyz Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find {'--force': False, '': 'xyz', '': 'abc', '': None, 'add': True, 'delete': False, 'find': False, 'get': False} $>

Slide 20

Slide 20 text

python boom.py get abc xyz Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find $>

Slide 21

Slide 21 text

BOOM.PY """ boom - a simple CLI key-value store Usage: boom boom get boom add [-f|--force] [] boom delete [-f|--force] boom find Commands: add Adds with value get Displays the value of delete Deletes and its value find Find keys matching 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)

Slide 22

Slide 22 text

¯\_(ツ)_/¯

Slide 23

Slide 23 text

benefits

Slide 24

Slide 24 text

small dependency easy to read/write doubles as documentation follows a convention

Slide 25

Slide 25 text

(fine print)

Slide 26

Slide 26 text

what can’t it do? no validation/type safety no secondary parsing spotty error messages

Slide 27

Slide 27 text

but it’s still better than argparse

Slide 28

Slide 28 text

docopt @epochblue thank you.