【问题标题】:Unable to parse simple command line with commandlineparser无法使用 commandlineparser 解析简单的命令行
【发布时间】: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.MissingRequiredOptionErrorCommandLine.UnknownOptionError

MissingRequiredOptionError是因为找不到Url参数而产生的)

那么你知道我的错误在哪里吗?

提前感谢您的帮助;)

【问题讨论】:

    标签: c# command-line-parser


    【解决方案1】:

    所以来自commandlineparser 的最终开发人员说无法解析它。 单行选项必须是一个字符选项。 绕过它的唯一方法是预处理参数。

    我这样做了,它正在工作,但它不允许短选项组合(例如在tar -xvf 中)

    args = args.Select(arg => Regex.IsMatch(arg, "^-\\w{2,}") ? "-" + arg : arg ).ToArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 2012-08-18
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2015-02-20
      相关资源
      最近更新 更多