Created
April 21, 2014 21:08
-
-
Save shafi-codez/11156616 to your computer and use it in GitHub Desktop.
how to use docopt examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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