More about script arguments.

In this section we will discuss several more details about script arguments. First of all we would like to mention method ShowUsage() of the WScript.Arguments object. This method makes a script self-documenting by displaying information about how it should be used. This method takes no arguments and prints information about the correct usage of the script.

When you run the ShowUsage method, a help screen (referred to as the usage) appears and displays details about the script's command line options. This information comes from the runtime section of the *.WSF file. Everything written between the <runtime> and </runtime> tags is pieced together to produce what is called a "usage statement." The usage statement tells the user how to use the script. The general syntax of the <runtime> section is:

<runtime>
    <description>Description text</description>
    <named attributes />
    <unnamed attributes />
    <example>Example Text</example>
	...
</runtime>
where The example below shows how to create a runtime section for a script that requires one named argument usage and three unnamed arguments (files) and also may take one optional named argument logo.
<runtime>
 <description>
  This is a small example that illustrates how the ShowUsage() method works.
 </description>
 <named
   name = "usage"
   helpstring = "Please don't show the usage"
   type = "string"
   required = "true"
 />
 <named
   name = "logo"
   helpstring = "Display or not the logo"
   type = "bolean"
   required = "false"
 />
 <unnamed 
   name = "outfile"
   helpstring = "output file"
   type = "string"
   required = "true"
 />
 <unnamed
   name = "file"
   helpstring = "file to search into"
   type = "string"
   required = 2
 />
 <example>Example: usage.wsf test.log /usage:none /logo+</example>
</runtime>

Now, when we know how to pass named and unnamed arguments to a script, we need to know how to get information about all script arguments inside the script. We have talked about the Arguments property if the WScript object. We mentioned that this property is an instance of the WshArguments object. This object itself has two properties that help us to retrieve all named and unnamed parameters. These properties are:

The following example show how to access all unnamed arguments of a script and some named arguments:
var args = WScript.Arguments;
var unnamed = args.Unnamed;
var str = "\n------------------------\nUnnamed arguments:\n";
for(i=0;i<unnamed.length;i++)
   str += unnamed.Item(i) + "\n";
str += "\n------------------------\nNamed arguments:\n";
str += "logo = " + args.Named.Item("logo") + "\n";
str += "usage = " + args.Named("usage") + "\n";
WScript.Echo(str);

Assignment: please combine these two examples together in one .wsf file and check ho wit works. Once you are done with this you can take a look at the complete example here.


References