在expect脚本中全局设置flags,比如nocase

Setting flags globally in expect scripts, such as nocase

我有一些 expect 脚本在 expect 命令的每个实例上调用 -nocase -re。例如:

expect {
    -nocase "this" { do_this_stuff }
    -nocase "that" { do_that_stuff }
    -nocase "others" { do_other_stuff }
}

我想通过全局调用一次选项来优化我的脚本。

我在 man pages and the wiki and the man 页面中搜索了 Tcl 本身,但没有找到执行此操作的方法参考。

是否可以在脚本的开头全局设置期望标志,该脚本适用于 expect 的每个后续调用?

也许有办法做到这一点,我不是 Expect-savy,但你也可以定义你的 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

您可能还想就地替换 expect,方法是使用 interp hide or an explicit rename:

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]
 }

也许是实施 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 }
}

这预计您传递的列表包含 pattern/action 对。不要使用其他 expect 选项,如 -glob-exact 等。传递一个奇数列表应该没问题,其中最后一个元素是一个没有动作主体的模式.