Skip to content

Commit 7607853

Browse files
committed
Allowing aliases to contain other aliases
1 parent 0724d38 commit 7607853

1 file changed

Lines changed: 50 additions & 12 deletions

File tree

cmd2.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,13 +2305,24 @@ def parseline(self, line):
23052305
# Deal with empty line or all whitespace line
23062306
return None, None, line
23072307

2308-
# Handle aliases
2309-
for cur_alias in self.aliases:
2310-
if line == cur_alias or line.startswith(cur_alias + ' '):
2311-
line = line.replace(cur_alias, self.aliases[cur_alias], 1)
2312-
break
2308+
# Make a copy of aliases so we can edit it
2309+
tmp_aliases = list(self.aliases.keys())
2310+
keep_expanding = len(tmp_aliases) > 0
2311+
2312+
# Expand aliases
2313+
while keep_expanding:
2314+
for cur_alias in tmp_aliases:
2315+
keep_expanding = False
2316+
2317+
if line == cur_alias or line.startswith(cur_alias + ' '):
2318+
line = line.replace(cur_alias, self.aliases[cur_alias], 1)
2319+
2320+
# Do not expand the same alias more than once
2321+
tmp_aliases.remove(cur_alias)
2322+
keep_expanding = len(tmp_aliases) > 0
2323+
break
23132324

2314-
# Expand command shortcuts to the full command name
2325+
# Expand command shortcut to its full command name
23152326
for (shortcut, expansion) in self.shortcuts:
23162327
if line.startswith(shortcut):
23172328
# If the next character after the shortcut isn't a space, then insert one
@@ -2744,6 +2755,14 @@ def do_alias(self, arglist):
27442755
can be outputted to a startup_script to preserve aliases across sessions.
27452756
27462757
Example: alias ls !ls -lF
2758+
2759+
If you want to use redirection or pipes in the alias, then either quote the tokens with these
2760+
characters or quote the entire alias value.
2761+
2762+
Examples:
2763+
alias save_results print_results ">" out.txt
2764+
alias save_results print_results "> out.txt"
2765+
alias save_results "print_results > out.txt"
27472766
"""
27482767
# If no args were given, then print a list of current aliases
27492768
if len(arglist) == 0:
@@ -3692,16 +3711,35 @@ def parsed(self, raw):
36923711
s = self.input_source_parser.transformString(s.lstrip())
36933712
s = self.commentGrammars.transformString(s)
36943713

3695-
# Handle aliases
3696-
for cur_alias in self.aliases:
3697-
if s == cur_alias or s.startswith(cur_alias + ' '):
3698-
s = s.replace(cur_alias, self.aliases[cur_alias], 1)
3699-
break
3714+
# Make a copy of aliases so we can edit it
3715+
tmp_aliases = list(self.aliases.keys())
3716+
keep_expanding = len(tmp_aliases) > 0
3717+
3718+
# Expand aliases
3719+
while keep_expanding:
3720+
for cur_alias in tmp_aliases:
3721+
keep_expanding = False
37003722

3723+
if s == cur_alias or s.startswith(cur_alias + ' '):
3724+
s = s.replace(cur_alias, self.aliases[cur_alias], 1)
3725+
3726+
# Do not expand the same alias more than once
3727+
tmp_aliases.remove(cur_alias)
3728+
keep_expanding = len(tmp_aliases) > 0
3729+
break
3730+
3731+
# Expand command shortcut to its full command name
37013732
for (shortcut, expansion) in self.shortcuts:
37023733
if s.startswith(shortcut):
3703-
s = s.replace(shortcut, expansion + ' ', 1)
3734+
# If the next character after the shortcut isn't a space, then insert one
3735+
shortcut_len = len(shortcut)
3736+
if len(s) == shortcut_len or s[shortcut_len] != ' ':
3737+
expansion += ' '
3738+
3739+
# Expand the shortcut
3740+
s = s.replace(shortcut, expansion, 1)
37043741
break
3742+
37053743
try:
37063744
result = self.main_parser.parseString(s)
37073745
except pyparsing.ParseException:

0 commit comments

Comments
 (0)