当前 TCL 脚本的完整路径

发布于 2024-09-25 16:26:08 字数 75 浏览 3 评论 0原文

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

在 PHP 中,它是:__FILE__

Is there a possibility to get the full path of the currently executing TCL script?

In PHP it would be: __FILE__

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尛丟丟 2024-10-02 16:26:08

根据“当前执行 TCL 脚本”的含义,您实际上可能会寻求 信息脚本,甚至可能info nameofexecutable< /code>或者更深奥的东西。

Depending on what you mean by "currently executing TCL script", you might actually seek info script, or possibly even info nameofexecutable or something more esoteric.

命比纸薄 2024-10-02 16:26:08

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

set thisFile [ dict get [ info frame 0 ] file ]

Psuedocode(工作原理):

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

在这种情况下,从信息框架返回的文件值已经标准化,因此不需要文件标准化

info scriptinfo frame 之间的区别主要在于与 Tcl 包一起使用。如果在 package require (require package) 期间提供的 Tcl 文件中使用 info script,则 info script 将返回当前正在执行的 Tcl 脚本的路径,并且不会提供包含 info script 命令的 Tcl 文件的实际名称;但是,此处提供的信息框架示例将正确返回包含该命令的文件的文件名。

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

set sourcedScript [ info script ]

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

set scriptAtInvocation $::argv0

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

set exeAtInvocation [ info nameofexecutable ]

更新 - 详细信息:信息框架

这是 Tcl 中堆栈跟踪的样子。 frame_index 向我们展示了信息帧 $frame_index 的值从 0[ info frame ] 的样子。

调用infoframe [ infoframe]在功能上等同于infoframe0,但使用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

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

The correct way to retrieve the name of the file that the current statement resides in, is this (a true equivalent to PHP/C++'s __FILE__):

set thisFile [ dict get [ info frame 0 ] file ]

Psuedocode (how it works):

  1. set thisFile <value> : sets variable thisFile to value
  2. dict get <dict> file : returns the file value from a dict
  3. info frame <#> : returns a dict with information about the frame at the specified stack level (#), and 0 will return the most recent stack frame
    NOTICE: See end of post for more information on info frame.

In this case, the file value returned from info frame is already normalized, so file normalize <path> in not needed.

The difference between info script and info frame is mainly for use with Tcl Packages. If info script was used in a Tcl file that was provided durring a package require (require package <name>), then info script would return the path to the currently executing Tcl script and would not provide the actual name of the Tcl file that contained the info script command; However, the info frame example provided here would correctly return the file name of the file that contains the command.

If you want the name of the script currently being evaluated, then:

set sourcedScript [ info script ]

If you want the name of the script (or interpreter) that was initially invoked, then:

set scriptAtInvocation $::argv0

If you want the name of the executable that was initially invoked, then:

set exeAtInvocation [ info nameofexecutable ]

UPDATE - Details about: info frame

Here is what a stacktrace looks like within Tcl. The frame_index is the showing us what info frame $frame_index looks like for values from 0 through [ info frame ].

Calling info frame [ info frame ] is functionally equivalent to info frame 0, but using 0 is of course faster.

There are only actually 1 to [ info frame ] stack frames, and 0 behaves like [ info frame ]. In this example you can see that 0 and 5 (which is [ info frame ]) are the same:

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

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

少钕鈤記 2024-10-02 16:26:08

您想要 $argv0

You want $argv0

叫嚣ゝ 2024-10-02 16:26:08

您也可以使用 [file normalize] 来获取完全规范化的名称。

file normalize $argv0
file normalize [info nameofexecutable]

You can use [file normalize] to get the fully normalized name, too.

file normalize $argv0
file normalize [info nameofexecutable]
你是我的挚爱i 2024-10-02 16:26:08

在我发布问题后几秒钟... lindex $argv 0 是一个很好的起点;-)

seconds after I've posted my question ... lindex $argv 0 is a good starting point ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文