Advertisement
4_2005-2006 String Manipulation #171148

Get an Array of string from Command$

This function returns an array with the command line arguments, contained in the command$, like cmd.exe does (with %1 , %2 ...) If the function does not succeed, the returned array has Ubound=-1 , like split function in VB.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'===================================================================
' GetCommandArgs - © Nik Keso 2009
'----------------------------------
'The function returns an array with the command line arguments,
'contained in the command$, like cmd.exe does (with %1 , %2 ...)
'If the function does not succeed, the returned array has Ubound=-1 ,
'like split function in VB.
'----------------------------------
'INPUT: comSTR = the string with the arguments (command$)
'OUTPUT: Array of string with the arguments
'===================================================================
Function GetCommandArgs(ByVal comSTR As String) As String()
Dim CountQ As Integer 'chr(34) counter
Dim OpenQ As Boolean ' left open string indicator (ex:  "c:\bbb ccc.bat ) OpenQ=true, (ex:  "c:\bbb ccc.bat" ) OpenQ=false
Dim ArgIndex As Integer
Dim tmpSTR As String
Dim strIndx As Integer
Dim TmpArr() As String

GetCommandArgs = Split("", " ") 'trick to return uninitialized array like split, if function does NOT succeed!
TmpArr = Split("", " ")
comSTR = Trim$(comSTR) 'remove front and back spaces
If Len(comSTR) = 0 Then Exit Function
CountQ = UBound(Split(comSTR, """"))
If CountQ Mod 2 = 1 Then Exit Function 'like cmd.exe , command$ must contain even number of chr(34)=(")
strIndx = 1
Do
  If Mid$(comSTR, strIndx, 1) = """" Then OpenQ = Not OpenQ
  If Mid$(comSTR, strIndx, 1) = " " And OpenQ = False Then
    If tmpSTR <> "" Then 'don't include the spaces between args as args!!!!!
      ReDim Preserve TmpArr(ArgIndex)
      TmpArr(ArgIndex) = tmpSTR
      ArgIndex = ArgIndex + 1
    End If
    tmpSTR = ""
  Else
    tmpSTR = tmpSTR & Mid$(comSTR, strIndx, 1)
  End If
  
  strIndx = strIndx + 1
Loop Until strIndx = Len(comSTR) + 1
ReDim Preserve TmpArr(ArgIndex)
TmpArr(ArgIndex) = tmpSTR
GetCommandArgs = TmpArr
End Function
Original Comments (3)
Recovered from Wayback Machine