always call a function if it's default argument for an option
authorAlexander Solovyov <piranha@piranha.org.ua>
Tue, 22 Sep 2009 14:24:57 +0300
changeset 93 625fd9886735
parent 92 979199a41678
child 94 0178aca18a6d
always call a function if it's default argument for an option
opster.py
test_opts.py
--- a/opster.py	Tue Sep 15 19:38:41 2009 +0300
+++ b/opster.py	Tue Sep 22 14:24:57 2009 +0300
@@ -270,7 +270,7 @@
 
     '''
     argmap, defmap, state = {}, {}, {}
-    shortlist, namelist = '', []
+    shortlist, namelist, funlist = '', [], []
 
     for short, name, default, comment in options:
         if short and len(short) != 1:
@@ -288,6 +288,7 @@
         if isinstance(default, list):
             state[pyname] = default[:]
         elif callable(default):
+            funlist.append(pyname)
             state[pyname] = None
         else:
             state[pyname] = default
@@ -308,6 +309,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 +320,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
 
 
--- a/test_opts.py	Tue Sep 15 19:38:41 2009 +0300
+++ b/test_opts.py	Tue Sep 22 14:24:57 2009 +0300
@@ -17,7 +17,7 @@
     '''
     print locals()
 
-@command(usage='[-l HOST] [DIR ...]')
+@command(usage='[-l HOST] DIR')
 def another(dirname,
             listen=('l', 'localhost', 'ip to listen on'),
             port=('p', 8000, 'port to listen on'),
@@ -30,5 +30,5 @@
     print locals()
 
 if __name__ == '__main__':
-    main()
-    #another()
+    #main()
+    another()