Handling Multiple Command Line Arguments
Posted
by Konstantin Komissarchik
on February 5th, 2004
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:22pmMany 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]ReplySame is needed for WSH??
Posted by ltfrebac on 10/04/2004 09:13amI need the same funcionality in my wsh scripts, anyone?
ReplyTHe DON IS BACK
Posted by Legacy on 08/16/2002 12:00amOriginally posted by: simon eduamah
ReplyVB
Posted by Legacy on 07/24/2002 12:00amOriginally posted by: kumar
this site vb command very useful me. So u can send more command my e-mail ID.
ReplyBrilliant!
Posted by Legacy on 07/22/2002 12:00amOriginally 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!
Replycommand line with Variables ????
Posted by Legacy on 12/10/2001 12:00amOriginally 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 ??
-
ReplyAn easy way to handle variables in command line
Posted by GemunuRW on 02/18/2005 01:38amYou 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 SubReplyExcellent
Posted by Legacy on 11/28/2001 12:00amOriginally posted by: Steven
It is really useful! Thank you.
Reply
Excellent Work
Posted by Legacy on 10/17/2001 12:00amOriginally posted by: Kenneth
Very Helpful!!!
Reply
Thanks!
Posted by Legacy on 09/13/2001 12:00amOriginally posted by: Sergey Podalov
Thanks a lot! ;)
ReplyImplementing GetParams and GetParamCount
Posted by Legacy on 08/30/2001 12:00amOriginally 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.
ReplyLoading, Please Wait ...