在 Tcl shell 中获取 Tcl 脚本时,如何让 Tcl 将脚本中的每个 Tcl 命令打印到 shell? [英] When source a Tcl script in Tcl shell, how can I let Tcl print every Tcl command inside the script to the shell?

查看:81
本文介绍了在 Tcl shell 中获取 Tcl 脚本时,如何让 Tcl 将脚本中的每个 Tcl 命令打印到 shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行我们的软件将在终端中启动一个 Tcl shell,然后我们可以通过 tcl 脚本文件在这个 Tcl shell 中执行一系列 tcl 命令.但是tcl脚本文件中的tcl命令不会被打印到Tcl shell上,所以我们看不到tcl脚本源码执行了哪些命令.

Running our software will start a Tcl shell in Terminal, then we can execute a series of tcl commands in this Tcl shell by souce a tcl script file. But the tcl commands inside the tcl script file will not be printed on the Tcl shell, so, we cannot see what commands have been executed by source the tcl script.

所以我的问题是,当脚本为源代码时,是否有任何 Tcl 配置可以打印出脚本中的命令.

So my question is, is there any configuration of Tcl can print out the commands in the script when the script been source.

以下是一个示例 tcl 脚本

Following is an sample tcl script

# sample.tcl, composed of 3 tcl commands:
read designfile
analyze
write output

如果我们在 Tcl shell 中交互执行 3 个 tcl 命令,结果将如下所示:

If we interactively execute the 3 tcl commands in the Tcl shell, the result will be look as:

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 脚本,结果将是:

If we source this tcl sript in Tcl shell, the result will be:

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"

我们希望在获取脚本时的结果如下.

We expect the the result will be as below when we source the script.

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"

推荐答案

这是执行跟踪的工作.这些可以应用于各种 Tcl 命令,而不仅仅是过程.

That's a job for an execution trace. These can be applied to all sorts of Tcl commands, not just procedures.

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

请注意,这可能会打印出比您预期更多的内容!这是一个更复杂的版本,它只打印最外层的执行.

Note that this may print a lot more than you expect! Here's a more sophisticated version that prints only the outermost level of execution.

# 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"
    }
}}}

这篇关于在 Tcl shell 中获取 Tcl 脚本时,如何让 Tcl 将脚本中的每个 Tcl 命令打印到 shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆