Fullpath of current TCL script

后端 未结 5 2228
梦如初夏
梦如初夏 2021-02-13 03:49

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

In PHP it would be: __FILE__

相关标签:
5条回答
  • 2021-02-13 04:23

    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

    0 讨论(0)
  • 2021-02-13 04:32

    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.

    0 讨论(0)
  • 2021-02-13 04:36

    You want $argv0

    0 讨论(0)
  • 2021-02-13 04:39

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

    0 讨论(0)
  • 2021-02-13 04:41

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

    file normalize $argv0
    file normalize [info nameofexecutable]
    
    0 讨论(0)
提交回复
热议问题