带冒号的多个参数

时间:2015-04-01 10:10:40

标签: vbscript

我试图找出如何运行带有多个参数的脚本,但每个参数都需要在它们之间有一个冒号...例如:

setup.exe SERVERNAME:PORT

我想要克服的问题是:

1.问题是用户可以输入多个参数

2.输入的参数必须在它们之间有一个冒号,并且必须完整为SERVERNAME:PORT - 有没有办法做到这一点?

我将下面写成草稿,我不太确定它是否会给我我想要的东西?

argsCount = WScript.Arguments.Count
Set args = Wscript.Arguments

If argsCount < 1 then
    WScript.Echo "Error no arguments selected - usage: script.vbs <SERVERNAME>:<PORT>, <SERVERNAME>:<PORT>, <SERVERNAME>:<PORT> etc"
    WScript.Quit
Else
    For Each arg In args
        setup.exe <IP>:<PORT>
    Next
End If

1 个答案:

答案 0 :(得分:0)

如果您愿意放弃,并使用RegExp:

Option Explicit

Dim args : Set args = WScript.Arguments.Unnamed

If args.Count < 1 then
    WScript.Echo "Error no arguments selected - usage: script.vbs <SERVERNAME>:<PORT> <SERVERNAME>:<PORT> <SERVERNAME>:<PORT> etc"
    WScript.Quit
Else
    Dim r : Set r = New RegExp
    r.Pattern = "^\w+:\d+$"
    Dim a, m
    For Each a In args
        Set m = r.Execute(a)
        If 1 <> m.Count Then
           WScript.Echo "Bingo:", a
        Else
           WScript.Echo "setup.exe", m(0).Value
        End If
    Next
End If

输出:

cscript y.vbs
Error no arguments selected - usage: script.vbs <SERVERNAME>:<PORT> <SERVERNAME>:<PORT> <SERVERNAME>:<PORT> et
c

cscript y.vbs server:123 a:0 0:a "nice try:4711" abc:abc abc:000
setup.exe server:123
setup.exe a:0
Bingo: 0:a
Bingo: nice try:4711
Bingo: abc:abc
setup.exe abc:000

版本0.2:

使用Split(OneAndOnlyArg,“,”)来提供RegExp:

Option Explicit

Dim args : Set args = WScript.Arguments.Unnamed

If args.Count <> 1 then
    WScript.Echo "Error no arguments selected - usage: script.vbs <SERVERNAME>:<PORT>,<SERVERNAME>:<PORT>,<SERVERNAME>:<PORT> etc"
    WScript.Quit
Else
    Dim r : Set r = New RegExp
    r.Pattern = "^\w+:\d+$"
    Dim a, m
    For Each a In Split(args(0), ",")
        Set m = r.Execute(a)
        If 1 <> m.Count Then
           WScript.Echo "Bingo:", a
        Else
           WScript.Echo "setup.exe", m(0).Value
        End If
    Next
End If

输出:

cscript y.vbs server:123,a:0,0:a,abc:abc,abc:000
setup.exe server:123
setup.exe a:0
Bingo: 0:a
Bingo: abc:abc
setup.exe abc:000

请注意,空格会破坏这种方法。