• subscribe
May 07, 2002 12:00 AM

VBScripting Solutions: Take Advantage of Named Arguments

Windows IT Pro
InstantDoc ID #24943
Downloads
24943.zip

VBScripting Solutions

Remember when you first learned how to write scripts? I remember thinking that, all in all, scripting wasn't so hard to master. A few dozen scripts later, I began to reckon that scripting was a bit boring. After I passed the threshold of writing more than a hundred scripts, I faced just one certainty: There has to be a better way to create scripts.

Windows Script Host (WSH) 5.6 can help you find a better way through its expanded support of arguments. WSH has always supported arguments. However, WSH 5.6 has taken this support to the next level.

Let's look at how WSH 5.6 supports arguments compared with earlier versions. All the code that I use requires WSH 5.6. If you're running any other version, go to http://msdn.microsoft.com/downloads/default.asp and navigate to Web Development, Windows Script, Windows Script 5.6. WSH 5.6 is 100 percent backward-compatible, so upgrading is painless.

Arguments in Previous WSH Versions
WSH 5.5 and earlier support arguments only through the WshArguments collection. This collection object holds all the command-line arguments that a script receives from its caller (i.e., the person launching the script). When you launch a WSH script by double-clicking the filename, you're running it without arguments. When you launch a script from the Run dialog box, you can pass arguments to WshArguments. For example, the command

MyScript.vbs A B C

passes three arguments (i.e., A, B, and C) to WshArguments.

The WshArguments object isn't externally creatable. In other words, you can't use the CreateObject method to obtain a reference to it. Instead, you must use a method or property of another object. Specifically, you use the Arguments property of the WScript object (WSH's root object) in code such as

Dim args
Set args = WScript.Arguments

In WSH 5.5 and earlier, WshArguments is an ordinary collection that basically lets you retrieve, count, and list the items it contains. To retrieve an argument, you use WshArguments' Item property. When you use this property, you must include a 0-based index number that specifies the argument's position on the command line. For example, if you want to access argument A in the previous command, you include the index number 0 in code such as

WScript.Arguments.Item(0)

Item is WshArguments' default property. As such, you can omit it in VBScript code. Thus, both of the following MsgBox statements are correct and successfully display the first argument:

Set args = WScript.Arguments
MsgBox args(0)
MsgBox args.Item(0)

Although WshArguments' programming interface might appear powerful, it has some structural limitations. First and foremost, in scripts, you can identify arguments only by their position on the command line. Because of this functional limitation, managing optional arguments requires a lot of effort. Second, to launch a script, all the callers must know all the argument details, including the order of the arguments, the expected values, and the syntax to use. Requiring such close cooperation between scriptwriters and callers can lead to frustration on both sides. On one side, every time the scriptwriters want to change or add an argument to satisfy one caller's needs, they must inform all the callers about the changes. On the other side, scriptwriters' changes can break the existing code. Callers might come in one morning to find that the script they've successfully run many times before is suddenly laden with mysterious errors.

Arguments in WSH 5.6
Like its predecessors, WSH 5.6 lets you access arguments by specifying their position. Unlike its predecessors, WSH 5.6 also lets you access arguments by name, thanks to the addition of named arguments.

A named argument is a command-line argument that you've qualified with a name. You can express a named argument in three formats:

  • String. The String format has the syntax /name:value, where name is the argument's name and value is the argument's string value.
  • Simple. The Simple format has the syntax /name. In this case, the argument has a name and an empty value. This format is useful for arguments that specify an optional condition that's either present or not present. The presence of the argument alone provides enough information for the script, so a value isn't necessary.
  • Boolean. The Boolean format uses the syntax /name\[+|-], where + is true and ­ is false. WSH automatically translates the + and ­ into their respective Boolean values. This format is ideal for arguments that toggle on and off a state that's internal to the script.


ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here