Expand aliases and shortcuts.
(self, line: str)
| 646 | return to_parse, to_parse.argv[1:] |
| 647 | |
| 648 | def _expand(self, line: str) -> str: |
| 649 | """Expand aliases and shortcuts.""" |
| 650 | # Make a copy of aliases so we can keep track of what aliases have been resolved to avoid an infinite loop |
| 651 | remaining_aliases = list(self.aliases.keys()) |
| 652 | keep_expanding = bool(remaining_aliases) |
| 653 | |
| 654 | while keep_expanding: |
| 655 | keep_expanding = False |
| 656 | |
| 657 | # apply our regex to line |
| 658 | match = self._command_pattern.search(line) |
| 659 | if match: |
| 660 | # we got a match, extract the command |
| 661 | command = match.group(1) |
| 662 | |
| 663 | # Check if this command matches an alias that wasn't already processed |
| 664 | if command in remaining_aliases: |
| 665 | # rebuild line with the expanded alias |
| 666 | line = self.aliases[command] + match.group(2) + line[match.end(2) :] |
| 667 | remaining_aliases.remove(command) |
| 668 | keep_expanding = bool(remaining_aliases) |
| 669 | |
| 670 | # expand shortcuts |
| 671 | for shortcut, expansion in self.shortcuts: |
| 672 | if line.startswith(shortcut): |
| 673 | # If the next character after the shortcut isn't a space, then insert one |
| 674 | shortcut_len = len(shortcut) |
| 675 | effective_expansion = expansion |
| 676 | if len(line) == shortcut_len or line[shortcut_len] != " ": |
| 677 | effective_expansion += " " |
| 678 | |
| 679 | # Expand the shortcut |
| 680 | line = line.replace(shortcut, effective_expansion, 1) |
| 681 | break |
| 682 | return line |
| 683 | |
| 684 | @staticmethod |
| 685 | def _command_and_args(tokens: list[str]) -> tuple[str, str]: |
no test coverage detected