Skip to content

Commit d4ba882

Browse files
committed
Change recommendations for hook names
1 parent 4f13c37 commit d4ba882

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ any of the deprecated hook methods, instead they should register their hooks as
133133
[described](https://cmd2.readthedocs.io/en/latest/hooks.html) in the cmd2
134134
documentation.
135135

136+
You should name your hooks so that they begin with the name of your plugin. Hook
137+
methods get mixed into the `cmd2` application and this naming convention helps
138+
avoid unintentional method overriding.
139+
140+
136141
Here's a simple example:
137142

138143
```python
@@ -143,9 +148,9 @@ class MyPlugin:
143148
super().__init__(*args, **kwargs)
144149
# code placed here runs after cmd2 initializes
145150
# this is where you register any hook functions
146-
self.register_postparsing_hook(self.postparsing_hook)
151+
self.register_postparsing_hook(self.cmd2_myplugin_postparsing_hook)
147152

148-
def postparsing_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
153+
def cmd2_myplugin_postparsing_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
149154
"""Method to be called after parsing user input, but before running the command"""
150155
self.poutput('in postparsing_hook')
151156
return data

cmd2_myplugin/myplugin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ def __init__(self, *args, **kwargs):
3434
super().__init__(*args, **kwargs)
3535
# code placed here runs after cmd2 initializes
3636
# this is where you register any hook functions
37-
self.register_preloop_hook(self.preloop_hook)
38-
self.register_postloop_hook(self.postloop_hook)
39-
self.register_postparsing_hook(self.postparsing_hook)
37+
self.register_preloop_hook(self.cmd2_myplugin_preloop_hook)
38+
self.register_postloop_hook(self.cmd2_myplugin_postloop_hook)
39+
self.register_postparsing_hook(self.cmd2_myplugin_postparsing_hook)
4040

4141
def do_say(self, statement):
4242
"""Simple say command"""
4343
self.poutput(statement)
4444

4545
#
4646
# define hooks as functions, not methods
47-
def preloop_hook(self) -> None:
47+
def cmd2_myplugin_preloop_hook(self) -> None:
4848
"""Method to be called before the command loop begins"""
4949
self.poutput("preloop hook")
5050

51-
def postloop_hook(self) -> None:
51+
def cmd2_myplugin_postloop_hook(self) -> None:
5252
"""Method to be called after the command loop finishes"""
5353
self.poutput("postloop hook")
5454

55-
def postparsing_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
55+
def cmd2_myplugin_postparsing_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
5656
"""Method to be called after parsing user input, but before running the command"""
5757
self.poutput('in postparsing_hook')
5858
return data

0 commit comments

Comments
 (0)