Search Tools Links Login

Advanced Command Line parser


This codes stripes a command line; like for example this:
/d "c:program filessample dir with white spacesand more" /s "S100-01" "S200-01" "S300-01" /n 7 /index
Into variables you can use within your applications.
For example;
if you execute
getParmValue("s", valueList)
valueList will contain
S100-01, S200-01 and S300-01
For more detail check the code.

Original Author: R Kluit

Code

'//[CommandLineModule] Basic Module
Option Explicit
Dim strList As String
'//
'// Get items from $command var.
'//
Private Function GetParam(Count As Integer) As String
Dim i As Long
Dim j As Integer
Dim c As String
Dim bInside As Boolean
Dim bQuoted As Boolean
j = 1
bInside = False
bQuoted = False
GetParam = ""
For i = 1 To Len(Command)
c = Mid$(Command, i, 1)
If bInside And bQuoted Then
If c = """" Then
j = j + 1
bInside = False
bQuoted = False
End If
ElseIf bInside And Not bQuoted Then
If c = " " Then
j = j + 1
bInside = False
bQuoted = False
End If
Else
If c = """" Then
If j > Count Then Exit Function
bInside = True
bQuoted = True
ElseIf c <> " " Then
If j > Count Then Exit Function
bInside = True
bQuoted = False
End If
End If
If bInside And j = Count And c <> """" Then GetParam = GetParam & c
Next i
End Function
'//
'// Count items whitin $command var.
'//
Private Function GetParamCount() As Integer
Dim i As Long
Dim c As String
Dim bInside As Boolean
Dim bQuoted As Boolean
GetParamCount = 0
bInside = False
bQuoted = False
For i = 1 To Len(Command)
c = Mid$(Command, i, 1)
If bInside And bQuoted Then
If c = """" Then
GetParamCount = GetParamCount + 1
bInside = False
bQuoted = False
End If
ElseIf bInside And Not bQuoted Then
If c = " " Then
GetParamCount = GetParamCount + 1
bInside = False
bQuoted = False
End If
Else
If c = """" Then
bInside = True
bQuoted = True
ElseIf c <> " " Then
bInside = True
bQuoted = False
End If
End If
Next i
If bInside Then GetParamCount = GetParamCount + 1
End Function
'//
'// Set options allowed at command line switches
'//
Public Function setAllowList(list As String)
strList = "*" & list
End Function
'//
'// Check if commandline is valid
'//
Public Function validateCommandline() As Boolean
Dim dmyArr() As String
If strList = "" Then
Err.Raise 100, "[validateCommandLine]", "AllowList has not been set"
Else
On Error Resume Next
Call getParmValue(" ", dmyArr)
If Err Then
validateCommandline = False
Else
validateCommandline = True
End If
End If
End Function
'//
'// Get Value by given option
'//
Public Function getParmValue(ParmName As String, ReturnValue() As String) As Boolean
Dim i As Integer
Dim j As Integer
Dim strTmp As String
ReDim ReturnValue(0)
ParmName = LCase(ParmName)
For i = 1 To GetParamCount
strTmp = GetParam(i)
If Len(strTmp) >= 2 Then
Select Case Left(strTmp, 1)

Case "-", "/"
strTmp = LCase(Trim(Mid(strTmp, 2)))
If Not strList = "" Then
If InStr(1, strList, "|" & strTmp & "|", vbTextCompare) = 0 Then
Err.Raise 100, "[getParmValue]", "AllowList value mismatch"
End If
End If

If strTmp = ParmName Then

getParmValue = True '//Value Found

For j = i + 1 To GetParamCount
strTmp = GetParam(j)

Select Case Left(strTmp, 1)

Case "-", "/"
i = j - 1
Exit For

Case Else
Call addtoArray(ReturnValue, strTmp)

End Select
Next

Exit Function
End If

End Select
End If
Next
End Function
'//
'// Resize array
'//
Private Function addtoArray(ary() As String, item As String)
ReDim Preserve ary(UBound(ary) + 1)
ary(UBound(ary) - 1) = item
End Function
'//[SampleMain] Basic Module
Sub main()
'// (optional) Set options allowed by the command line options;
'//
'// |s| will allow both -s as /s
'// |index| will allow both -index as /index
'// etc
CommandLineModule.setAllowList ("|s|index|d|n|")
'//
'// (optional) With the limitations set, we can check if the commandline is valid.
'//
MsgBox "Command line is valid: " & CommandLineModule.validateCommandline
'//
'// Variable to store optional values, such as filenames
'//
'// The options /s is followed by several named arguments
'// These values are stored in this array
'//
Dim valueList() As String
If CommandLineModule.getParmValue("s", valueList) Then

'// List the values stored in the array
'//
For i = 0 To UBound(valueList) - 1
MsgBox "-s value: " & valueList(i)
Next
Else
'// Exit app if argument is missing
'//
MsgBox "Missing argument: -s"
Exit Sub

End If
'//
'// Check if value is pressent
If CommandLineModule.getParmValue("n", valueList) Then
If valueList(0) = "" Then
MsgBox "Missing value for option -n"
Else
MsgBox "the -n argument: " & valueList(0)
End If
End If
'//
'// Check if value is pressent
If CommandLineModule.getParmValue("index", valueList) Then
MsgBox "Option index active"
Else
MsgBox "Option index not active"
End If
End Sub
'//[CommandLineModule] Basic Module

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 91 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.