当前TCL脚本的完整路径

dmo*_*ock 14 tcl

是否有可能获得当前正在执行的TCL脚本的完整路径?

在PHP中它将是: __FILE__

Cam*_*ird 16

根据"当前正在执行的TCL脚本"的含义,您实际上可能会寻求info script甚至可能info nameofexecutable更深奥的东西.


nik*_*ikc 13

检索当前语句所在文件名的正确方法是(真正等同于PHP/C++ __FILE__):

set thisFile [ dict get [ info frame 0 ] file ]
Run Code Online (Sandbox Code Playgroud)

Psuedocode(它是如何工作的):

  1. set thisFile <value> :将变量thisFile设置为 value
  2. dict get <dict> file :从dict返回文件值
  3. info frame <#>:返回一个dict,其中包含有关指定堆栈级别(#)的帧的信息,0并将返回最新的堆栈帧 注意:有关信息框架的更多信息,请参阅文章末尾.

在这种情况下,file返回的值info frame已经标准化,因此file normalize <path>不需要.

info script和之间的区别info frame主要是与Tcl包一起使用.如果info script在包require(require package <name>)中提供的Tcl文件中使用,那么info script将返回当前正在执行的Tcl脚本的路径,并且不会提供包含该info script命令的Tcl文件的实际名称; 但是,info frame此处提供的示例将正确返回包含该命令的文件的文件名.

如果您想要当前正在评估的脚本的名称,那么:

set sourcedScript [ info script ]
Run Code Online (Sandbox Code Playgroud)

如果您想要最初调用的脚本(或解释器)的名称,那么:

set scriptAtInvocation $::argv0
Run Code Online (Sandbox Code Playgroud)

如果您想要最初调用的可执行文件的名称,那么:

set exeAtInvocation [ info nameofexecutable ]
Run Code Online (Sandbox Code Playgroud)

更新 - 详细信息: info frame

这是堆栈跟踪在Tcl中的样子.该frame_index是我们展示什么info frame $frame_index样子从值0通过[ info frame ].

呼叫info frame [ info frame ]在功能上等同于info frame 0,但使用0当然更快.

只有真正1[ info frame ]栈帧,和0行为像[ info frame ].在这个例子中,你可以看到05(它[ info frame ])是相同的:

frame_index: 0 | type = source | proc = ::stacktrace | line = 26 | level = 0 | file = /tcltest/stacktrace.tcl | cmd = info frame $frame_counter
frame_index: 1 | type = source | line = 6 | level = 4 | file = /tcltest/main.tcl | cmd = a
frame_index: 2 | type = source | proc = ::a | line = 2 | level = 3 | file = /tcltest/a.tcl | cmd = b
frame_index: 3 | type = source | proc = ::b | line = 2 | level = 2 | file = /tcltest/b.tcl | cmd = c
frame_index: 4 | type = source | proc = ::c | line = 5 | level = 1 | file = /tcltest/c.tcl | cmd = stacktrace
frame_index: 5 | type = source | proc = ::stacktrace | line = 26 | level = 0 | file = /tcltest/stacktrace.tcl | cmd = info frame $frame_counter
Run Code Online (Sandbox Code Playgroud)

请参阅:https: //github.com/Xilinx/XilinxTclStore/blob/master/tclapp/xilinx/profiler/app.tcl#L273


gle*_*man 8

你要 $argv0

  • 请注意,全局`argv0`变量是tclsh和wish(以及tclkit)的一个特性,而不是Tcl的特征.但这很常见; 支持函数`Tcl_Main`为你实现它... (4认同)