1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
Namespace Tools Public Class CommandLineProcessor Private m_Args As New Collection Private m_Flags As New Collection Private m_Params As New Dictionary(Of String, String) Private m_Extras As New Collection Private Const vbQuote As Char = Chr(34) Private Const vbSpace As Char = " "c Private Sub PopulateArgs() Dim commandLine As String = System.Environment.CommandLine ' Replace any escaped double-quotes (\") with a placeholder commandLine = commandLine.Replace("\" & vbQuote, Chr(255)) Dim inQuotes As Boolean = False Dim thisArg As String = "" For Each thisChar As Char In commandLine.ToCharArray If thisChar = vbQuote Then ' is this a beginning or ending quote? inQuotes = Not inQuotes Else If Not inQuotes And thisChar = vbSpace Then ' We're not inside quotes, this is the end of the argument ' Put the escaped double-quotes back, minus the \ thisArg = thisArg.Replace(Chr(255), vbQuote) If Not thisArg = "" Then m_Args.Add(thisArg) thisArg = "" Else ' If we're inside quotes we shouldn't split when we see a space thisArg &= thisChar.ToString End If End If Next ' Put the escaped double-quotes back, minus the \ thisArg = thisArg.Replace(Chr(255), vbQuote) If Not thisArg = "" Then m_Args.Add(thisArg) End Sub Private Sub ParseArgs(ByVal splitParamChars As Char(), Optional ByVal argPrefix As String = "/") Dim argCount As Integer = -1 For Each arg As String In m_Args argCount += 1 ' Arg #0 is the full path to this program so ignore it. If argCount > 0 Then If arg.StartsWith(argPrefix) Then If arg.Contains(splitParamChars) Then 'this is a param Dim argParts As String() = arg.Split(splitParamChars, 2) m_Params.Add(argParts(0), argParts(1)) Else 'this is a flag m_Flags.Add(arg) End If Else 'this is extra text m_Extras.Add(arg) End If End If Next End Sub Public Sub New() Me.New(":") End Sub Public Sub New(ByVal splitParamChars As String, Optional ByVal argPrefix As String = "/") Me.New(splitParamChars.ToCharArray, argPrefix) End Sub Public Sub New(ByVal splitParamChars As Char(), Optional ByVal argPrefix As String = "/") PopulateArgs() ParseArgs(splitParamChars, argPrefix) End Sub Public ReadOnly Property IsFlagPresent(ByVal flagText As String, Optional ByVal caseSensitive As Boolean = False) Get If caseSensitive Then flagText = flagText.ToLower For Each thisItem As String In m_Flags If caseSensitive Then thisItem = thisItem.ToLower If thisItem = flagText Then Return True Next Return False End Get End Property Public ReadOnly Property IsParamPresent(ByVal paramText As String, Optional ByVal caseSensitive As Boolean = False) As Boolean Get If caseSensitive Then paramText = paramText.ToLower For Each thisItem As KeyValuePair(Of String, String) In m_Params Dim compareToThis As String = thisItem.Key If caseSensitive Then compareToThis = compareToThis.ToLower If compareToThis = paramText Then Return True Next Return False End Get End Property Public ReadOnly Property ParamValue(ByVal paramText As String, Optional ByVal caseSensitive As Boolean = False) As String Get If caseSensitive Then paramText = paramText.ToLower For Each thisItem As KeyValuePair(Of String, String) In m_Params Dim compareToThis As String = thisItem.Key If caseSensitive Then compareToThis = compareToThis.ToLower If compareToThis = paramText Then Return thisItem.Value Next Return "" End Get End Property Public ReadOnly Property ExtrasPresent As Boolean Get If m_Extras.Count > 0 Then Return True Else Return False End If End Get End Property Public ReadOnly Property Params As Dictionary(Of String, String) Get Return m_Params End Get End Property Public ReadOnly Property Extras As Collection Get Return m_Extras End Get End Property Public ReadOnly Property Flags As Collection Get Return m_Flags End Get End Property Public ReadOnly Property FullArgList As Collection Get Return m_Args End Get End Property Public ReadOnly Property CommandString As String Get Return System.Environment.CommandLine End Get End Property Public ReadOnly Property ArgCount As Integer Get Return m_Args.Count End Get End Property End Class End Namespace |