slightly better api for fancyopts
authorAlexander Solovyov <piranha@piranha.org.ua>
Sun, 28 Jun 2009 10:19:15 +0300
changeset 23 3cd126244055
parent 22 8e56f2a8b90a
child 24 57009da05d69
slightly better api for fancyopts
fancycmd.py
test_opts.py
--- a/fancycmd.py	Sat Jun 27 22:17:18 2009 +0300
+++ b/fancycmd.py	Sun Jun 28 10:19:15 2009 +0300
@@ -11,12 +11,14 @@
 # Public interface
 # --------
 
-def fancyopts(cmd, usage, options, args):
-    if not args:
-        cmd_help(cmd, usage, options)
-    else:
-        opts, args = parse(args, options)
-        cmd(*args, **opts)
+def fancyopts(cmd, options, usage):
+    def inner(args):
+        if not args:
+            cmd_help(cmd, usage, options)
+        else:
+            opts, args = parse(args, options)
+            cmd(*args, **opts)
+    return inner
 
 def dispatch(args, cmdtable, globalopts=None):
     '''Dispatch command arguments based on subcommands.
--- a/test_opts.py	Sat Jun 27 22:17:18 2009 +0300
+++ b/test_opts.py	Sun Jun 28 10:19:15 2009 +0300
@@ -17,4 +17,4 @@
     print opts.get('pid_file')
 
 if __name__ == '__main__':
-    fancyopts(main, '%s [-l HOST] DIR' % sys.argv[0], opts, sys.argv[1:])
+    fancyopts(main, opts, usage='%s [-l HOST] DIR' % sys.argv[0])(sys.argv[1:])