Updated opster.py to 7507884b8dff
authorDmitriy Morozov <dmitriy@mrzv.org>
Sat, 13 Feb 2010 14:44:24 -0800
changeset 33 039200276414
parent 32 c38b0505a831
child 34 f5109280e7fe
Updated opster.py to 7507884b8dff
opster.py
--- a/opster.py	Mon Oct 12 15:35:52 2009 -0700
+++ b/opster.py	Sat Feb 13 14:44:24 2010 -0800
@@ -74,9 +74,6 @@
                 # no catcher here because this is call from Python
                 return call_cmd_regular(func, options_)(*args, **opts)
 
-            if argv is None:
-                argv = sys.argv[1:]
-
             try:
                 opts, args = catcher(lambda: parse(argv, options_), help_func)
             except Abort:
@@ -223,18 +220,20 @@
         --pid-file   name of file to write process ID to
     <BLANKLINE>
     '''
-    print '%s\n' % usage
+    write(usage + '\n')
     doc = func.__doc__
     if not doc:
         doc = '(no help text available)'
-    print '%s\n' % doc.strip()
+    write('\n' + doc.strip() + '\n\n')
     if options:
-        print ''.join(help_options(options))
+        write(''.join(help_options(options)))
 
 def help_options(options):
     yield 'options:\n\n'
     output = []
     for short, name, default, desc in options:
+        if hasattr(default, '__call__'):
+            default = default(None)
         default = default and ' (default: %s)' % default or ''
         output.append(('%2s%s' % (short and '-%s' % short,
                                   name and ' --%s' % name),
@@ -270,7 +269,7 @@
 
     '''
     argmap, defmap, state = {}, {}, {}
-    shortlist, namelist = '', []
+    shortlist, namelist, funlist = '', [], []
 
     for short, name, default, comment in options:
         if short and len(short) != 1:
@@ -287,7 +286,8 @@
         # copy defaults to state
         if isinstance(default, list):
             state[pyname] = default[:]
-        elif callable(default):
+        elif hasattr(default, '__call__'):
+            funlist.append(pyname)
             state[pyname] = None
         else:
             state[pyname] = default
@@ -308,6 +308,7 @@
         name = argmap[opt]
         t = type(defmap[name])
         if t is types.FunctionType:
+            del funlist[funlist.index(name)]
             state[name] = defmap[name](val)
         elif t is types.IntType:
             state[name] = int(val)
@@ -318,6 +319,9 @@
         elif t in (types.NoneType, types.BooleanType):
             state[name] = not defmap[name]
 
+    for name in funlist:
+        state[name] = defmap[name](None)
+
     return state, args