summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCase Ploeg <caseploog@gmail.com>2022-01-27 16:26:01 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2022-04-13 16:07:05 -0400
commit830c7b443aeccd6f620f7144e9bc49aa120fcdf7 (patch)
tree9dd5ba338fd6957ac62a6eea7cabe2942ace683f
parent036f1858e94d763ec6ffa02fa632e3b96165f58d (diff)
downloadcmd2-git-830c7b443aeccd6f620f7144e9bc49aa120fcdf7.tar.gz
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.
-rwxr-xr-xexamples/hooks.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/examples/hooks.py b/examples/hooks.py
index e83c77fb..c2739348 100755
--- a/examples/hooks.py
+++ b/examples/hooks.py
@@ -62,17 +62,19 @@ class CmdLineApp(cmd2.Cmd):
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: