CLAP v3

CLAP is a magical .NET Command-Line Automatic Parser. It has plenty of features and is extremely easy to use. If you are new to CLAP – please see the short intro here.

CLAP has a new version and it brings some more cool features to the tons of already-existing ones.

New in this version, since v2:

  • Expression Validation
  • Support for Custom Types
  • File-Input
  • Interception
  • Multi-Parsers

Everything is fully documented in CLAP’s official web site:

http://adrianaisemberg.github.com/CLAP


Expression Validation

Until now, validation of parameter values was possible only with either all the built-in validation attributes or by using an implementation of a custom validator. Either way, validation was always against one value and a condition.

For example, if we want the “count” parameter value to be less than 100:

[Verb]
void Print([LessThan(100)]int count, string name)
{
}

But what if there is a dependency between the parameters of a verb?

Take the following verb for example:

[Verb]
void Run(int from, int to)
{
}

If we want “to” to be more than “from”, we can now use the new Expression Validation:

[Verb]
[Validate("to > from")]
void Run(int from, int to)
{
}

Read more about it here.

Support For Custom Types

Until now, CLAP supported only simple types of parameters: strings, numbers, bools, enums, arrays and a little more.

v3 brings support for any type!

Well, not any type, but only those that can be represented as JSON or XML.

Here’s an example for a simple Book type:

class Book
{
    public string Title { get; set; }
    public string ID { get; set; }
    public int PagesCount { get; set; }
}

And a verb that can accept such:

[Verb]
void Order(Book book)
{
}

Now an instance of a book, as a JSON or XML string, can be passed from the command-line directly into the verb!

By-the-way, the Book class can be marked with all kinds of validation attributes. Read more about it here.

But, passing such a string is not so easy from the command-line.

That’s why CLAP now supports:

File-Input

Passing a serialized instance of a class from the command-line is not a good practice.

Any parameter, including simple ones, can receive its input directly from a file.

For example, if we have the following text in a file called “book.txt”:

{
  Title: "The Book",
  ID: "FFA4D2234D",
  PagesCount: "250"
}

We can use the following command to pass it directly into our verb:

myexe order -book@=book.txt

Read more about custom types and file-input here and here.

Interception

Thanks to Kurt Jacobson (kurtaj), CLAP now has a cool new feature called: Interception.

Interception allows us to execute a method before every verb is executed. It also allows calling a method after each verb is executed. It also allows plenty of stuff.

For example, it allows you to write a log of all verbs that are called and even to cancel one if some condition is met:

[PreVerbExecution]
void Before(PreVerbExecutionContext context)
{
    var methodName = context.Method.MethodInfo.Name;

    if (DateTime.Now.Hour == 11)
    {
        context.Cancel = true;

        Log.Write("Cancelled " + methodName);
    }
    else
    {
        Log.Write("Running " + methodName);
    }
}

More information and interaction is available in the context object.

Read more about it here.

Kurt – Thanks!

Multi-Parsers

Until now, a CLAP parser could be constructed only using a single type.

Now a parser can support any number of types.

Not much to say about this, so it is best just to go and see it here.

Until Next Time…

Comments, suggestions, bug-reports, code-contribution, anything – please send them to me, either here at the bottom, directly to my email or from the project home at github – here.

Share, please:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Facebook
  • Tumblr
  • Twitter
This entry was posted in .NET, CLAP, Command-Line and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free