Permalink
Browse files

Real fix and test for main.help() with scriptname

  • Loading branch information...
1 parent 6fb66e7 commit 510467f1eddd5e14a16ee9f3d07d36a01083eb00 @oscarbenjamin committed May 9, 2012
Showing with 20 additions and 5 deletions.
  1. +18 −3 opster.py
  2. +1 −1 tests/opster.t
  3. +1 −1 tests/selfhelp.py
View
@@ -165,8 +165,8 @@ def command(argv=None, scriptname=None):
return func.help(scriptname)
with exchandle(func.help, scriptname):
- func.help = lambda: func.help(scriptname)
- return call_cmd(scriptname, func, options_)(*args, **opts)
+ with help_workaround(inner, scriptname):
+ return call_cmd(scriptname, func, options_)(*args, **opts)
except ErrorHandled:
return -1
@@ -227,7 +227,8 @@ def dispatch(self, args=None, scriptname=None):
mw = cmd != '_completion' and self.middleware or None
with exchandle(help_func, cmd):
- return call_cmd(cmd, func, options, mw)(*args, **opts)
+ with help_workaround(func, scriptname):
+ return call_cmd(cmd, func, options, mw)(*args, **opts)
except ErrorHandled:
return -1
@@ -697,6 +698,20 @@ def guess_usage(func, options):
@contextmanager
+def help_workaround(func, scriptname):
+ '''Context manager to temporarily replace func.help'''
+ if not hasattr(func, 'help'):
+ yield
+ return
+ help = func.help
+ try:
+ func.help = lambda: help(scriptname)
+ yield
+ finally:
+ func.help = help
+
+
+@contextmanager
def exchandle(help_func, cmd=None):
'''Context manager to turn internal exceptions into printed help messages.
View
@@ -293,7 +293,7 @@ Another things should be checked: calling help display from the function
itself::
$ run selfhelp.py --assist
- selfhelp.py [OPTIONS]
+ selfhelp [OPTIONS]
Displays ability to show help
View
@@ -9,4 +9,4 @@ def selfhelp(assist=('', False, 'show help')):
print('no help for you!')
if __name__ == '__main__':
- selfhelp.command()
+ selfhelp.command(scriptname='selfhelp')

0 comments on commit 510467f

Please sign in to comment.