Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.4 KiB

  1. Attribute VB_Name = "CmdLineOptions"
  2. Option Explicit
  3. ' i_strCommand is the command line.
  4. ' i_strOptionList is a comma separated list of options that we are looking for, eg
  5. ' "h,?,help" will look for /h, /?, /help
  6. Public Function OptionExists( _
  7. ByVal i_strCommand As String, _
  8. ByVal i_strOptionList, _
  9. ByVal i_blnIgnoreCase As Boolean _
  10. ) As Boolean
  11. Dim arrCmdOptions() As String
  12. Dim arrOptions() As String
  13. Dim intCmdIndex As Long
  14. Dim intOptionIndex As Long
  15. Dim strOption As String
  16. Dim strCmdWord As String
  17. arrCmdOptions = Split(i_strCommand)
  18. arrOptions = Split(i_strOptionList, ",")
  19. For intOptionIndex = LBound(arrOptions) To UBound(arrOptions)
  20. strOption = "/" & arrOptions(intOptionIndex)
  21. If (i_blnIgnoreCase) Then
  22. strOption = LCase$(strOption)
  23. End If
  24. For intCmdIndex = LBound(arrCmdOptions) To UBound(arrCmdOptions)
  25. If (arrCmdOptions(intCmdIndex) <> "") Then
  26. strCmdWord = arrCmdOptions(intCmdIndex)
  27. If (i_blnIgnoreCase) Then
  28. strCmdWord = LCase$(strCmdWord)
  29. End If
  30. If (strOption = strCmdWord) Then
  31. OptionExists = True
  32. Exit Function
  33. End If
  34. End If
  35. Next
  36. Next
  37. OptionExists = False
  38. End Function
  39. ' i_strCommand is the command line.
  40. ' i_strOptionList is a comma separated list of options that we are looking for, eg
  41. ' "i,input" will look for /i or /input. When one is found, the next word from
  42. ' the command line is returned. Multiple words enclosed in double quotes are
  43. ' considered a single word. The quotes are stripped.
  44. ' Eg GetOption("/abc def /ghi ""Hello World""", "ghi", True) returns Hello World
  45. Public Function GetOption( _
  46. ByVal i_strCommand As String, _
  47. ByVal i_strOptionList, _
  48. ByVal i_blnIgnoreCase As Boolean _
  49. ) As String
  50. On Error GoTo LErrorHandler
  51. Dim arrOptions() As String
  52. Dim intIndex As Long
  53. Dim strRemainingCmdLine As String
  54. Dim strCurrentChar As String
  55. Dim strCurrentWord As String
  56. Dim strTerminatingChar As String
  57. Dim strOption As String
  58. Dim intPos As Long
  59. Dim blnOptionFound As Boolean
  60. strRemainingCmdLine = Trim$(i_strCommand)
  61. arrOptions = Split(i_strOptionList, ",")
  62. Do While (strRemainingCmdLine <> "")
  63. strCurrentChar = Mid$(strRemainingCmdLine, 1, 1)
  64. Select Case strCurrentChar
  65. Case """"
  66. strTerminatingChar = """"
  67. Case Else
  68. strTerminatingChar = " "
  69. End Select
  70. ' Where does this word end? Start is 2, because we don't want
  71. ' the search to stop at the current character.
  72. intPos = InStr(2, strRemainingCmdLine, strTerminatingChar)
  73. If intPos = 0 Then
  74. intPos = Len(strRemainingCmdLine) + 1
  75. End If
  76. If (strTerminatingChar = """") Then
  77. ' Eat the terminating double quotes too.
  78. intPos = intPos + 1
  79. End If
  80. strCurrentWord = Mid$(strRemainingCmdLine, 1, intPos - 1)
  81. strRemainingCmdLine = Trim$(Mid$(strRemainingCmdLine, intPos))
  82. If blnOptionFound Then
  83. If strCurrentChar = "/" Then
  84. ' strCurrentWord represents the next option. The desired option
  85. ' was not followed by any non-option word.
  86. GetOption = ""
  87. Else
  88. If (strTerminatingChar = """") Then
  89. ' Strip out the enclosing double quotes
  90. GetOption = Mid$(strCurrentWord, 2, Len(strCurrentWord) - 2)
  91. Else
  92. GetOption = strCurrentWord
  93. End If
  94. End If
  95. Exit Function
  96. End If
  97. If (i_blnIgnoreCase) Then
  98. strCurrentWord = LCase$(strCurrentWord)
  99. End If
  100. For intIndex = LBound(arrOptions) To UBound(arrOptions)
  101. strOption = "/" & arrOptions(intIndex)
  102. If (i_blnIgnoreCase) Then
  103. strOption = LCase$(strOption)
  104. End If
  105. If (strOption = strCurrentWord) Then
  106. blnOptionFound = True
  107. End If
  108. Next
  109. Loop
  110. Exit Function
  111. LErrorHandler:
  112. GetOption = ""
  113. End Function