tcl - 在 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 机制。但是我正在努力解决它并在这样做时面临问题。谁能帮帮我??
解决方案
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
程序实际执行的操作,以及您如何以交互方式使用它。
推荐阅读
- php - iframe 跨域框架错误显示
- python - 如何使用 PyCharm 在 WSL ubuntu 中调试由 bash 脚本调用的库 python 脚本
- c# - 函数外部可用的 C# 数组
- javascript - Popper 对齐顶部下拉菜单
- windows - 无法更改 mongod 的 dbPath
- jquery - 使用 jquery 正则表达式从文本中提取 [+-] 小数
- java - G Maps Android Java,最后一个已知的位置标记颜色与位置标记不同,但在运行的应用程序上显示相同的标记颜色
- ajax - 未捕获的类型错误:无法在更改 HTMLSelectElement.onchange 时读取未定义的属性“pop”
- java - 启动相机android权限时崩溃
- laravel - 如何与这个 Laravel Eloquent 查询一起获得总和