Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Docopt

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.

8145132ebae0c1f62cdd7b6126d71768?s=128

Bill Israel

January 22, 2015
Tweet

More Decks by Bill Israel

Other Decks in Programming

Transcript

  1. docopt http://docopt.org/

  2. bill israel @epochblue

  3. a typical example

  4. $ 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)
  5. # 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)
  6. argument parsing usage ARGPARSE

  7. usage statements

  8. 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.
  9. usage argument parsing DOCOPT

  10. $ 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
  11. # 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)
  12. example: boom

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

  14. 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
  15. 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 """
  16. 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
  17. """ 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
  18. 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} $>
  19. 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} $>
  20. 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> $>
  21. 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)
  22. ¯\_(ツ)_/¯

  23. benefits

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

    convention
  25. (fine print)

  26. what can’t it do? no validation/type safety no secondary parsing

    spotty error messages
  27. but it’s still better than argparse

  28. docopt @epochblue thank you.