当前 TCL 脚本的完整路径
是否有可能获取当前正在执行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据“当前执行 TCL 脚本”的含义,您实际上可能会寻求
信息脚本
,甚至可能info nameofexecutable< /code>
或者更深奥的东西。
Depending on what you mean by "currently executing TCL script", you might actually seek
info script
, or possibly eveninfo nameofexecutable
or something more esoteric.检索当前语句所在文件名的正确方法是这样的(真正相当于 PHP/C++ 的
__FILE__
):Psuedocode(工作原理):
set thisFile < ;value>
:将变量 thisFile 设置为value
dict getfile
:从字典返回文件值infoframe<#>
:返回一个字典,其中包含有关指定堆栈级别的帧的信息(#
),0
将返回最新的堆栈帧注意:有关信息框架的更多信息,请参阅帖子末尾。
在这种情况下,从
信息框架
返回的文件
值已经标准化,因此不需要文件标准化
。info script
和info frame
之间的区别主要在于与 Tcl 包一起使用。如果在 package require (require package
) 期间提供的 Tcl 文件中使用info script
,则info script
将返回当前正在执行的 Tcl 脚本的路径,并且不会提供包含info script
命令的 Tcl 文件的实际名称;但是,此处提供的信息框架示例将正确返回包含该命令的文件的文件名。如果您想要当前正在评估的脚本的名称,那么:
如果您想要最初调用的脚本(或解释器)的名称,那么:
如果您想要最初调用的可执行文件的名称,那么:
更新 - 详细信息:
信息框架
这是 Tcl 中堆栈跟踪的样子。
frame_index
向我们展示了信息帧 $frame_index
的值从0
到[ info frame ]
的样子。调用
infoframe [ infoframe]
在功能上等同于infoframe0
,但使用0
当然更快。实际上只有
1
到[ info frame ]
堆栈帧,而0
的行为类似于[ info frame ]
。在此示例中,您可以看到0
和5
(即[ info frame ]
)是相同的:请参阅:
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__
):Psuedocode (how it works):
set thisFile <value>
: sets variable thisFile tovalue
dict get <dict> file
: returns the file value from a dictinfo frame <#>
: returns a dict with information about the frame at the specified stack level (#
), and0
will return the most recent stack frameNOTICE: See end of post for more information on info frame.
In this case, the
file
value returned frominfo frame
is already normalized, sofile normalize <path>
in not needed.The difference between
info script
andinfo frame
is mainly for use with Tcl Packages. Ifinfo script
was used in a Tcl file that was provided durring a package require (require package <name>
), theninfo script
would return the path to the currently executing Tcl script and would not provide the actual name of the Tcl file that contained theinfo script
command; However, theinfo 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:
If you want the name of the script (or interpreter) that was initially invoked, then:
If you want the name of the executable that was initially invoked, then:
UPDATE - Details about:
info frame
Here is what a stacktrace looks like within Tcl. The
frame_index
is the showing us whatinfo frame $frame_index
looks like for values from0
through[ info frame ]
.Calling
info frame [ info frame ]
is functionally equivalent toinfo frame 0
, but using0
is of course faster.There are only actually
1
to[ info frame ]
stack frames, and0
behaves like[ info frame ]
. In this example you can see that0
and5
(which is[ info frame ]
) are the same:See:
https://github.com/Xilinx/XilinxTclStore/blob/master/tclapp/xilinx/profiler/app.tcl#L273
您想要
$argv0
You want
$argv0
您也可以使用 [file normalize] 来获取完全规范化的名称。
You can use [file normalize] to get the fully normalized name, too.
在我发布问题后几秒钟...
lindex $argv 0
是一个很好的起点;-)seconds after I've posted my question ...
lindex $argv 0
is a good starting point ;-)