2012-04-25 153 views
1

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

他们提供的唯一的例子可以在这里找到: 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), 我没有得到。

任何人都可以如此善良,并将其张贴在一个更容易理解的命令集?

回答

4

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

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)) 
+0

皮特,这个答案是否适合你? – vic 2012-10-16 21:19:04

+0

我用这个,它工作,99%。我错过的关键是'p.Parse(args)'。出于某种原因,我认为这个库自动工作,它知道参数会被传入并自动解析。 – guanome 2013-08-13 15:13:04

+0

我定义参数时缺少的另一件事是'n | name ='。我没有意识到'='是我需要的,为了有一个有价值的论证。 – guanome 2013-08-13 15:48:26