adrianaisemberg/CLAP

[PreVerbExecution] method not called for [Empty] verb

AGlezB opened this issue · 0 comments

This issue can be reproduced always with CLAP version 4.6.0 NuGet package in a .NET FW 4.8 console app.

The docs for [Empty] read: Marks a method to be executed when there is no input. The method must not accept any parameter except if marked along with [Help].
I expect [PreVerbExecution] handler to be called for all the verbs and there is no indication that this is not the case.

Steps to reproduce

  1. Create a new console project and add CLAP 4.6.0 from NuGet. I called mine MyConsole.
  2. Add this code to Program.cs:
static void Main(string[] args) {
    Parser.Run(args, new Program());
}

[Empty]
[Verb]
private void MyVerb() {
    Console.WriteLine("MyVerb");
}

[PreVerbExecution]
private void MyPreVerb() {
    Console.WriteLine("MyPreVerb");
}
  1. Run MyConsole MyVerb and the output is like this:
MyPreVerb
MyVerb
  1. Now run it without arguments MyConsole and the output is like this:
MyVerb

Workaround

Change MyVerb() like so:

//[Empty]
[Verb(IsDefault = true)]
private void MyVerb() {
    Console.WriteLine("MyVerb");
}

Possible solutions

  1. Call [PreVerbExecution] when the method is annotated with both [Empty] and [Verb] but not when it is annotated with [Help]
  2. Call [PreVerbExecution] for all verbs regardless of [Empty]. This is less flexible than # 1.
  3. Add a parameter to control the behavior of [Empty]:
[Empty(IgnorePreVerb = false)]
[Verb]
private void MyVerb() {
    Console.WriteLine("MyVerb");
}

IMO this is the best choice.
4. Clearly document the current behabior.