首页 > 解决方案 > 在 TCL-8.5 中使用 Spawn-Expect 机制

问题描述

set pipeline [open "|Certify.exe args" "r"]
fconfigure $pipeline -blocking false
fconfigure $pipeline -buffering none
fileevent $pipeline readable [list handlePipeReadable $pipeline]


proc handlePipeReadable {pipe} {
if {[gets $pipe line] >= 0} {
# Managed to actually read a line; stored in $line now
} elseif {[eof $pipe]} {
    # Pipeline was closed; get exit code, etc.
    if {[catch {close $pipe} msg opt]} {
        set exitinfo [dict get $opt -errorcode]
    } else {
        # Successful termination
        set exitinfo ""
    }
    # Stop the waiting in [vwait], below
    set ::donepipe $pipe
} else {
    puts ""
    # Partial read; things will be properly buffered up for now...
    }
}

vwait ::donepipe

我曾尝试在 TCL 代码中使用管道。但出于某种原因,我想将其转换为 Spawn-Expect 机制。但是我正在努力解决它并在这样做时面临问题。谁能帮帮我??

标签: tclexpect

解决方案


Expect 使使用模式非常不同它使用不同的方式与包装程序进行交互,这更像是交互使用的工作方式(它停止了一整类与缓冲相关的错误,我怀疑这可能是你遇到的问题)。正因为如此,转换东西不是一个简单的改变。这是一个简单案例中的基本使用模式:

package require Expect

# Note: different words become different arguments here
spawn Certify.exe args

expect "some sort of prompt string"
send "your input\r";   # \r is *CARRIAGE RETURN*
expect "something else"
send "something else\r"
expect eof

close

当您可以设置超时、一次等待多个事物、等待模式以及文字字符串等时,真正的复杂性就出现了。但是在普通的 Tcl 中做同样的事情(即使忽略缓冲问题)要做的工作要多得多。几乎所有其他语言的工作也几乎总是更多。

请注意,Expect执行 GUI 自动化。只是命令行程序。GUI 自动化是一个复杂得多的话题。


不可能对可能完成的操作进行通用描述,因为这在很大程度上取决于Certify.exe程序实际执行的操作,以及您如何以交互方式使用它。


推荐阅读