Command Line Parsing and More
Posted
by Mike Poulin
on January 30th, 2004
Entry (position,txt,delim) Numentries (txt,delim) ParseCommandLine ()
The Entry function is usefull in many other areas. If you had a string that was "a/b/c/d" and you wanted to find out what the 3rd entry was you would use:
x$ = entry(3,"a/b/c/d","/") ' x$ would = "c". x% = NumEntries("a/b/c/d","/") ' x% = 4.
Run the ParaCommandLine from the main Form to set variables or actually run the various requests from the command$.
Add the following code to a module called ENTRY.BAS.
public Function Entry(byval Position as Integer, byval Txt as string, _
byval Delim as string) as string
'This function is the same as Progress' Entry function
on error resume next
Dim CurrentPosition as Integer, StrStart as Integer, _
StrEnd as Integer, StrCntr as Integer
StrStart = 1
CurrentPosition = 1
Txt = Txt & " " 'add a space to the end of the string
for StrCntr = 1 to len(Txt)
If mid(Txt, StrCntr, 1) = Delim Or StrCntr = len(Txt) then
StrEnd = StrCntr - 1
If StrEnd - StrStart < 0 then
Entry = ""
StrStart = StrCntr + 1
If CurrentPosition = Position then Exit Function
else
Entry = Trim(mid(Txt, StrStart, StrEnd - StrStart + 1))
If CurrentPosition = Position then Exit Function
StrStart = StrCntr + 1
End If
CurrentPosition = CurrentPosition + 1
End If
next
End Function
public Function NumEntries(byval Txt as string, _
byval Delim as string) as Integer
on error resume next
Dim StrCntr, DelimCntr as Integer
DelimCntr = 1
for StrCntr = 1 to len(Txt)
If mid(Txt, StrCntr, 1) = Delim then DelimCntr = DelimCntr + 1
next StrCntr
NumEntries = DelimCntr
End Function
The following sub can be placed on any form (usually them main form/module).
public Type Parse
Param1 as Integer
Param2 as string
Param3 as Boolean
... how ever many parameters you will have
End Type
public CommandLine as Parse
public Sub ParseCommandLine()
Dim ThisParam as Integer
Dim CCntr as Integer
Dim CVCntr as Integer
Dim StartCmnd as Integer
Dim ParamType as string
Dim ParamValue as string
on error resume next
for ThisParam = 2 to NumEntries(Command$, "/")
ParamType = Trim(Entry(1, Entry(ThisParam, _
Command$, "/"), " "))
'Find the entire parameter value (upto next / or
'end of command$)
ParamValue = ""
for CCntr = 1 to len(Command$)
If mid(Command$, CCntr, len(ParamType)) = ParamType then
StartCmnd = CCntr
for CVCntr = CCntr + len(ParamType) to len(Command$) + 1
If mid(Command$, CVCntr, 1) = "/" Or CVCntr = _
len(Command$) + 1 then
ParamValue = Trim(mid(Command$, StartCmnd + len(ParamType), _
CVCntr - StartCmnd - len(ParamType)))
Exit for
End If
next CVCntr
Exit for
End If
next CCntr
'set variables
Select Case UCase((ParamType))
Case "PARAM1"
' set THE CommandLine.xxxxx variable type here or
' perform operation
Case "PARAM2"
' set THE CommandLine.xxxxx variable type here or
' perform operation
Case "PARAM3"
' set THE CommandLine.xxxxx variable type here or
' perform operation ... HOW EVER MANY PARAMETERS YOU HAVE
End Select
next ThisParam
End Sub

Comments
There are no comments yet. Be the first to comment!