Cmd line parsing

anyone have a good cmd line parsing routine for RB / Xojo ?
the one I’m using is ok but not great

[NSProcessInfo arguments]
https://developer.apple.com/documentation/foundation/nsprocessinfo/1415596-arguments

Returns the launch arguments already split into an array of instructions.

Yeah I use that already
Was thinking more like GetOpt but there just dont seem to be many for RB/Xojo world

Maybe I’ll port ArgParser from python

We have app.ArgumentsMBS to get a string array cross platform.

Here’s a basic command line parsing routine I wrote a while back that splits an argument string into an array.

Hope it helps.

Private Function SplitArgumentsString(pArgumentsString As String) as String()
  Const ksp = " "
  Const ksq = "'"
  Const kdq = """"
  Const kbs = "\"
  Const kModeRaw = 0
  Const kModeSingleQuote = 1
  Const kModeDoubleQuote = 2
  
  Dim theResult(-1) As String
  Dim currentMode As Int32
  Dim currentArgument As String
  Dim previousCharacterWasEscapeBackslash As Boolean
  Dim charArray(-1) As String
  Dim count, i As Int32
  Dim theChar As String
  
  currentMode = kModeRaw
  currentArgument = ""
  previousCharacterWasEscapeBackslash = False
  
  charArray = Split(pArgumentsString, "")
  
  count = UBound(charArray)
  For i = 0 To count
    theChar = charArray(i)
    
    If previousCharacterWasEscapeBackslash = False Then
      Select Case theChar
      Case ksp
        'space
        
        'if we are in raw mode we break on a space otherwise append it to the argument
        If currentMode = kModeRaw Then
          theResult.Append(currentArgument)
          
          currentArgument = ""
        Else
          currentArgument = currentArgument + theChar
        End If
      Case kdq
        'double quote
        
        'if we are in raw mode switch to double quote mode
        'if we are in double quote mode switch back to raw mode
        'otherwise append the double quote to the argument
        If currentMode = kModeRaw Then
          currentMode = kModeDoubleQuote
        ElseIf currentMode = kModeDoubleQuote Then
          currentMode = kModeRaw
        Else
          currentArgument = currentArgument + theChar
        End If
      Case ksq
        'single quote
        
        'if we are in raw mode switch to single quote mode
        'if we are in single quote mode switch back to raw mode
        'otherwise append the single quote to the argument
        If currentMode = kModeRaw Then
          currentMode = kModeSingleQuote
        ElseIf currentMode = kModeSingleQuote Then
          currentMode = kModeRaw
        Else
          currentArgument = currentArgument + theChar
        End If
      Case kbs
        'backslash 
        #If TargetWin32 Then
          'win32
          
          'backslashes aren't special on win32 so just append the backslash to the argument
          currentArgument = currentArgument + theChar
        #Else
          'unix
          
          'if we are in single quote mode append the backslash to the argument
          'otherwise flag that the character was an escape backslash as we need to append the next character no matter what it is
          If currentMode = kModeSingleQuote Then
            currentArgument = currentArgument + theChar
          Else
            previousCharacterWasEscapeBackslash = True
          End If
        #EndIf
      Else
        'append the character to the argument
        currentArgument = currentArgument + theChar
      End Select
    Else
      'previous character was an escape backslash
      
      'append this character and reset the flag
      currentArgument = currentArgument + theChar
      
      previousCharacterWasEscapeBackslash = False
    End If
  Next
  
  If Len(currentArgument) > 0 Then
    theResult.Append(currentArgument)
  End If
  
  Return theResult
End Function
1 Like