0

I am using the commandline parser to parse 2 verbs. Here is a code snippet:

 [Verb("Option1")]
 public class VerbOption1
 {
     [Option('o', "option1")]
     public string Option1 { get; set; }
 }

 [Verb("Option2")]
 public class VerbOption2
 {
     [Option('t', "option2")]
     public string Option2 { get; set; }
 }

Parser.Default.ParseArguments<VerbOption1, VerbOption2>(args)
 .WithParsed<VerbOption1>(option1 => doSomething(option1))
 .WithParsed<VerbOption2>(option2 => doSomethingElse(option2));

The issue I am having is that I get an error saying that verb 'x' is not recognized. If I only use 1 verb, then it works. But as soon as I add a second verb I get this error.

Can anyone help to clarify what's happening and how I can resolve this error?

Any hep would be much appreciated.

Thanks

tsage080667
  • 89
  • 1
  • 10
  • 1
    I may be off, but have you checked the example on https://github.com/commandlineparser/commandline ? it uses the MapResult method : CommandLine.Parser.Default.ParseArguments(args) .MapResult( (AddOptions opts) => RunAddAndReturnExitCode(opts), (CommitOptions opts) => RunCommitAndReturnExitCode(opts), (CloneOptions opts) => RunCloneAndReturnExitCode(opts), errs => 1); – Laurent Gabiot Dec 03 '21 at 15:41
  • I reviewed the documentation, however, the issue is with the call to ParseArguement . In order to use MapResult, ParseArguement must return a parsed object and it is not. It fails with verb not recognized, before I can call MapResult. – tsage080667 Dec 03 '21 at 15:50
  • Could you also include the code for your "verbs"? VerbOption1 and VerbOption2 – Laurent Gabiot Dec 03 '21 at 15:57
  • Good! you can write a proper answer to your question, rather than putting it in the comments. – Laurent Gabiot Dec 03 '21 at 20:18

1 Answers1

0

I figured out the issue. Make sure the verb attribute assigned to the class has a name. Use that name in the command line followed by your options. That was the issue for me. I was using the commandline like I was still processing a single verb, thereby omitting the verb name. The verb name is required when parsing for multiple verbs.

tsage080667
  • 89
  • 1
  • 10
  • Can you share the code snippet and the CLI command that you got it to work? – Jimmy Vo Mar 24 '22 at 01:57
  • From the commanline, I was calling things this way: myprogram.exe -o somevalue. When in actual fact, I needed to call it this way: myprogram.exe Option1 -o somevalue, because I had more then one possible verb to parse for. – tsage080667 Mar 30 '22 at 20:17