2

尽管我尝试了很多次,但我无法将 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 等效项:Function(v) names.Add (v),我不明白。

任何人都可以如此友善并将其发布在一组更易于理解的命令中吗?

4

1 回答 1

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 ))
于 2012-08-16T20:55:01.500 回答