Don't know if I've just reinvented the wheel, but here's a patch which
implements expansion of variables within command arguments. e.g.

setlocal hostname example.com
setlocal moreurl abitmoreurl

go http://${hostname}/someurl/${moreurl}

This uses the python eval method so you can also do things like ${var1 +
var2} to concatenate variables etc.

-- 
jeff martin
information technologist
mkodo limited

mobile: 44 (0) 78 55 478 331
phone: 44 (0) 20 77 29 45 45
email: [EMAIL PROTECTED]

www.mkodo.com

73 Leonard St, London, EC2A 4QS. U.K



--- twill/parse.py.orig	2006-02-09 01:11:03.000000000 +0000
+++ twill/parse.py	2006-03-01 13:07:16.000000000 +0000
@@ -12,6 +12,7 @@
 
 import twill.commands as commands
 import namespaces
+import re
 
 ### pyparsing stuff
 
@@ -76,14 +77,14 @@
             newargs.append(val)
 
         # $variable substitution
-        elif arg.startswith('$'):
+        elif arg.startswith('$') and not arg.startswith('${'):
             try:
                 val = eval(arg[1:], globals_dict, locals_dict)
             except NameError:           # not in dictionary; don't interpret.
                 val = arg
             newargs.append(val)
         else:
-            newargs.append(arg)
+            newargs.append(variable_substitution(arg, globals_dict, locals_dict))
 
     return newargs
 
@@ -222,3 +223,21 @@
     global _print_commands
     _print_commands = bool(flag)
         
+
+variable_expression = re.compile("\${(.*?)}")
+
+def variable_substitution(raw_str, globals_dict, locals_dict):
+    str=''
+    pos = 0
+    for m in variable_expression.finditer(raw_str):
+        str = str+raw_str[pos:m.start()]
+        try:
+            str = str + eval(m.group(1), globals_dict, locals_dict)
+        except NameError:
+            str = str + m.group()
+        pos = m.end()
+
+    str = str+raw_str[pos:]
+
+    return str
+
setlocal foo bar

echo $foo                               # bar
echo ${foo}                             # bar
echo " ${foo}"                          #  bar
echo "stuff ${foo} stuff"               # stuff bar stuff
echo "stuff ${bar} stuff"               # stuff ${bar} stuff
echo "stuff ${foo + 'bar'} stuff"       # stuff barbar stuff
_______________________________________________
twill mailing list
[email protected]
http://lists.idyll.org/listinfo/twill

Reply via email to