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.

Bill Israel

January 22, 2015
Tweet

More Decks by Bill Israel

Other Decks in Programming

Transcript

  1. $ 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)
  2. # 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)
  3. 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.
  4. $ 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
  5. # 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)
  6. 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
  7. 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 """
  8. 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
  9. """ 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
  10. 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} $>
  11. 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} $>
  12. 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> $>
  13. 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)