Handling Multiple Command Line Arguments

Handling multiple command line arguments has always been ugly in VB
especially when some of the arguments are quoted because they
contain spaces and such. For example, if you wanted to write a program
that takes as an argument a filename, you would have to quote the
filename to ensure that a space inside of it does not confuse your
application.

Unfortunately there is no built-in functionality for handling this situation. This is why I wrote two functions: GetParam and GetParamCount
that I use all the time. They both can handle a mix of quoted and unquoted parameters.


public Function GetParam(Count as Integer) as string

    Dim i as Long
    Dim j as Integer
    Dim c as string
    Dim bInside as Boolean
    Dim bQuoted as Boolean

    j = 1
    bInside = false
    bQuoted = false
    GetParam = ""

    for i = 1 to len(Command)

        c = mid$(Command, i, 1)

        If bInside And bQuoted then
            If c = """" then
                j = j + 1
                bInside = false
                bQuoted = false
            End If
        ElseIf bInside And Not bQuoted then
            If c = " " then
                j = j + 1
                bInside = false
                bQuoted = false
            End If
        else
            If c = """" then
                If j > Count then Exit Function
                bInside = true
                bQuoted = true
            ElseIf c <> " " then
                If j > Count then Exit Function
                bInside = true
                bQuoted = false
            End If
        End If

        If bInside And j = Count And c <> """" then _
           GetParam = GetParam & c

    next i

End Function


public Function GetParamCount() as Integer

    Dim i as Long
    Dim c as string
    Dim bInside as Boolean
    Dim bQuoted as Boolean

    GetParamCount = 0
    bInside = false
    bQuoted = false

    for i = 1 to len(Command)

        c = mid$(Command, i, 1)

        If bInside And bQuoted then
            If c = """" then
                GetParamCount = GetParamCount + 1
                bInside = false
                bQuoted = false
            End If
        ElseIf bInside And Not bQuoted then
            If c = " " then
                GetParamCount = GetParamCount + 1
                bInside = false
                bQuoted = false
            End If
        else
            If c = """" then
                bInside = true
                bQuoted = true
            ElseIf c <> " " then
                bInside = true
                bQuoted = false
            End If
        End If

    next i

    If bInside then GetParamCount = GetParamCount + 1

End Function




Comments

  • Optimised version with identical functionality

    Posted by MarkRM on 09/29/2005 02:22pm

    Many thanks for some excellent code! I've improved it a bit with a couple of optimisations.
    
    [code]
    Public Function GetParam(intCount As Integer) As String
        Dim blnInside As Boolean
        Dim blnQuoted As Boolean
        Dim intParamNum As Integer
        Dim intPos As Integer
        Dim strChar As String
    
        intParamNum = 1
    
        For intPos = 1 To Len(Command)
            strChar = Mid$(Command, intPos, 1)
    
            If blnInside Then
                If (blnQuoted And strChar = """") Or (Not blnQuoted And strChar = " ") Then
                    intParamNum = intParamNum + 1
                    blnInside = False
                    blnQuoted = False
                End If
            Else
                If strChar = """" Then
                    If intParamNum > intCount Then Exit Function
                    blnInside = True
                    blnQuoted = True
                ElseIf strChar <> " " Then
                    If intParamNum > intCount Then Exit Function
                    blnInside = True
                End If
            End If
    
            If blnInside And intParamNum = intCount And strChar <> """" Then
                GetParam = GetParam & strChar
            End If
        Next
    End Function
    
    Public Function GetParamCount() As Integer
        Dim blnInside As Boolean
        Dim blnQuoted As Boolean
        Dim intPos As Integer
        Dim strChar As String
    
        For intPos = 1 To Len(Command)
            strChar = Mid$(Command, intPos, 1)
    
            If blnInside Then
                If (blnQuoted And strChar = """") Or (Not blnQuoted And strChar = " ") Then
                    GetParamCount = GetParamCount + 1
                    blnInside = False
                    blnQuoted = False
                End If
            Else
                If strChar = """" Then
                    blnInside = True
                    blnQuoted = True
                ElseIf strChar <> " " Then
                    blnInside = True
                End If
            End If
        Next
        If blnInside Then GetParamCount = GetParamCount + 1
    End Function
    [/code]

    Reply
  • Same is needed for WSH??

    Posted by ltfrebac on 10/04/2004 09:13am

    I need the same funcionality in my wsh scripts, anyone?

    Reply
  • THe DON IS BACK

    Posted by Legacy on 08/16/2002 12:00am

    Originally posted by: simon eduamah

    great!
    

    Reply
  • VB

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: kumar

    this site vb command very useful me. So u can send more command my e-mail ID.

    Reply
  • Brilliant!

    Posted by Legacy on 07/22/2002 12:00am

    Originally posted by: Dom

    After a long time trying to write my own functions, and then searching for code to do this, I finally found your code - and it works perfectly!

    Thank you!

    Reply
  • command line with Variables ????

    Posted by Legacy on 12/10/2001 12:00am

    Originally posted by: Retype

    i need to do more with the command line !!
    i have to extract variables out out of it

    for example -o c:\ -i c:\windows -p *.mp3
    so i can use them in my programm as variables

    can s.o. help me ??

    • An easy way to handle variables in command line

      Posted by GemunuRW on 02/18/2005 01:38am

      You can try following logic to achieve what you want
      
      '-o indicates output Directory 
      '-i indicates input directory
      '-p indicates file category
      ' for example -o c:\ -i c:\windows -p *.mp3
      
      
      Sub Main()
      
         Dim stringArgs() As String
         Dim outDirectory As String
         Dim InDirectory As String
         Dim fileCategory As String
         
         Dim n As Integer
         
         stringArgs= Split(Trim(Command$), "-")
         For n = LBound(stringArgs) To UBound(stringArgs)
            Select Case Mid(LCase(stringArgs(n)), 1, 1)
            Case ""
              ' continue
            Case "o"
              ' output Directory
              outDirectory = Trim(Mid(stringArgs(n), 3))
              
            Case "i"
              ' input directory
              InDirectory = Trim(Mid(stringArgs(n), 3))
              
            Case "p"
              ' file category
              fileCategory = Trim(Mid(stringArgs(n), 3))
              
            End Select
            
         Next n
      
      End Sub

      Reply
    Reply
  • Excellent

    Posted by Legacy on 11/28/2001 12:00am

    Originally posted by: Steven

    It is really useful! Thank you.

    Reply
  • Excellent Work

    Posted by Legacy on 10/17/2001 12:00am

    Originally posted by: Kenneth

    Very Helpful!!!

    Reply
  • Thanks!

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

    Originally posted by: Sergey Podalov

    Thanks a lot! ;)

    Reply
  • Implementing GetParams and GetParamCount

    Posted by Legacy on 08/30/2001 12:00am

    Originally posted by: Nathan Fowler

    Dim params As Integer
    Dim x As Integer
    params = GetParamCount
    MsgBox params
    For x = 1 To params
    MsgBox GetParam(x)
    Next


    This simply displays the parameter count and each individual command-line parameter entered. From this snippet you can easily adapt it to your application.

    Kudos to the author of these two fuctions, they are quite useful.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds