-1

我有没有抛出任何错误的代码。我使用了 NDesk 选项集并添加了 2 个字符串参数。我可以看到它在 args 中提取了正确的名称。但是当我使用 parse(args) 时,它不会引发错误。所以我假设它正在工作。

我正在尝试检查 p(args) 是真还是假。但我不能使用布尔表达式来List<string>

任何帮助我如何做到这一点。如果 parse 有正确的参数,我想要执行函数。

我的代码是这样的

private static Regex fileNamePattern = new Regex(@"^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}[.]pdf$", RegexOptions.Compiled | RegexOptions.IgnoreCase); 

//missing method name
{
    string inputFile;
    string outputFile;
    var p = new OptionSet() {
        {"i"," pdf file",v=>inputFile=v},{"o","index file with kws",v=>outputFile=v},
    };

    Console.WriteLine($"args length: {args.Length}");
    Console.WriteLine($"args 0: {args[0]}");
    Console.WriteLine($"args 1: {args[1]}");

    p.Parse(args); //I would like to use this if(parse(args))
    {

    }
    // 
}

private static void UpdateImportIndexFile(string inputFile, string outputFile)
{
    using (var dip = File.CreateText(outputFile))
    {
        var match = fileNamePattern.Match(Path.GetFileName(MainFilePath));
        if (match.Success)
        {
            //create text file (outputfile);
        }
    }
}
4

1 回答 1

2

由于p是一个类的实例,并且该parse方法不支持返回以在某种意义上模拟TryParse将您的解析包装在一个try块中的功能

try{
  val = p.Parse(args);
}catch(OptionException e){
  //if false
}

有关更多信息http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#M:NDesk.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})

于 2019-03-18T17:35:13.977 回答