我已经开始使用这个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
and 因为两个参数的值都是null
,所以我的内部方法失败了?
我也尝试将选项更改为不需要,然后在控制台窗口中引发错误,这些参数尚未指定,但会触发正确的方法。