Skip to content

Commit 830c7b4

Browse files
Case Ploegkmvanbrunt
authored andcommitted
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.
1 parent 036f185 commit 830c7b4

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

examples/hooks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,19 @@ def add_whitespace_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.
6262
command_pattern = re.compile(r'^([^\s\d]+)(\d+)')
6363
match = command_pattern.search(command)
6464
if match:
65-
data.statement = self.statement_parser.parse(
66-
"{} {} {}".format(match.group(1), match.group(2), '' if data.statement.args is None else data.statement.args)
67-
)
65+
command = match.group(1)
66+
first_arg = match.group(2)
67+
rest_args = data.statement.args
68+
post_command = data.statement.post_command
69+
data.statement = self.statement_parser.parse(f'{command} {first_arg} {rest_args} {post_command}')
6870
return data
6971

7072
def downcase_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
7173
"""A hook to make uppercase commands lowercase."""
7274
command = data.statement.command.lower()
73-
data.statement = self.statement_parser.parse(
74-
"{} {}".format(command, '' if data.statement.args is None else data.statement.args)
75-
)
75+
args = data.statement.args
76+
post_command = data.statement.post_command
77+
data.statement = self.statement_parser.parse(f'{command} {args} {post_command}')
7678
return data
7779

7880
def abbrev_hook(self, data: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:

0 commit comments

Comments
 (0)