Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix globaloptions with func.help() on subcommands

  • Loading branch information...
commit 66ded1323b4ce893adc9078cbfd7903b28e81c22 1 parent 3eee917
@oscarbenjamin authored
Showing with 8 additions and 4 deletions.
  1. +5 −4 opster.py
  2. +3 −0  tests/opster.t
View
9 opster.py
@@ -165,7 +165,7 @@ def command(argv=None, scriptname=None):
return func.help(scriptname)
with exchandle(func.help, scriptname):
- with help_workaround(inner, scriptname):
+ with help_workaround(func, scriptname):
return call_cmd(scriptname, func, options_)(*args, **opts)
except ErrorHandled:
@@ -230,7 +230,7 @@ def dispatch(self, args=None, scriptname=None):
mw = cmd != '_completion' and self.middleware or None
with exchandle(help_func, cmd):
- with help_workaround(func, scriptname + ' ' + cmd):
+ with help_workaround(func, cmd, help_func):
return call_cmd(cmd, func, options, mw)(*args, **opts)
except ErrorHandled:
@@ -701,7 +701,7 @@ def guess_usage(func, options):
@contextmanager
-def help_workaround(func, scriptname):
+def help_workaround(func, scriptname, help_func=None):
'''Context manager to temporarily replace func.help'''
# Retrieve inner if function is command wrapped
func = getattr(func, '_inner', func)
@@ -713,8 +713,9 @@ def help_workaround(func, scriptname):
# Wrap the with block with a replaced help function
help = func.help
+ help_func = help_func or help
try:
- func.help = lambda: help(scriptname)
+ func.help = lambda: help_func(scriptname)
yield
finally:
func.help = help
View
3  tests/opster.t
@@ -512,6 +512,7 @@ that we can have subsubcommands.
options:
-s --showhelp Print the help message
+ -h --help display help
$ run subcmds.py cmd subcmd1 --help
subcmds.py cmd subcmd1 [OPTIONS]
@@ -551,6 +552,7 @@ that we can have subsubcommands.
-q --quiet quietly
-s --showhelp Print the help message
+ -h --help display help
$ run subcmds.py cmd subcmd2
subcmd2: invalid arguments
@@ -618,6 +620,7 @@ that we can have subsubcommands.
-l --loud loudly
-s --showhelp Print the help message
+ -h --help display help
$ run subcmds.py cmd subcmd3 subsubcmd
Please sign in to comment.
Something went wrong with that request. Please try again.