fix bug with option names clashing with call_cmd argument names
authorAlexander Solovyov <piranha@piranha.org.ua>
Wed, 02 Sep 2009 12:50:02 +0300
changeset 78 69c10074f5c5
parent 77 2f1dfda4cfb3
child 79 cc24c392fd45
fix bug with option names clashing with call_cmd argument names
docs/changelog.rst
opster.py
test.py
--- a/docs/changelog.rst	Wed Aug 19 12:35:31 2009 +0300
+++ b/docs/changelog.rst	Wed Sep 02 12:50:02 2009 +0300
@@ -1,6 +1,10 @@
 Changelog
 ---------
 
+0.9.8
+~~~~~
+Fixed bug with option names clashing with name of arguments for call_cmd. 
+
 0.9.7
 ~~~~~
 Library renamed to opster.
--- a/opster.py	Wed Aug 19 12:35:31 2009 +0300
+++ b/opster.py	Wed Sep 02 12:50:02 2009 +0300
@@ -75,7 +75,7 @@
             try:
                 if opts.pop('help', False):
                     return help_func()
-                return catcher(lambda: call_cmd(name_, func, *args, **opts),
+                return catcher(lambda: call_cmd(name_, func)(*args, **opts),
                                help_func)
             except Abort:
                 return -1
@@ -121,7 +121,7 @@
             lambda: _dispatch(args, cmdtable, globaloptions),
             help_func)
         return catcher(
-            lambda: call_cmd(name, middleware(func), *args, **kwargs),
+            lambda: call_cmd(name, middleware(func))(*args, **kwargs),
             help_func)
     except Abort:
         return -1
@@ -444,13 +444,15 @@
 
     raise Abort
 
-def call_cmd(name, func, *args, **kwargs):
-    try:
-        return func(*args, **kwargs)
-    except TypeError:
-        if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
-            raise ParseError(name, "invalid arguments")
-        raise
+def call_cmd(name, func):
+    def inner(*args, **kwargs):
+        try:
+            return func(*args, **kwargs)
+        except TypeError:
+            if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
+                raise ParseError(name, "invalid arguments")
+            raise
+    return inner
 
 def replace_name(usage, name):
     if '%name' in usage:
--- a/test.py	Wed Aug 19 12:35:31 2009 +0300
+++ b/test.py	Wed Sep 02 12:50:02 2009 +0300
@@ -16,7 +16,8 @@
     ui.write('\n')
 
 cplx_opts = [('p', 'pass', False, 'don\'t run the command'),
-             ('', 'exit', 0, 'exit with supplied code (default: 0)')]
+             ('', 'exit', 0, 'exit with supplied code (default: 0)'),
+             ('n', 'name', '', 'optional name')]
 
 @command(cplx_opts, usage='[-p] [--exit value] ...', name='complex', hide=True)
 def complex_(ui, *args, **opts):