问题
I mean something like get_included_files(); in php or Error().stack; in javascript or $BASH_SOURCE array in bash ?
I've only found in macros ( https://www.autoitscript.com/autoit3/docs/macros.htm ) @ScriptFullPath and @ScriptName.
回答1:
You can implement the functionality by using a global variable. Say $includeDepth. $includeDepth should be incremented by 1 before any #include and decremented by 1 after it. If $includeDepth is 0, then the code is not running as part of an #include. For example:
Global $includeDepth
$includeDepth+= 1
#include <MyScript1.au3>
#include <MyScript2.au3>
$includeDepth-= 1
; Declarations, functions, and initializations to be run in both "include" and "non-include" modes
If $includeDepth <= 0 Then
; Code for when in "non-include" mode
EndIf
You need to be very consistent in this usage, though. However, as long as library #includes don't include your own scripts (or scripts where this checking is done), there is no need to modify them. From this point of view, it's also not necessary to increment/decrement $includeDepth while including library #includes. However, it doesn't hurt and it reinforces the practice.
回答2:
Yes there is a way. Check if @ScriptName matches the name of the current script. If it doesn't your script has been included in something else. I use this method to run a unit test if an included script is run as a stand alone script. I add the following code to the end of included_script.au3...
; unit test code
if @ScriptName == "included_script.au3" Then
MsgBox(0,"Unit Test","Running unit test...",3)
test()
exit
endif
func test()
; no test defined
EndFunc
When included in a "main.au3" file, the @ScriptName will be set to "main.au3" and it will not run test()
来源:https://stackoverflow.com/questions/29540373/is-there-a-way-in-autoit-script-to-find-if-current-file-was-included-or-it-is-ru