vb.net 解析多个命名的命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13463744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:13:35  来源:igfitidea点击:

Parse multiple named command line parameters

vb.netcommand-line

提问by scholzr

I need to add the ability to a program to accept multiple named parameters when opening the program via the command line. i.e.

我需要向程序添加在通过命令行打开程序时接受多个命名参数的能力。IE

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?

最终,我希望能够做的是将每个参数和值分开,并将其加载到多维数组中以备后用。我知道我可以通过在“=”处分离字符串并去除“/”来找到一种方法来做到这一点,但由于我对此有点陌生,我想看看是否有一种“首选”的处理方式有多个命名参数?

回答by Reed Copsey

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.)

我处理这个问题的偏好是使用现有的库,例如命令行解析器库。(但是,默认情况下,它使用不同的输入格式,基于--input=Value而不是/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.

这为您提供了不必自己编写代码、获得很大的灵活性和健壮性以及简化代码的优势。

回答by Andrew Neely

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