From b0b825d0cdc0e38b48477719c608a065692f413d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 17 Jul 2020 16:01:54 -0400 Subject: Fixed hook documentation --- cmd2/cmd2.py | 8 ++++---- docs/features/hooks.rst | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 65047cc8..cdee3523 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -4281,11 +4281,11 @@ class Cmd(cmd.Cmd): signature = inspect.signature(func) _, param = list(signature.parameters.items())[0] if param.annotation != plugin.CommandFinalizationData: - raise TypeError("{} must have one parameter declared with type " - "'cmd2.plugin.CommandFinalizationData'".format(func.__name__)) + raise TypeError("{} must have one parameter declared with type {}".format(func.__name__, + plugin.CommandFinalizationData)) if signature.return_annotation != plugin.CommandFinalizationData: - raise TypeError("{} must declare return a return type of " - "'cmd2.plugin.CommandFinalizationData'".format(func.__name__)) + raise TypeError("{} must declare return a return type of {}".format(func.__name__, + plugin.CommandFinalizationData)) def register_cmdfinalization_hook(self, func: Callable[[plugin.CommandFinalizationData], plugin.CommandFinalizationData]) -> None: diff --git a/docs/features/hooks.rst b/docs/features/hooks.rst index d05e2638..acf9dcc4 100644 --- a/docs/features/hooks.rst +++ b/docs/features/hooks.rst @@ -26,11 +26,11 @@ You can also register methods to be called at the beginning of the command loop:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_preloop_hook(self.myhookmethod) - def myhookmethod(self): + def myhookmethod(self) -> None: self.poutput("before the loop begins") To retain backwards compatibility with ``cmd.Cmd``, after all registered @@ -41,12 +41,12 @@ A similar approach allows you to register functions to be called after the command loop has finished:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_postloop_hook(self.myhookmethod) - def myhookmethod(self): - self.poutput("before the loop begins") + def myhookmethod(self) -> None: + self.poutput("after the loop ends") To retain backwards compatibility with ``cmd.Cmd``, after all registered postloop hooks have been called, the :meth:`~cmd2.Cmd.postloop` method is @@ -147,7 +147,7 @@ timer for command execution been started. To define and register a postparsing hook, do the following:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_postparsing_hook(self.myhookmethod) @@ -215,7 +215,7 @@ Once output is redirected and the timer started, all the hooks registered with :meth:`~cmd2.Cmd.register_precmd_hook` are called. Here's how to do it:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_precmd_hook(self.myhookmethod) @@ -254,7 +254,7 @@ timer is still running. Here's how to define and register a postcommand hook:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_postcmd_hook(self.myhookmethod) @@ -306,7 +306,7 @@ or the command method raise an exception. Here's how to create and register a command finalization hook:: class App(cmd2.Cmd): - def __init__(self, *args, *kwargs): + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.register_cmdfinalization_hook(self.myhookmethod) -- cgit v1.2.1