basic usage guess
authorAlexander Solovyov <piranha@piranha.org.ua>
Tue, 28 Jul 2009 23:59:08 +0300
changeset 59 66464f54aa70
parent 58 6196eca2dd06
child 60 1d27eceed4d2
basic usage guess
finaloption.py
--- a/finaloption.py	Tue Jul 28 21:39:40 2009 +0300
+++ b/finaloption.py	Tue Jul 28 23:59:08 2009 +0300
@@ -19,7 +19,7 @@
 # Public interface
 # --------
 
-def command(options=None, usage='%name', name=None, shortlist=False):
+def command(options=None, usage=None, name=None, shortlist=False):
     '''Decorator to mark function to be used for command line processing.
 
     All arguments are optional:
@@ -43,11 +43,12 @@
             options_ = []
 
         name_ = name or func.__name__
+        usage_ = usage or guess_usage(func, options_)
         CMDTABLE[(shortlist and '^' or '') + name_] = (
-            func, options_, usage)
+            func, options_, usage_)
 
         def help_func(name=None):
-            return help_cmd(func, replace_name(usage, sysname()), options_)
+            return help_cmd(func, replace_name(usage_, sysname()), options_)
 
         @wraps(func)
         def inner(*arguments, **kwarguments):
@@ -393,6 +394,19 @@
     for lname, (sname, default, hlp) in zip(args[-len(defaults):], defaults):
         yield (sname, lname.replace('_', '-'), default, hlp)
 
+def guess_usage(func, options):
+    usage = '%name '
+    if options:
+        usage += '[OPTIONS] '
+    args, varargs = inspect.getargspec(func)[:2]
+    argnum = len(args) - len(options)
+    if argnum > 0:
+        usage += 'ARGUMENT'
+        if argnum > 1:
+            usage += 'S'
+    elif varargs:
+        usage += '[ARGUMENTS]'
+    return usage
 
 def catcher(target, help_func):
     try: