在Tcl shell中编写Tcl脚本时,如何让Tcl将脚本中的每个Tcl命令打印到shell中?

在Tcl shell中编写Tcl脚本时,如何让Tcl将脚本中的每个Tcl命令打印到shell中?,tcl,Tcl,运行我们的软件将在终端中启动一个Tcl shell,然后我们可以通过一个Tcl脚本文件在这个Tcl shell中执行一系列Tcl命令。但是tcl脚本文件中的tcl命令不会打印在tcl shell上,因此,我们无法看到源tcl脚本执行了哪些命令 所以我的问题是,当脚本源代码生成时,Tcl是否有任何配置可以打印出脚本中的命令 下面是一个示例tcl脚本 # sample.tcl, composed of 3 tcl commands: read designfile analyze write out

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

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

下面是一个示例tcl脚本

# sample.tcl, composed of 3 tcl commands:
read designfile
analyze
write output
如果我们在tcl shell中交互执行3个tcl命令,结果将如下所示:

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

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"
我们希望在编写脚本源代码时,结果如下所示

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命令,而不仅仅是过程

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
请注意,这可能会打印出比预期多得多的内容!这里有一个更复杂的版本,只打印最外层的执行

# 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"
    }
}}}
非常感谢多纳尔! 根据他的建议,我最终得到了如下跟踪命令,以满足我的需求

proc print {args} {
    set cmd [lindex $args 0]
    puts stdout "$> $cmd"
}

trace add execution source enterstep print

我通过运行各种
package require
语句对此进行了测试,当时我意识到可能确实需要第二个版本,以避免生成数千行不必要的输出。谢谢Donal!但“应用”似乎无法识别。当我获取tcl文件时,它报告:无效的命令名“apply”升级您的tcl,对于
apply
,您至少需要8.5,最好快进到8.6。如果您没有apply,这意味着您使用的是不受支持的tcl版本。版本8.4及之前的版本不再受支持。升级。还请注意,可以用过程替换这些lambda术语。您应该将其添加为注释,并勾选Donal的回答为已接受。是的,我想这样做,但当我添加注释时,我不知道如何在注释中显示我的代码。