Command Line Parsing and More | CodeGuru

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$ […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 30, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn 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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.