Parse Command Line Parameters (VBScript)


/ Published in: Visual Basic
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Function CmdLineParam(AStartAt, AsingleSwitch, AMatchCase, ALongSwitch, AAssigned, APosition, AFollowing)
  2.  
  3. ' AStartAt Which argument position to start at (Zero based).
  4. ' ASingleSwitch single character param char. -a -A
  5. ' AMatchCase singleSwitch should be came case for a match
  6. ' ALongSwitch Long Switch value that is preceed by --. i.e. --test --file=
  7. ' AAssigned Everything following the single switch char, or everything after = in a LongSwitch
  8. ' APosition Which value on the command line this matched
  9. ' AFollowing Value of next parameter on the command line
  10.  
  11. ' Returns true if the parameter was found
  12. Dim Found
  13. Dim c1
  14. Dim count
  15. Dim args
  16. Dim ASign
  17.  
  18. Set args = WScript.Arguments
  19.  
  20. Found = False
  21. c1 = AStartAt
  22. count = args.count
  23.  
  24. While Not found And c1 < count
  25.  
  26. If (Left(args(c1),2)) = "--" Then
  27.  
  28. If UCase(Mid(args(c1),3,Len(ALongSwitch))) = (UCase(ALongSwitch)) Then
  29. Found = True
  30. ASign = InStr(args(c1),"=")
  31.  
  32. If ASign <> 0 Then
  33. AAssigned = Mid(args(c1),ASign+1,Len(args(c1)))
  34. Else
  35. AAssigned = ""
  36. End If
  37. End If
  38.  
  39. ElseIf (Left(args(c1),1)) = "-" Then
  40. If AMatchCase = True Then
  41. ' make sure it matches
  42. If Mid(args(c1),2,1) = ASingleSwitch Then
  43. found = True
  44.  
  45. APosition = c1
  46. AAssigned = Mid(args(c1),3,Len(args(c1)))
  47. If (c1 < count+1) Then AFollowing = args(c1+1)
  48. End If
  49. Else
  50. ' we don't care about case
  51. If UCase(Mid(args(c1),2,1)) = UCase(ASingleSwitch) Then
  52. found = True
  53. APosition = c1
  54. AAssigned = Mid(args(c1),3,Len(args(c1)))
  55.  
  56. End If
  57. End If
  58. End If
  59.  
  60. AFollowing = ""
  61. If Found And (c1 < count-1) Then
  62. AFollowing = args(c1+1)
  63. End If
  64.  
  65. c1 = c1 + 1
  66. Wend
  67.  
  68. CmdLineParam = Found
  69. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.