[alias] Fix variable replacement in aliases
This commit is contained in:
parent
859b32abb7
commit
c0e6b26b0c
@ -146,23 +146,20 @@ def cmd_unalias(msg):
|
|||||||
|
|
||||||
|
|
||||||
def replace_variables(cnt, msg=None):
|
def replace_variables(cnt, msg=None):
|
||||||
cnt = cnt.split(' ')
|
if isinstance(cnt, list):
|
||||||
|
return [replace_variables(c, msg) for c in cnt]
|
||||||
|
|
||||||
unsetCnt = list()
|
unsetCnt = list()
|
||||||
for i in range(0, len(cnt)):
|
for res in re.findall("\\$\{(?P<name>[a-zA-Z0-9]+)\}", cnt):
|
||||||
if i not in unsetCnt:
|
|
||||||
res = re.match("^([^$]*)(\\\\)?\\$([a-zA-Z0-9]+)(.*)$", cnt[i])
|
|
||||||
if res is not None:
|
|
||||||
try:
|
try:
|
||||||
varI = int(res.group(3))
|
varI = int(res) - 1
|
||||||
|
cnt = cnt.replace("${%s}" % res, msg.args[varI], 1)
|
||||||
unsetCnt.append(varI)
|
unsetCnt.append(varI)
|
||||||
cnt[i] = res.group(1) + msg.cmds[varI] + res.group(4)
|
|
||||||
except:
|
except:
|
||||||
if res.group(2) != "":
|
cnt = cnt.replace("${%s}" % res, get_variable(res), 1)
|
||||||
cnt[i] = res.group(1) + "$" + res.group(3) + res.group(4)
|
for u in sorted(unsetCnt, reverse=True):
|
||||||
else:
|
msg.args.pop(u)
|
||||||
cnt[i] = (res.group(1) + get_variable(res.group(3), msg) +
|
return cnt
|
||||||
res.group(4))
|
|
||||||
return " ".join(cnt)
|
|
||||||
|
|
||||||
|
|
||||||
@hook("pre_Command")
|
@hook("pre_Command")
|
||||||
@ -176,7 +173,7 @@ def treat_alias(msg):
|
|||||||
args = shlex.split(txt)
|
args = shlex.split(txt)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
args = txt.split(' ')
|
args = txt.split(' ')
|
||||||
nmsg = Command(args[0], args[1:] + msg.args, **msg.export_args())
|
nmsg = Command(args[0], replace_variables(args[1:], msg) + msg.args, **msg.export_args())
|
||||||
|
|
||||||
# Avoid infinite recursion
|
# Avoid infinite recursion
|
||||||
if msg.cmd != nmsg.cmd:
|
if msg.cmd != nmsg.cmd:
|
||||||
|
Loading…
Reference in New Issue
Block a user