问题
I have a fairly large VB Script project in which the primary script "includes" a number of "libraries" using the standard trick of reading file contents and running ExecuteGlobal
on them. Some of the libraries are pretty vast and written by various third parties.
I want to use Option Explicit
. If I make it the first line executed, however, some of those libraries blow up. But, if I move the directive to below my list of includes, I encounter the error Expected Statement
on that line. Even more confusing, if Option Explicit
appears at the top of one of the libraries (in the middle of the list of them), all is well. But, I wanted to remove (or comment that out) from any of the libraries and only enforce the restriction in my implementation script.
What's the rule about where Option Explicit
must appear? Does it have to be the first line or not? Why is it kosher for it to not be the first line when I apply it via an "include"? How can I achieve my objective?
Code Examples:
Option Explicit ' CAUSES RUNTIME ERROR IN A LIBRARY
Sub Include( sRelativeFilePath )
Dim oFs : Set oFs = CreateObject("Scripting.FileSystemObject")
Dim sThisFolder : sThisFolder = oFs.GetParentFolderName( WScript.ScriptFullName )
Dim sAbsFilePath : sAbsFilePath = oFs.BuildPath( sThisFolder, sRelativeFilePath )
ExecuteGlobal oFs.openTextFile( sAbsFilePath ).readAll()
End Sub
Include ".\SomeLib.vbs"
Include ".\SomeOther.vbs"
Include ".\YetAnother.vbs"
Vs
Sub Include( sRelativeFilePath )
Dim oFs : Set oFs = CreateObject("Scripting.FileSystemObject")
Dim sThisFolder : sThisFolder = oFs.GetParentFolderName( WScript.ScriptFullName )
Dim sAbsFilePath : sAbsFilePath = oFs.BuildPath( sThisFolder, sRelativeFilePath )
ExecuteGlobal oFs.openTextFile( sAbsFilePath ).readAll()
End Sub
Include ".\SomeLib.vbs"
Include ".\SomeOther.vbs"
Include ".\YetAnother.vbs"
Option Explicit ' CAUSES COMPILATION ERROR
回答1:
What's the rule about where Option Explicit
must appear? Does it have to be the first line or not?
Per the documentation,
If used, the Option Explicit statement must appear in a script before any other statements.
Why is it kosher for it to not be the first line when I apply it via an "include"?
Well, as you said, it's not really "including", you just are loading text and evaluating a separate script at runtime with ExecuteGlobal. It's not substituting the text of the libraries within your script, it's loading and running a separate script. That separate script can have Option Explicit
in it as the first statement, since it's run separately.
How can I achieve my objective?
In order to run with Option Explicit
, you need to ensure all your libraries declare all their variables too. If you're not willing to find the variables names and modify the libraries to declare them, then I don't think you have any other alternatives.
You may be able to just have your main loading-scripts script not use Option Explicit, and anything more complex than that put in your own library that does use Option Explicit. Hopefully your main script is simple enough that it's easily debugged without using Option Explicit on it.
来源:https://stackoverflow.com/questions/49902607/vbscript-option-explicit-line-order-in-relation-to-libraries