Skip to content

Instantly share code, notes, and snippets.

@shafi-codez
Created April 21, 2014 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shafi-codez/11156616 to your computer and use it in GitHub Desktop.
Save shafi-codez/11156616 to your computer and use it in GitHub Desktop.
how to use docopt examples
"""Usage:
app.py --user=username --pwd=password --json
app.py --user=username --pwd=password
Arguments:
--user=username Application User Name
--pwd=password Application password
Options:
--json enable json output
"""
#/usr/bin/python2.6 /home/sulla/PycharmProjects/testProj/app.py --user=shafi --pwd=testing --json
# Docopt is a library for parsing command line arguments
import docopt
import json
if __name__ == '__main__':
result = {}
try:
# Parse arguments, use file docstring as a parameter definition
arguments = docopt.docopt(__doc__)
user = arguments['--user']
pwd = arguments['--pwd']
result['user'] = user
result['pwd'] = pwd
result['result'] = 'ok'
if arguments['--json']:
print json.dumps(result,indent=4)
# Handle invalid options
except docopt.DocoptExit as e:
print e.usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment