验证 vbScript 参数名称 [英] Validate vbScript argument names
问题描述
我正在从 PowerShell 调用 vbScript,参数在文本字符串中定义,我想验证所使用的参数是否正确.假设我的有效参数是 ARG1、ARG2 &ARG3,并且有人有一串 "/ARGG1:one/ARG2:two"
,我想根据一组有效参数验证命名参数并捕获 <强>ARGG1.我失败的地方是获取命名参数的名称.例如,我可以使用 WScript.Arguments.Named("ARG1")
引用特定的命名参数.但我似乎无法遍历 WScript.Arguments.Named 并返回使用的实际参数名称.像这样
I am calling a vbScript from PowerShell, with arguments that are defined in a text string, and I want to validate that the arguments being used are correct. Say my valid arguments are ARG1, ARG2 & ARG3, and someone has a string of "/ARGG1:one /ARG2:two"
, I want to validate the named arguments against an array of valid arguments and catch that ARGG1.
Where I am failing is in getting the names of the named arguments. I can reference a particular named argument with WScript.Arguments.Named("ARG1")
for example. But I can't seem to loop through WScript.Arguments.Named and return the actual argument names used.
Something like this
For Each Argument In WScript.Arguments.Named
msgBox Argument.name
Next
推荐答案
当迭代 Named
参数集合时,您访问的是键 (argument name) 而不是 WshNamed
对象本身.如果您将密钥传递回集合,您将能够访问该命名参数的值.
When iterating the Named
argument collection you are accessing the key (argument name) not the WshNamed
object itself. If you pass the key back into the collection you will be able to access the value for that Named Argument.
Dim key
For Each key In WScript.Arguments.Named
Dim argument: argument = WScript.Arguments.Named.Item(key)
Call WScript.Echo(key) 'Return argument name
Call WScript.Echo(argument) 'Return argument value
Next
输出:
ARG1
one
ARG2
two
这篇关于验证 vbScript 参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!