gpt4 book ai didi

vb.net - VB.NET 中的 NDESK 命令行解析

转载 作者:行者123 更新时间:2023-12-02 22:44:07 27 4
gpt4 key购买 nike

虽然我尝试了很多次,但我无法将 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),我不明白。

谁能这么好心,把它贴在一组更容易理解的命令中?

最佳答案

这是 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 ))

关于vb.net - VB.NET 中的 NDESK 命令行解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10315830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com