【发布时间】:2021-03-11 08:12:34
【问题描述】:
我已经开始使用这个https://github.com/commandlineparser/commandline 将解析的输入参数传递给我的应用程序。
我的问题是传入的输入参数不是必需的,这意味着您可以在不指定它们的情况下启动应用程序。
到目前为止,我已经定义了我的命令行选项
public class CommandLineOptions
{
[Option(longName: "client-id", Required = false, HelpText = "Id of the client")]
public string ClientId { get; set; }
[Option(longName: "pw", Required = false, HelpText = "pw.")]
public string Password{ get; set; }
}
我主要是这样解析它们的
Access access= Parser.Default.ParseArguments<CommandLineOptions>(args)
.MapResult(parsedFunc: (CommandLineOptions opts) => new Access(opts.ClientId, opts.Password),
notParsedFunc: (IEnumerable<Error> a) => new Access());
如果指定,我想使用parsedfunc:,如果没有指定,我想使用notParsedFunc: 。
但这总是触发parsedFunc,并且由于两个参数的值都是null,我的内部方法失败了吗?
我也尝试将选项更改为不需要,然后在控制台窗口中抛出一个错误,这些参数没有被指定,但触发了正确的方法。
【问题讨论】:
-
由于建议阅读文档是违反网站规则的...根据参数名称,它看起来像
nonParsedFunc只会在出现错误的情况下被调用(可能会找到您尚未定义的参数)和不是在缺少可选参数时...
标签: c# .net command-line-parser