Skip to content

Instantly share code, notes, and snippets.

@imom0
Created September 10, 2012 15:29
Show Gist options
  • Save imom0/3691542 to your computer and use it in GitHub Desktop.
Save imom0/3691542 to your computer and use it in GitHub Desktop.
simple gists: argparse vs docopt
#!/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...'
#!/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()
@keleshev
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment