Previous post introduced the Global and Help features of CLAP.
In this post, a new syntax is introduced to handle Help and Empty handlers.
- Source code and binaries at github: https://github.com/adrianaisemberg/CLAP
- NuGet: Install-Package CLAP
In the following example, the Help method is defined as a help handler using the [Help] attribute:
If the input verb is “help” the help handler is triggered and prints the help string to the console.
The help handler must accept a single string argument which will automatically contain the generated help string.
The name of the handler can be different from the method’s name. In the following example, “h” is used instead of “help”.
Like verbs, additional names can be registered for a help handler. In the following example, “help”, “h” and “?” are all registered for printing the help:
Empty handlers are invoked when the exe is executed without any arguments.
In the following example, the “Foo” method is defined as the empty handler:
Notice that no more than one empty handler can be registered for a type. In-addition, the empty handler must not accept any parameters.
The empty handler can be a static or an instance method. If it is an instance method, like verbs, an instance of the type must be passed to the Run method of the parser.
Empty Help Handlers
An empty handler can be defined as a help handler, meaning that when no arguments are passed to the exe, the help is handled:
The method will be invoked also if “help” is passed as an argument.
Notice that an empty help handler has the exception of having a single string parameter, for the help string.
Up until now, a whole set of verbs, parameters and global parameters could be defined using only attributes, so the Main method only included a single call the the parser.
If Help and Empty handlers were needed, they had to be registered using delegates (or lambda expressions).
From now on (version 1.2), Help and Empty handlers can be also defined using attributes, leaving you with the minimum code required for running your app from the Main method.