summaryrefslogtreecommitdiff
path: root/cmd2/parsing.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-06 01:38:15 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-06 01:38:15 -0400
commitaa823bd0f2d1151f6c5ced7be01fdc0b20e6dff5 (patch)
treee21dbf97f5f8f2130a9fe06f5ae64d40e2bf1a4d /cmd2/parsing.py
parent3a887590edc956d32565359455f9a12da8372f14 (diff)
downloadcmd2-git-aa823bd0f2d1151f6c5ced7be01fdc0b20e6dff5.tar.gz
Refactored alias resolution to get code coverage back to 100%
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r--cmd2/parsing.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 41bb6b15..a2df1de7 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -625,25 +625,27 @@ class StatementParser:
return to_parse, to_parse.argv[1:]
def _expand(self, line: str) -> str:
- """Expand shortcuts and aliases"""
- # expand aliases
- used_aliases = []
- while True:
+ """Expand aliases and shortcuts"""
+
+ # Make a copy of aliases so we can keep track of what aliases have been resolved to avoid an infinite loop
+ remaining_aliases = list(self.aliases.keys())
+ keep_expanding = bool(remaining_aliases)
+
+ while keep_expanding:
+ 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)
- # 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:
+ # Check if this command matches an alias that wasn't already processed
+ if command in remaining_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
+ remaining_aliases.remove(command)
+ keep_expanding = bool(remaining_aliases)
# expand shortcuts
for (shortcut, expansion) in self.shortcuts: