diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-05-02 00:01:40 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-05-02 00:01:40 -0400 |
commit | 9c7e400d69852f9d490f2e38771e15087d52103e (patch) | |
tree | c2f218ffa063bcf0df6461830bc85dae22fb42de /cmd2/parsing.py | |
parent | b22c0bd891ed08c8b09df56df9d91f48166a5e2a (diff) | |
download | cmd2-git-9c7e400d69852f9d490f2e38771e15087d52103e.tar.gz |
Macro resolution now occurs during parsing
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r-- | cmd2/parsing.py | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 2af8a207..41bb6b15 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -198,9 +198,9 @@ class Statement(str): return rtn @property - def expanded_command_line(self) -> str: - """Contains command_and_args plus any ending terminator, suffix, and redirection chars""" - rtn = self.command_and_args + def post_command(self) -> str: + """A string containing any ending terminator, suffix, and redirection chars""" + rtn = '' if self.multiline_command: rtn += constants.MULTILINE_TERMINATOR elif self.terminator: @@ -220,6 +220,11 @@ class Statement(str): return rtn @property + def expanded_command_line(self) -> str: + """Combines command_and_args and post_command""" + return self.command_and_args + self.post_command + + @property def argv(self) -> List[str]: """a list of arguments a la sys.argv. @@ -621,25 +626,24 @@ class StatementParser: def _expand(self, line: str) -> str: """Expand shortcuts and aliases""" - # expand aliases - # make a copy of aliases so we can edit it - tmp_aliases = list(self.aliases.keys()) - keep_expanding = bool(tmp_aliases) - while keep_expanding: - for cur_alias in tmp_aliases: - keep_expanding = False - # apply our regex to line - match = self._command_pattern.search(line) - if match: - # we got a match, extract the command - command = match.group(1) - if command and command == cur_alias: - # rebuild line with the expanded alias - line = self.aliases[cur_alias] + match.group(2) + line[match.end(2):] - tmp_aliases.remove(cur_alias) - keep_expanding = bool(tmp_aliases) - break + used_aliases = [] + while True: + # apply our regex to line + match = self._command_pattern.search(line) + if match: + # we got a match, extract the command + command = match.group(1) + + # Check if this command matches an alias and wasn't already processed to avoid an infinite loop + if command in self.aliases and command not in used_aliases: + # rebuild line with the expanded alias + line = self.aliases[command] + match.group(2) + line[match.end(2):] + used_aliases.append(command) + else: + break + else: + break # expand shortcuts for (shortcut, expansion) in self.shortcuts: |