summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-05-26 18:29:58 -0600
committerkotfu <kotfu@kotfu.net>2018-05-26 18:29:58 -0600
commitedb52d649f6496632115b3350349d53a32c1a2bd (patch)
tree475bb181e85dd9460f193241c2e3718ccc968926 /cmd2/cmd2.py
parent50bb3eaf9d1fa5f1d6d3eb75810e0446a7e0820e (diff)
downloadcmd2-git-edb52d649f6496632115b3350349d53a32c1a2bd.tar.gz
Add preloop and postloop hook capabilities
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index b7be8cb2..a694b3ae 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3080,6 +3080,8 @@ Script should contain one command per line, just like command would be typed in
self.cmdqueue.extend(callargs)
# Always run the preloop first
+ for func in self._preloop_hooks:
+ func()
self.preloop()
# If transcript-based regression testing was requested, then do that instead of the main loop
@@ -3098,6 +3100,8 @@ Script should contain one command per line, just like command would be typed in
self._cmdloop()
# Run the postloop() no matter what
+ for func in self._postloop_hooks:
+ func()
self.postloop()
###
@@ -3107,8 +3111,18 @@ Script should contain one command per line, just like command would be typed in
###
def _initialize_plugin_system(self):
"""Initialize the plugin system"""
+ self._preloop_hooks = []
+ self._postloop_hooks = []
self._postparsing_hooks = []
+ def register_preloop_hook(self, func):
+ """Register a function to be called at the beginning of the command loop."""
+ self._preloop_hooks.append(func)
+
+ def register_postloop_hook(self, func):
+ """Register a function to be called at the end of the command loop."""
+ self._postloop_hooks.append(func)
+
def register_postparsing_hook(self, func):
"""Register a function to be called after parsing user input but before running the command"""
self._postparsing_hooks.append(func)