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=Valueinstead 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.