Created
September 10, 2012 15:29
-
-
Save imom0/3691542 to your computer and use it in GitHub Desktop.
simple gists: argparse vs docopt
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
#!/usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
"""Test docopt example. | |
Usage: | |
myopt.py run (update|watch) | |
myopt.py -h | --help | |
myopt.py --version | |
Options: | |
-h --help Show help message. | |
--version Show version. | |
""" | |
from docopt import docopt | |
if __name__ == '__main__': | |
args = docopt(__doc__, version='1.0') | |
if args['run']: | |
if args['update']: | |
print 'start updating...' | |
elif args['watch']: | |
print 'watch running status...' |
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
#!/usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--run', dest='action', | |
help='run scrpit with certain action') | |
args = parser.parse_args() | |
if args.action == 'update': | |
print 'start updating...' | |
# update() | |
elif args.action == 'watch': | |
print 'watch running status...' | |
# watch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw, line 20 in docopt version is redundant, because if args['run'] will always be true here (otherwise docopt would have shown help or version).
Also, your argparse version would be much bigger if you implemented
run
as command (as you did in docopt), it would require to add a sub-parser.