Thursday, 10 January 2013

Command Line Parser Library verb commands

Hence we're finally here. Command Line Parser Library supports verb commands. This was a feature promised some time ago to the community and now with help of contributors is finally implemented.
It allows you to extend the grammar supported by your application. Anyway when you switch to version Beta one small change is required to your code (and it doesn't relate to verbs).

Short name given to options is now modeled on a character rather than a string constrained to one single character. Community asked for it and after all this is a minor breaking change that made the API cleaner.

It impacts following options definition:
that should become:
So it will not  makes you lose sleep...
Coming back to verb commands, these allow you to define a very familiar syntax to *nix world. If you use git, you probably typed
$ git commit -a
a lot of times or for example
$ zpool status tank
to request ZFS storage pool status in Solaris or SmartOS.

We are at the point; backing to git example commit is the verb command and -a is a switch (in our jargon a BaseOptionAttribute derived type that maps an ordinary option). It applies only to the preceding verb command. If you have another verb, say add, that does not support -a option, parsing will fail.

The following class define options for the verb command named add:
And this is the master options class that must be passed directly to the parser:
It is interesting to note that the parser can create instances for you as these are defined with a parameterless constructor. If this is not the case or you want initialize types by your own (e.g. using a Inversion of Control / Dependency Injection framework) you're free to do that; the library will not overwrite an existing object instance.

As final thought remember that to use the facilities provided by HelpText::AutoBuild you still need that master class and now its child types inherit from CommandLineOptionsBase (this thing should change in a very near release).
Following snippet illustrates how to call the helper method in a verb scenario:
I hope this new feature give you even more freedom when designing command line syntax for your brand new application or important upgrade!
Last thing, don't forget to have fun while programming...

No comments:

Post a Comment