Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Argument --help

Status
Not open for further replies.

humbletech99

Programmer
Nov 22, 2005
155
GB
I have written some VBScript in wsf and am looking for a good way to handling the arguments. I know that the XML named arguments can be a little helpful, really just for free help usage, but I don't want the program to just be able to handle /h and /help but also unix style arguments like -h and --help.

I figure I will have to write this myself in VBS, but I was wondering if anyone had a good way of doing this, short of having to have 2 separate sections, one for /help style processing and one for --help or -h style processing.

What I have thought of so far is doing the named arguments first as usual, and then doing a separate loop though all WScript.Arguments for the -h and --help style arguments, but considering that I want each /option to have a corresponding -option and --option style as well, this means that there will be a lot of duplicate lines which strikes me as not the best way of attacking the problem or maintaining the code if I changes things.

Q1. Does anyone have a better way of handling all these arguments together to avoid duplication?

There are 2 other bugs I have found in the default way of handling arguments with VBS/Wsf:

Q2. When I type
Code:
cscript scriptname.wsf /option1:something /randominvalidoption
I notice that it doesn't catch the /randominvalidoption and just silently ignores it. I would like an unrecognized option to trip the help usage. So far all I can think of is again cycling through all arg in a case select or something.

Q3. I have changed the default action for the WSF file type to be the Console Based Windows Script Host, but when running
Code:
scriptname.wsf /arg1 /option1:something
it does not pass the options and arguments to the script. This is odd really, since even if I were to run
Code:
wscript scriptname.wsf /arg1 /option1:something
it actually does pass the arguments. What's even more strange is that if I put WSF back to the Windows Based Script Host by default, and run it again, it passes all the arguments.

I can't quite figure out this "feature" since running either cscript scriptname.wsf.... or wscript scriptname.wsf .... does pass arguments to the script...

So,to summarize my questions:

Q1. What's the best way of handling /help or /server:NAME as well as --help or --server NAME style arguments?

Q2. How to catch unrecognized switches to print usage and exit?

Q3. Why do args not get passed when called scriptname.wsf without a preceding scripthost program, but only when cscript is the default?
 
I don't use wsf's and this may not be what you're looking for, but may give you some ideas...

Code:
Option Explicit

Dim bolDispHelp : bolDispHelp = False
Dim arrOptions : arrOptions = Array("h", "help")

Dim objDictOptions : Set objDictOptions = CreateObject("Scripting.Dictionary")
objDictOptions.CompareMode = vbTextCompare

Dim colNamedArgs : Set colNamedArgs = WScript.Arguments.Named
Dim strOption, intCount, strTemp
For Each strOption In arrOptions
   If colNamedArgs.Exists(strOption) Then bolDispHelp = True ' test for named args
   For intCount = 1 To 2
       strTemp = String(intCount, "-") & strOption 'build strings for unnamed args
       If Not objDictOptions.Exists(strTemp) Then objDictOptions.Add strTemp, "" ' add to dictionary
   Next
Next

Dim colUnNamedArgs : Set colUnNamedArgs = WScript.Arguments.Unnamed
Dim strArg
For Each strArg In colUnNamedArgs
   If objDictOptions.Exists(strArg) Then bolDispHelp = True ' check dictionary for match
Next

If bolDispHelp Then WScript.Echo "some help stuff"

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
yes some manual looping and processing, this is what I thought. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top