【发布时间】:2020-08-18 15:20:45
【问题描述】:
我必须解析一个强制命令行,该命令行混合了单/双破折号 args、args 与 = 或 作为分隔符等(不是很合适......)
命令行类似于myexe.exe -Url=https://blabla.com/ --TestValue=val1 -monoapp -Ctx "default ctx" Open -Test2=CO0ZJP6f-ca
我正在尝试使用commandlineparser 来做到这一点。 (我想它能够做到这一点,对吧?)
所以首先我尝试解析第一个参数 (myexe.exe -Url=https://blabla.com/)。
public class CommandLineOptions
{
[Option("Url", Required = true)]
public string Url { get; set; }
}
....... In another file but in the same assembly
static void Main(string[] args) // args[0] = "-Url=https://blabla.com/"
{
var commandLineOptions = new CommandLineOptions();
var parseResult = Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(result => commandLineOptions = result);
System.Console.WriteLine(parseResult.Tag); //NotParsed
System.Console.WriteLine(commandLineOptions.Url);
}
使用该代码,我有 2 个错误 CommandLine.MissingRequiredOptionError 和 CommandLine.UnknownOptionError。
(MissingRequiredOptionError是因为找不到Url参数而产生的)
那么你知道我的错误在哪里吗?
提前感谢您的帮助;)
【问题讨论】: