I need to add the ability to a program to accept multiple named parameters when opening the program via the command line. i.e.
program.exe /param1=value /param2=value
and then be able to utilize these parameters as variables in the program. I have found a couple of ways to accomplish pieces of this, but can't seem to figure out how to put it all together.
I have been able to pass one named parameter and recover it using the code below, and while I could duplicate it for every possible named parameter, I know that can't be the preffered way to do this.
Dim inputArgument As String = "/input="
Dim inputName As String = ""
For Each s As String In My.Application.CommandLineArgs
If s.ToLower.StartsWith(inputArgument) Then
inputName = s.Remove(0, inputArgument.Length)
End If
Next
Alternatively, I can get multiple unnamed parameters from the command line using
My.Application.CommandLineArgs
But this requires that the parameters all be passed in the same order/format each time. I need to be able to pass a random subset of parameters each time.
Ultimately, what I would like to be able to do, is separate each argument and value, and load it into a multidimentional array for later use. I know that I could find a way to do this by separating the string at the "=" and stripping the "/", but as I am somewhat new to this, I wanted to see if there was a "preffered" way for dealing with multiple named parameters?
My preference for handling this would be to use an existing library, such as the Command Line Parser Library. (However, by default, it uses a different input format, based around --input=Value
instead of /input=value
.)
This gives you the advantage of not having to write the code yourself, getting a lot of flexibility and robustness, and simplifying your code.
Here is a small function to do what you wanted to do. It allows you to store all parameters in name-value pairs in a structure.
Module Module1
Private Structure NameCommandLineStuct
Dim Name As String
Dim Value As String
End Structure
Private CommandLineArgs As New List(Of NameCommandLineStuct)
Sub Main()
If ParseCommandLine() Then
For Each commandItem As NameCommandLineStuct In CommandLineArgs
Select Case commandItem.Name.ToLower
Case "one"
Console.Write(String.Format("key one is {0}", commandItem.Value))
Case "two"
Console.Write(String.Format("key two is {0}", commandItem.Value))
End Select
Next
End If
End Sub
Function ParseCommandLine() As Boolean
'step one, Do we have a command line?
If String.IsNullOrEmpty(Command) Then
'give up if we don't
Return False
End If
'does the command line have at least one named parameter?
If Not Command.Contains("/") Then
'give up if we don't
Return False
End If
'Split the command line on our slashes.
Dim Params As String() = Split(Command, "/")
'Iterate through the parameters passed
For Each arg As String In Params
'only process if the argument is not empty
If Not String.IsNullOrEmpty(arg) Then
'and contains an equal
If arg.Contains("=") Then
Dim tmp As NameCommandLineStuct
'find the equal sign
Dim idx As Integer = arg.IndexOf("=")
'if the equal isn't at the end of the string
If idx < arg.Length - 1 Then
'parse the name value pair
tmp.Name = arg.Substring(0, idx).Trim()
tmp.Value = arg.Substring(idx + 1).Trim()
'add it to the list.
CommandLineArgs.Add(tmp)
End If
End If
End If
Next
Return True
End Function
End Module
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With