我有一些Expect脚本,它们在Expect命令的每个实例上调用-nocase -re
。例如:
expect {
-nocase "this" { do_this_stuff }
-nocase "that" { do_that_stuff }
-nocase "others" { do_other_stuff }
}
我想通过全局调用一次选项来优化脚本。
我已经在man页,wiki和man页中搜索了Tcl本身,但没有找到执行此操作的方法的参考。
是否可以在适用于expect
的每个后续调用的脚本的开头全局设置期望标志?
答案 0 :(得分:1)
也许是实现mrcalvin建议的更好方法:
proc expect_nocase_re {pattern_action_list} {
# global spawn_id ;# this _may_ be needed
set myargs [list]
for {pattern body} $pattern_action_list {
lappend myargs -nocase -re $pattern $body
}
uplevel 1 expect $myargs
}
# usage
expect_nocase_re {
this { do_this }
that { do_that }
other { do_other }
}
这希望您传递的列表仅包含 个模式/操作对。请勿使用其他expect
选项,例如-glob
,-exact
等。传递奇数列表应该没问题,其中最后一个元素是没有动作主体的模式
答案 1 :(得分:0)
也许有很多方法可以做到,但我并不是“精明”,但是您也可以定义expect
命令:
proc my.expect args {
uplevel [list expect \
-nocase "this" { do_this_stuff } \
-nocase "that" { do_that_stuff } \
-nocase "others" { do_other_stuff } \
{*}$args]
}
这假设您每次都故意使用my.expect
。
您可能还希望通过使用interp hide
或显式rename
来就地替换expect
:
interp hide {} expect
proc expect args {
uplevel [list interp invokehidden {} expect \
-nocase "this" { do_this_stuff } \
-nocase "that" { do_that_stuff } \
-nocase "others" { do_other_stuff } \
{*}$args]
}