Skip to content

Commit aa823bd

Browse files
committed
Refactored alias resolution to get code coverage back to 100%
1 parent 3a88759 commit aa823bd

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

cmd2/parsing.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -625,25 +625,27 @@ def get_command_arg_list(self, command_name: str, to_parse: Union[Statement, str
625625
return to_parse, to_parse.argv[1:]
626626

627627
def _expand(self, line: str) -> str:
628-
"""Expand shortcuts and aliases"""
629-
# expand aliases
630-
used_aliases = []
631-
while True:
628+
"""Expand aliases and shortcuts"""
629+
630+
# Make a copy of aliases so we can keep track of what aliases have been resolved to avoid an infinite loop
631+
remaining_aliases = list(self.aliases.keys())
632+
keep_expanding = bool(remaining_aliases)
633+
634+
while keep_expanding:
635+
keep_expanding = False
636+
632637
# apply our regex to line
633638
match = self._command_pattern.search(line)
634639
if match:
635640
# we got a match, extract the command
636641
command = match.group(1)
637642

638-
# Check if this command matches an alias and wasn't already processed to avoid an infinite loop
639-
if command in self.aliases and command not in used_aliases:
643+
# Check if this command matches an alias that wasn't already processed
644+
if command in remaining_aliases:
640645
# rebuild line with the expanded alias
641646
line = self.aliases[command] + match.group(2) + line[match.end(2):]
642-
used_aliases.append(command)
643-
else:
644-
break
645-
else:
646-
break
647+
remaining_aliases.remove(command)
648+
keep_expanding = bool(remaining_aliases)
647649

648650
# expand shortcuts
649651
for (shortcut, expansion) in self.shortcuts:

0 commit comments

Comments
 (0)