From c9c0cb9c3dda7b43a88c60e06ac864f1195b5c4f Mon Sep 17 00:00:00 2001 From: Case Ploeg Date: Thu, 27 Jan 2022 16:26:01 -0500 Subject: [PATCH 1/3] update postparsing hooks Previously redirection information was lost by the postparsing hooks add_whitespace_hook() and downcase_hook(). This was fine for this simple shell, but sets a bad example for more complicated use cases. To fix this, when parsing the new Statement object, include the `post_command` attribute from the original Statement. --- examples/hooks.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/hooks.py b/examples/hooks.py index e83c77fb6..c27393481 100755 --- a/examples/hooks.py +++ b/examples/hooks.py @@ -62,17 +62,19 @@ def add_whitespace_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin. command_pattern = re.compile(r'^([^\s\d]+)(\d+)') match = command_pattern.search(command) if match: - data.statement = self.statement_parser.parse( - "{} {} {}".format(match.group(1), match.group(2), '' if data.statement.args is None else data.statement.args) - ) + command = match.group(1) + first_arg = match.group(2) + rest_args = data.statement.args + post_command = data.statement.post_command + data.statement = self.statement_parser.parse(f'{command} {first_arg} {rest_args} {post_command}') return data def downcase_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: """A hook to make uppercase commands lowercase.""" command = data.statement.command.lower() - data.statement = self.statement_parser.parse( - "{} {}".format(command, '' if data.statement.args is None else data.statement.args) - ) + args = data.statement.args + post_command = data.statement.post_command + data.statement = self.statement_parser.parse(f'{command} {args} {post_command}') return data def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: From 45863b776de23deab1a64381f0bd26ccb00abb03 Mon Sep 17 00:00:00 2001 From: Case Ploeg Date: Thu, 27 Jan 2022 16:39:15 -0500 Subject: [PATCH 2/3] Add an educational postcommand hook Not necessary for the sake of the example, but might help some curious individuals get a better feel for how the system works. --- examples/hooks.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/hooks.py b/examples/hooks.py index c27393481..9c40cc98b 100755 --- a/examples/hooks.py +++ b/examples/hooks.py @@ -34,6 +34,12 @@ class CmdLineApp(cmd2.Cmd): and have them all treated as valid input which prints a list of 10 numbers starting with the number 5. + + We also add a postcommand hook, which updates the shell prompt to show the + raw contents of the Statement after the postparsing hooks are finished. To + use this hook, run `(Cmd) set debug True`. All of the above variations of + the list command should produce the same raw content. + """ # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist @@ -41,10 +47,11 @@ class CmdLineApp(cmd2.Cmd): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - # register three hooks + # register four hooks self.register_postparsing_hook(self.add_whitespace_hook) self.register_postparsing_hook(self.downcase_hook) self.register_postparsing_hook(self.abbrev_hook) + self.register_postcmd_hook(self.proof_hook) def add_whitespace_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData: """A hook to split alphabetic command names immediately followed by a number. @@ -88,6 +95,12 @@ def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.Postpars data.statement = self.statement_parser.parse(raw) return data + def proof_hook(self, data: cmd2.plugin.PostcommandData) -> cmd2.plugin.PostcommandData: + """Update the shell prompt with the new raw statement after postparsing hooks are finished""" + if self.debug: + self.prompt = f'({data.statement.raw})' + return data + @cmd2.with_argument_list def do_list(self, arglist: List[str]) -> None: """Generate a list of 10 numbers.""" From 0d2e4b8e3bf2dc093e38ff356792e5daa82ddd4c Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 13 Apr 2022 16:00:52 -0400 Subject: [PATCH 3/3] Fixed formatting error --- examples/hooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hooks.py b/examples/hooks.py index 9c40cc98b..97b90739d 100755 --- a/examples/hooks.py +++ b/examples/hooks.py @@ -37,7 +37,7 @@ class CmdLineApp(cmd2.Cmd): We also add a postcommand hook, which updates the shell prompt to show the raw contents of the Statement after the postparsing hooks are finished. To - use this hook, run `(Cmd) set debug True`. All of the above variations of + use this hook, run `(Cmd) set debug True`. All of the above variations of the list command should produce the same raw content. """