diff options
author | kotfu <kotfu@kotfu.net> | 2018-05-26 14:01:07 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-05-26 14:01:07 -0600 |
commit | bc91e16bbd08197beea1c9a115ccc5f921cfb3d6 (patch) | |
tree | fa8d1ec277fc04a6dfc5b1cd4412d691f2c06a69 /cmd2/cmd2.py | |
parent | cad21a60fa92ebe4a7c177142d273f9f7497967b (diff) | |
download | cmd2-git-bc91e16bbd08197beea1c9a115ccc5f921cfb3d6.tar.gz |
Allow registration of multiple postparsing hooks
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-x | cmd2/cmd2.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f480b3ae..6f111c7a 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -402,6 +402,10 @@ class Cmd(cmd.Cmd): import atexit atexit.register(readline.write_history_file, persistent_history_file) + # initialize plugin system + # needs to be done before we call __init__(0) + self._initialize_plugin_system() + # Call super class constructor super().__init__(completekey=completekey, stdin=stdin, stdout=stdout) @@ -1621,6 +1625,11 @@ class Cmd(cmd.Cmd): """ return statement + # + def hook_postparsing(self, statement: Statement) -> Tuple[bool, Statement]: + stop = False + return stop, statement + # noinspection PyMethodMayBeStatic def postparsing_precmd(self, statement: Statement) -> Tuple[bool, Statement]: """This runs after parsing the command-line, but before anything else; even before adding cmd to history. @@ -1682,8 +1691,15 @@ class Cmd(cmd.Cmd): stop = False try: statement = self._complete_statement(line) - (stop, statement) = self.postparsing_precmd(statement) + # call the postparsing hooks + for func in self._postparsing_hooks: + (stop, statement) = func(statement) + if stop: + break + if not stop: + (stop, statement) = self.postparsing_precmd(statement) if stop: + #stop = self.hook_cmdcompleted(stop) return self.postparsing_postcmd(stop) try: @@ -2974,6 +2990,19 @@ Script should contain one command per line, just like command would be typed in # Run the postloop() no matter what self.postloop() + ### + # + # plugin related functions + # + ### + def _initialize_plugin_system(self): + """Initialize the plugin system""" + self._postparsing_hooks = [] + + 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) + class HistoryItem(str): """Class used to represent an item in the History list. |