-1

I have code that is not throwing any error. I have used NDesk option set and added 2 string Parameters. I can see it has pulled correct names in args. But when I uses parse(args) it is not throwing an error. So I am assuming it is working.

I am trying to check if p(args) is true or false. But I can not use bool expressions to List<string>.

Any help how I can accomplish that. I want execute function if parse has correct arguments.

My code is like this

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);
        }
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user2897967
  • 337
  • 2
  • 8
  • 24

1 Answers1

2

Since p is an instance of a class and the parse method does not support a return to emulate in a sense the functionality of a TryParse wrap your parse in a try block

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

For more information http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#M:NDesk.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})

CoderJoe
  • 447
  • 3
  • 25
  • What are you *talking* about? The class does not [have](http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html) such a method. Did you read the question or is this just a text template to paste into any question with the word "Parse"? Who *upvoted* this three times? – nvoigt Mar 18 '19 at 19:01
  • Parse and TryParse are native to c# @nvoigt https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netframework-4.7.2 – CoderJoe Mar 18 '19 at 19:06
  • But you do realize, that `p` in this case is not an `Int32` right? It's not even a static class name, but a class instance of a class called `OptionSet`. – nvoigt Mar 18 '19 at 19:08
  • I see I will update my answer to reflect that @nvoigt – CoderJoe Mar 18 '19 at 19:12