<-- Previous || Up || Next -->

Get Opt Function
String Extras Class

Public Function GetOpt( _
      ByRef rOutArg As String _
    , ByRef rInCmd As String _
    , ByVal vInOpts As String _
    ) As String

"Get Option"
Parse the next argument from a string that represents a series of command line arguments.
Similar to the UNIX "getopt" function for parsing command line options.

Summary: This function returns the letter of the command line argument that was found, or it returns an empty string if there are no command line options left.
       Const strOpts = "d:cn"
       Dim strArg As String
       Dim strCmds As String
       strCmds = "-cd50 -- -SAMPLE.DAT"
    for example
       GetOpt(strArg, strCmds, strOpts) = "c"
       strArg = ""
       strCmds = "-d50 -- -SAMPLE.DAT"
       GetOpt(strArg, strCmds, strOpts) = "d"
       strArg = "50"
       strCmds = "-- -SAMPLE.DAT"
       GetOpt(strArg, strCmds, strOpts) = ""
       strArg = ""
       strCmds = "-SAMPLE.DAT"
See also:
    GetToken Function
    GetVBToken Function
    GetOpt Function (UNIX)
rOutArg: String to which the value of any string argument will be assigned. When this function parses an argument that has a string value, the string value will be assigned to rOutArg. For example, when parsing the argument "-d50", the value "50" would be assigned to rOutArg.
rInCmd: String containing the remaining command line arguments which are to be parsed. This by-reference argument is modified when individual arguments are parsed from this list. The command line arguments in this string can either be boolean values or string values. For example, "-d50 -c SAMPLE.DAT" would represent two command line arguments. The "d" argument has a value of "50" and the "c" argument is True. "SAMPLE.DAT" would be left in the string rOutArg because it does not represent an optional argument.
vInOpts: String describing the valid argument letters. Should contain one or more characters which name the valid arguments. Those arguments which have a string value should be followed by a colon (:). For example, a vInOpts value of "cd:" tells this function that the valid arguments are named "c" and "d", and that the "d" argument will be followed by some string value.

Note: Two hyphens (--) can be used to stop processing command line arguments. For example, with a rInCmd value of "-- -SAMPLE.DAT", this function will return an empty string (signifying that no command line arguments were found) and it will leave "-SAMPLE.DAT" assigned to rInCmd.

Note: Function returns a question-mark if the command line contains an unrecognized argument. Your function which calls GetOpt should probably generate an error in such cases, since GetOpt cannot know how to process the rest of the command line argument phrase containing the invalid argument--if the unknown argument is followed by anything but a space character, GetOpt assumes that those characters represent arguments (not the string value of some unknown string-type argument).
Note: Multiple arguments can be specified following a hyphen, as long as all but possibly the last are boolean/flag type arguments. Only the last argument within in a list can have a string value, because with string arguments, the rest of the word up to the following space are considered part of the argument's string value. For example, assuming a vInOpts of "d:c", the "c" and "d" options could be specified following the same hyphen as in "-cd50" as long as the "d" argument follows the "c" argument since it accepts a string value.

Copyright 1996-1999 Entisoft
Entisoft Tools is a trademark of Entisoft.