NDESK在VB.NET中进行命令行解析

Pet*_*ete 2 vb.net parameters command-line parsing

虽然我尝试了很多次,但我无法将NDESK.Options解析示例转换为简单的vb.net代码(抱歉,我不是专业版).

他们提供的唯一示例如下:http: //www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html

但是,我不明白代码的这个关键部分:

var p = new OptionSet () {
        { "n|name=", "the {NAME} of someone to greet.",
          v => names.Add (v) },
        { "r|repeat=", 
            "the number of {TIMES} to repeat the greeting.\n" + 
                "this must be an integer.",
          (int v) => repeat = v },
        { "v", "increase debug message verbosity",
          v => { if (v != null) ++verbosity; } },
        { "h|help",  "show this message and exit", 
          v => show_help = v != null },
    }; 
Run Code Online (Sandbox Code Playgroud)

这部分:v => names.Add(v)得到以下vb.net等价:函数(v)names.Add(v),我没有得到.

任何人都可以如此善良并将其发布在更容易理解的命令集中吗?

vic*_*vic 5

以下是NDesk.Options OptionSet对象的上述代码的VB.NET版本.
此代码示例使用集合初始值设定项.

Static names = New List(Of String)()
Dim repeat As Integer
Dim verbosity As Integer
Dim show_help As Boolean = False

Dim p = New OptionSet() From {
 {"n|name=", "the {NAME} of someone to greet.", _
     Sub(v As String) names.Add(v)}, _
 {"r|repeat=", _
     "the number of {TIMES} to repeat the greeting.\n" & _
     "this must be an integer.", _
     Sub(v As Integer) repeat = v}, _
 {"v", "increase debug message verbosity", _
     Sub(v As Integer) verbosity = If(Not IsNothing(v), verbosity + 1, verbosity)}, _
 {"h|help", "show this message and exit", _
     Sub(v) show_help = Not IsNothing(v)}
 }
Run Code Online (Sandbox Code Playgroud)

此代码示例创建OptionSet集合,然后添加调用Add方法的每个选项.另外,请注意,最后一个选项是传递函数的函数指针(AddressOf)的示例.

Static names = New List(Of String)()
Dim repeat As Integer
Dim verbosity As Integer
Dim show_help As Boolean = False

Dim p = New OptionSet()
p.Add("n|name=", "the {NAME} of someone to greet.", _
            Sub(v As String) names.Add(v))
p.Add("r|repeat=", _
            "the number of {TIMES} to repeat the greeting.\n" & _
            "this must be an integer.", _
            Sub(v As Integer) repeat = v)
p.Add("v", "increase debug message verbosity", _
            Sub(v As Integer) verbosity = If(Not IsNothing(v), verbosity + 1, verbosity))
p.Add("h|help", "show this message and exit", _
            Sub(v) show_help = Not IsNothing(v))
' you can also pass your function address to Option object as an action.
' like this:
p.Add("f|callF", "Call a function.", New Action(Of String)(AddressOf YourFunctionName ))
Run Code Online (Sandbox Code Playgroud)

  • 在定义参数时,我缺少的另一件事是`n | name =`.我没有意识到`=`是我需要的,以便有一个有价值的参数. (2认同)