VBScript Option显式行顺序(与库有关) [英] VBScript Option Explicit line order (in relation to libraries)

查看:69
本文介绍了VBScript Option显式行顺序(与库有关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的VB脚本项目,在该项目中,主脚本使用读取文件内容并在其上运行ExecuteGlobal的标准技巧,包括"许多库".一些库非常庞大,由各种第三方编写.

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.

我要使用Option Explicit.但是,如果我将其作为第一行执行的话,其中一些库就会崩溃.但是,如果将指令移动到包含列表的下方,则在该行会遇到错误Expected Statement.更令人困惑的是,如果Option Explicit出现在其中一个库的顶部(在它们列表的中间),那么一切都很好.但是,我想从任何库中删除(或注释掉),而只在实现脚本中强制执行限制.

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.

Option Explicit必须出现在哪里的规则是什么?是否必须是第一行?当我通过"include"应用它时,为什么它不是第一行?我如何实现我的目标?

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?

代码示例:

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 

推荐答案

Option Explicit必须出现在哪里的规则是什么?一定不是第一行吗?

每个

What's the rule about where Option Explicit must appear? Does it have to be the first line or not?

Per the documentation,

如果使用,则Option Explicit语句必须在脚本中出现在任何其他语句之前.

If used, the Option Explicit statement must appear in a script before any other statements.

当我通过"include"应用它时为什么不成为第一行呢?

好吧,正如您所说,它并不是真正的包含",您只是在运行时使用

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.

为了与Option Explicit一起运行,您需要确保所有库也都声明了它们的所有变量.如果您不愿意找到变量名并修改库以声明它们,那么我认为您没有其他选择.

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.

您也许可以只使主要的加载脚本脚本不使用Option Explicit,并且比您自己的确实使用Option Explicit的库中放置的脚本更复杂.希望您的主脚本足够简单,可以在不使用Option Explicit的情况下轻松调试.

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.

这篇关于VBScript Option显式行顺序(与库有关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆