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

时间:2012-04-25 12:40:44

标签: vb.net parsing command-line parameters

虽然我尝试了很多次,但我无法将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 },
    }; 

这部分:v => names.Add(v)获得以下vb.net等价物: 功能(v)names.Add(v), 我得不到。

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

1 个答案:

答案 0 :(得分: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)}
 }

此代码示例创建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 ))