Skip to content

Commit

Permalink
Fix globaloptions with func.help() on subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarbenjamin committed May 11, 2012
1 parent 3eee917 commit 66ded13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions opster.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/opster.t
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 66ded13

Please sign in to comment.