Stop wasting your time parsing argument vectors!
$ python argparse_action.py -h
usage: argparse_action.py [-h] [-s SIMPLE_VALUE] [-c] [-t] [-f]
[-a COLLECTION] [-A] [-B] [--version]
optional arguments:
-h, --help show this help message and exit
-s SIMPLE_VALUE Store a simple value
-c Store a constant value
-t Set a switch to true
-f Set a switch to false
-a COLLECTION Add repeated values to a list
-A Add different values to list
-B Add different values to list
--version show program's version number and exit
Style: Make hard things look effortless
Usage:
quick_example.py tcp <host> <port> [--timeout=<seconds>]
quick_example.py serial <port> [--baud=9600] [--timeout=<seconds>]
quick_example.py -h | --help | --version
Can you read & understand this?
Yes?
Docopt uses the syntax specs to generate a parser
based on a simple docstring you write!
With docopt you write:
"""Usage: quick_example.py tcp <host> <port> [--timeout=<seconds>] quick_example.py serial <port> [--baud=9600] [--timeout=<seconds>] quick_example.py -h | --help | --version """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version='0.1.1rc') print(arguments) # print a dict() with our arguments
(watch the great video for more detail and play around)
~/Development/Python/docopt-demo