首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当在Tcl shell中生成Tcl脚本时,如何让Tcl将脚本中的每个Tcl命令打印到shell中?

当在Tcl shell中生成Tcl脚本时,如何让Tcl将脚本中的每个Tcl命令打印到shell中?
EN

Stack Overflow用户
提问于 2020-09-10 14:45:09
回答 2查看 1.4K关注 0票数 0

运行我们的软件将在终端启动一个Tcl外壳,然后我们可以通过创建一个tcl脚本文件在这个Tcl shell中执行一系列tcl命令。但是tcl脚本文件中的tcl命令不会打印在Tcl shell上,因此,我们无法看到源tcl脚本执行了哪些命令。

所以我的问题是,当脚本是源的时候,是否有任何Tcl的配置可以打印脚本中的命令。

下面是一个示例tcl脚本

代码语言:javascript
复制
# sample.tcl, composed of 3 tcl commands:
read designfile
analyze
write output

如果我们在tcl shell中以交互方式执行3个Tcl命令,结果如下:

代码语言:javascript
复制
Tcl> read designfile
Info: reading designfile
Info: size: 128k
...
...
Tcl> analyze
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
Tcl> write output
Info: analyzed data been written into file "output"

如果我们在tcl shell中获取这个Tcl sript,结果将是:

代码语言:javascript
复制
Tcl> source sample.tcl
Info: reading designfile
Info: size: 128k
...
...
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
Info: analyzed data been written into file "output"

我们期望当我们获取脚本时,结果将如下所示。

代码语言:javascript
复制
Tcl> source sample.tcl
> read designfile
Info: reading designfile
Info: size: 128k
...
...
> analyze
Info: analyzing designfile
Info: 108 modules need to be analyzed
...
...
> write output
Info: analyzed data been written into file "output"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-10 15:04:28

这是执行追踪的任务。这些可以应用于各种Tcl命令,而不仅仅是过程。

代码语言:javascript
复制
trace add execution source enterstep {apply {{cmd op} {
    # Trim multiline commands down a bit; I find this helps me at least!
    regsub {\n.*} $cmd ... cmd
    # Print what's being run
    puts "EXECUTE: $cmd"
}}}
source thefile.tcl

请注意,这可能会打印出比您预期的多得多!这里有一个更复杂的版本,它只输出最外层的执行级别。

代码语言:javascript
复制
# Called before [source] starts running
trace add execution source enter {apply {args {
    global SOURCE_LEVEL
    if {![info exists SOURCE_LEVEL]} {
        set SOURCE_LEVEL [info level]
    }
}}}
# Called after [source] finishes running
trace add execution source leave {apply {args {
    global SOURCE_LEVEL
    if {$SOURCE_LEVEL == [info level]} {
        unset SOURCE_LEVEL
    }
}}}
# Called for every command called while [source] is running
trace add execution source enterstep {apply {{cmd op} {
    global SOURCE_LEVEL
    if {$SOURCE_LEVEL == [info level]} {
        regsub {\n.*} $cmd "..." cmd
        puts "EXECUTE: $cmd"
    }
}}}
票数 1
EN

Stack Overflow用户

发布于 2020-09-11 08:46:11

非常感谢Donal!根据他的建议,最后我得到了如下跟踪命令,以满足我的要求。

代码语言:javascript
复制
proc print {args} {
    set cmd [lindex $args 0]
    puts stdout "$> $cmd"
}

trace add execution source enterstep print
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63832193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档