fix usage generation
authorAlexander Solovyov <piranha@piranha.org.ua>
Mon, 13 Jul 2009 17:50:35 +0300
changeset 32 9d42e3d16bcf
parent 31 80701f91156c
child 33 e82b3f5c36d9
fix usage generation
finaloption.py
test.py
--- a/finaloption.py	Mon Jul 13 15:50:16 2009 +0300
+++ b/finaloption.py	Mon Jul 13 17:50:35 2009 +0300
@@ -32,24 +32,18 @@
        only for multiple subcommands
     '''
     def wrapper(func):
-        name_ = name or func.__name__
-        if '%name' in usage.split():
-            usage_ = usage.replace('%name', name_, 1)
-        else:
-            usage_ = name_ + (usage and ': ' + usage or '')
-
         options_ = options or list(guess_options(func))
         options_.append(('h', 'help', False, 'show help'))
 
+        name_ = name or func.__name__
         CMDTABLE[(shortlist and '^' or '') + name_] = (
-            func, options_, usage_)
+            func, options_, usage)
 
         def help_func(name=None):
             name_ = sys.argv[0]
             if name_.startswith('./'):
                 name_ = name_[2:]
-            usage_ = usage.replace('%name', name_, 1)
-            return help_cmd(func, usage_, options_)
+            return help_cmd(func, replace_name(usage, name_), options_)
 
         def inner(args=None):
 
@@ -156,7 +150,7 @@
             return helplist()
 
         aliases, (cmd, options, usage) = findcmd(name, cmdtable)
-        return help_cmd(cmd, aliases[0]  + ' ' + usage, options)
+        return help_cmd(cmd, replace_name(usage, aliases[0]), options)
     return inner
 
 def help_cmd(func, usage, options):
@@ -408,6 +402,12 @@
             raise ParseError(name, "invalid arguments")
         raise
 
+
+def replace_name(usage, name):
+    if '%name' in usage:
+        return usage.replace('%name', name, 1)
+    return name + ' ' + usage
+
 # --------
 # Exceptions
 # --------
--- a/test.py	Mon Jul 13 15:50:16 2009 +0300
+++ b/test.py	Mon Jul 13 17:50:35 2009 +0300
@@ -20,7 +20,7 @@
 def complex_(*args, **opts):
     '''That's more complex command indented to do something
 
-    Let's try to do that (damn, but what?!)
+    Let's try to do that (what?!)
     '''
     if opts.get('pass'):
         return