Commits

Ronny Pfannschmidt committed 40abb53

switch from argparse to docopt

Comments (0)

Files changed (3)

couchdb_compose/__main__.py

 import py
-from couchdb_compose.cli import parser
+import sys
+from couchdb_compose import cli
 from couchdb_compose.composer import Composer, actions
+from docopt import docopt
+
+def doc_main(args, composer=None):
+    print args
+    composer = composer or Composer(py.path.local(args['--path']))
+    composer.run_actions(actions)
+    cli._dispatch(args, composer)
 
 def main(argv=None, composer=None):
-    args = parser.parse_args(argv)
-    composer = composer or Composer(args.path)
-    composer.run_actions(actions)
-    args.func(args, composer)
-
-
+    if argv is None:
+        argv = sys.argv[1:]
+    args = docopt(cli.__doc__, argv=argv)
+    return doc_main(args, composer)
 if __name__ == '__main__':
     main()

couchdb_compose/cli.py

+"""
+couchdb-compose composes your couchdb  documents
+
+usage: couchdb-compose show [options]
+       couchdb-compose push DATABASE [--deploy-views] [options]
+       couchdb-compose deploy_views DATABASE [options]
+       couchdb-compose drop_viewdata DATABASE [options]
+
+    -h, --help      help
+    --path PATH     different path for the default composer [default: ./]
+    --deploy-views  deploy views before pushing the ddoc
+
+"""
 
 from __future__ import print_function
 import py
 
+def _dispatch(args, composer):
+    g = globals()
+    for key in args:
+        if key in g and args[key]:
+            return g[key](args, composer)
+
+    raise ValueError(args)
+
 
 def show(args, composer):
     py.std.pprint.pprint(composer.doc, py.std.sys.stdout)
 
 
 def push(args, composer):
-    db = args.database
-    if args.deploy_views:
+    db = get_database(args)
+    if args['--deploy-views']:
         deploy_views(args, composer)
 
     import couchdbkit
 
 
 def deploy_views(args, composer):
-    db = args.database
+    db = get_database(args)
     newid = composer.doc['_id'] + ":view-deploy"
     print('storing to', db, newid)
     newdoc = dict(composer.doc, _id=newid)
 
 
 def drop_viewdata(args, composer):
-    db = args.database
+    db = get_database(args)
     print('removing all ddocs')
     for ddoc in db.all_docs(startkey='_design', endkey='_desigo'):
         db.delete_doc(ddoc['id'])
     print('view cleanup')
     db.view_cleanup()
 
-from argparse import ArgumentParser
-parser = ArgumentParser(prog='couchdb-compose')
-
-parser.add_argument('--path', nargs='?',  default=py.path.local(), type=py.path.local)
-subparsers = parser.add_subparsers()
-
-show_parser = subparsers.add_parser('show', help='show the constructed ddoc')
-show_parser.set_defaults(func=show)
-
-push_parser = subparsers.add_parser('push', help='stores the composed doc to the db')
-push_parser.set_defaults(func=push)
-push_parser.add_argument('--deploy-views', action='store_true',
-                         help='deploy views before pushing')
-
-
-deploy_views_parser = subparsers.add_parser('deploy_views',
-                                            help='stores the ddoc to a different id and updates all views, '
-                                                 'usefull for view updates before a push')
-deploy_views_parser.set_defaults(func=deploy_views)
-
-
-drop_viewdata_parser = subparsers.add_parser('drop_viewdata',
-                                               help='drop all views and all view data\n'
-                                                    'use with caution, only for testing')
-drop_viewdata_parser.set_defaults(func=drop_viewdata)
-
-
-
-def get_database(name_or_uri):
+def get_database(args):
+    name_or_uri = args["DATABASE"]
     import couchdbkit
     if '/' in name_or_uri:
         return couchdbkit.Database(name_or_uri)
     else:
         return couchdbkit.Server().get_or_create_db(name_or_uri)
 
-with_database = [
-    push_parser,
-    deploy_views_parser,
-    drop_viewdata_parser,
-]
 
-for item in with_database:
-    item.add_argument('database', type=get_database)
-
-
         'Hamlish-Jinja',
         'MarkupSafe',
         'py',
+        'docopt',
     ],
-    
+
     setup_requires=[
         'hgdistver',
     ],