From 830c7b443aeccd6f620f7144e9bc49aa120fcdf7 Mon Sep 17 00:00:00 2001 From: Case Ploeg Date: Thu, 27 Jan 2022 16:26:01 -0500 Subject: [PATCH] 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: