Command Line Parsing and More

The following code will simplify command line parsing. The Functions and Subs are:

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

  • Or use only that single pure vb fkt. Can handle long filenames, too.

    Posted by Legacy on 09/02/2003 12:00am

    Originally posted by: Nighty

    Sub SplitCmdLine(cmdline As String, arg1 As String, remain As String)
    
    'split cmdLine to first argument and a rest arg. string
    'Can handle LongDir/Filenames in Quotes now.
    ' Example: Cmd Line = "C:\Program Files\My Exe.exe" "C:\Temp\Con fig.xml" /B /W
    ' -------> arg1 = C:\Program Files\My Exe.exe and remain= "C:\Temp\Config.xml" /B /W
    Dim cmdstr As String
    Dim nPos1 As Integer
    Dim nPos2 As Integer

    cmdstr = Trim(cmdline)
    arg1 = ""
    remain = ""

    If Len(cmdstr) > 0 Then
    nPos1 = InStr(cmdstr, Chr(34)) + 1
    If nPos1 = 2 Then ' ASC(34) = "
    ' " found begin at pos 1
    nPos2 = InStr(nPos1, cmdstr, Chr(34))
    If nPos2 > 0 Then ' ASC(34) = "
    ' Found End "
    arg1 = Mid(cmdstr, nPos1, nPos2 - nPos1)
    remain = Mid(cmdstr, nPos2 + 2)
    Exit Sub
    End If
    End If

    If InStr(cmdstr, " ") = 0 Then
    ' Argument not in quotes, so we can search for a space separator.
    arg1 = cmdstr
    remain = ""
    Else
    arg1 = Left(cmdstr, InStr(cmdstr, " ") - 1)
    remain = Trim(Mid(cmdstr, InStr(cmdstr, " ") + 1))
    End If
    End If

    End Sub

    Reply
  • Commandline Parsing the next step

    Posted by Legacy on 08/02/2003 12:00am

    Originally posted by: Paul Manthey

    Without using any references in vb there is another way of parsing the command line like in the original article by Mike.
    
    

    First change in the sub ParseCommandLine the line

    for ThisParam = 2 to NumEntries(Command$, "/")

    to

    for ThisParam = 2 to NumEntries(Command$)

    This is done for using option signs "/" and "-".

    The sub NumEntries changes to

    Public Function NumEntries(ByVal Txt As String) As Integer
    Dim StrCntr, DelimCntr As Integer
    Dim InQuotes As Boolean, ParseChar As String

    On Error Resume Next

    DelimCntr = 1
    For StrCntr = 1 To Len(Txt)
    ParseChar = Mid(Txt, StrCntr, 1)
    If ParseChar = Chr(34) Then
    InQuotes = Not InQuotes
    ElseIf Not InQuotes Then
    If ParseChar = "/" Or ParseChar = "-" Then _
    DelimCntr = DelimCntr + 1
    End If
    Next

    NumEntries = DelimCntr
    End Function

    The variable InQuotes avoids to take into account option signs in quotes like in "c:\Programme\first-file.txt" or in "/root/bin/config".

    Last the sub Entry variates this method for treating a missing quote at the end of the command line - which is normally allowed.

    Public Function Entry(ByVal Position As Integer, _
    ByVal Txt As String, ByVal Delim As String) As String

    Dim CurrentPosition As Integer, StrStart As Integer
    Dim StrEnd As Integer, StrCntr As Integer
    Dim ParseChar As String, Found As Boolean

    On Error Resume Next

    StrStart = 1
    CurrentPosition = 1
    Txt = Txt & " "

    For StrCntr = 1 To Len(Txt)
    ParseChar = Mid(Txt, StrCntr, 1)
    If ParseChar = Chr(34) Then
    StrCntr = InStr(StrCntr + 1, Txt, Chr(34))

    ' next quote or end of text
    If StrCntr = 0 Then
    StrCntr = Len(Txt)
    ParseChar = Mid(Txt, StrCntr, 1)
    End If
    End If

    If Delim = "/" Then
    Found = (ParseChar = "/" Or ParseChar = "-")
    Else
    Found = (ParseChar = Delim)
    End If

    If Found 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

    Hope this will help some of you.
    Paul

    Reply
  • CommandLine Parsing

    Posted by Legacy on 07/04/2003 12:00am

    Originally posted by: Askhat Sexenali

    There is small issue in your way of parsing command line parameters. In case when user has to specify long filename, he has to use spaces there inside double quotes.

    Here is another way of doing parsing using regular expressions. Do not forget to add reference to "Microsoft VBScript Regular Expressions 5.5" library.

    Parameter position in Entry function starts from 0.

    Function Entry (position as Integer, txt as String) as String
    Dim r As New VBScript_RegExp_55.RegExp
    Dim mc As VBScript_RegExp_55.MatchCollection
    r.Pattern = "(""?.*?""?)(?:\s|$)"
    Set mc = r.Execute(txt)
    Entry = mc(position).SubMatches(0)
    End Function

    Function NumEntries(txt as String) as Integer
    Dim r As New VBScript_RegExp_55.RegExp
    Dim mc As VBScript_RegExp_55.MatchCollection
    r.Pattern = "(""?.*?""?)(?:\s|$)"
    Set mc = r.Execute(txt)
    NumEntries = mc.Count
    End Function

    Reply
  • Try This Instead

    Posted by Legacy on 06/11/2003 12:00am

    Originally posted by: Shawn B

    Great Job, but this can be done in VB much easier.
    
    

    You can call this with:

    numEntries% = ParseCommandLine(ParamPosition%, sParamIWant$)

    - or -

    numEntries% = ParseCommandLine()


    In a module or form, add this:


    Public Function ParseCommandLine(Optional lPos As Long, Optional vPosValue As Variant) As Long
    Dim vArray As Variant
    Dim vParse As Variant
    Dim lCnt As Long

    lCnt = 0

    vArray = Split(Command$, "/", -1)

    For Each vParse In vArray
    lCnt = lCnt + 1

    Select Case lCnt
    Case 1
    ' set variable here or perform operation i.e. var1 = vParse
    Case 2
    ' set variable here or perform operation i.e. var2 = vParse
    Case 3
    ' set variable here or perform operation i.e var3 = vParse
    End Select

    If (lPos = lCnt) Then
    vPosValue = vParse
    End If

    Next

    ParseCommandLine = lCnt

    End Function

    Reply
  • Thank you! :)

    Posted by Legacy on 03/08/2003 12:00am

    Originally posted by: crc12

    i'm making a IDFConverter
    using your code ;) thankyou

    Reply
  • Nice

    Posted by Legacy on 01/28/2003 12:00am

    Originally posted by: adi

    is working!!!

    Reply
  • CommandLine Parsing

    Posted by Legacy on 03/13/2001 12:00am

    Originally posted by: Bharat Tamhankar

    Thank you very much. It was really helpful...

    Reply
  • Passing parameters to DLL as query string from browser

    Posted by Legacy on 01/22/2001 12:00am

    Originally posted by: Saiprasad

    hi,
    I have a doubt, is it possible to a DLL from a browser and pass parameters to the DLL as query string.

    thks.
    saiprasad

    Reply
  • THANKS!!!!!!!!

    Posted by Legacy on 12/21/2000 12:00am

    Originally posted by: Vidman

    Sweeeeeet! Thanks! This helps me out SOOOO much! :]

    -=d�n=-
    http://vidman.ca

    Reply
  • Thank you, thank you

    Posted by Legacy on 03/23/2000 12:00am

    Originally posted by: Gratefull User

    For the code that saved my skin:
    Yhank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you very much.

    Andreas

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Some of the statistics about the growth of enterprise mobility are surprising, not the least of which is the 4X increase in mobile malware from 2013 to 2014. Many employees today not only expect to be able to use their own personal devices at work, but will even contravene policies that restrict personal device use for work.  IT must find the right approach that balances the benefits of mobility with the risks involved.  This on-demand webcast offers steps toward developing a winning mobile strategy, and …

Most Popular Programming Stories

More for Developers

RSS Feeds