public static void Main(string[] args) {
    Consolery.Run(typeof(Program), args);
}

[Action]
public static void DoWork(
    [Required] string name,
    [Optional(-1)] int count) {
	// ...
}
C:\>program.exe Administrator /count:10
NConsoler is an open source library for building .Net console applications
Download NConsoler - Free
version 1.0

NConsoler 1.0 released

June 18th, 2009

We finally released version 1.0. There are a few bugs fixed that came from our users as well as issues we've found. We are going to continue development and introduce some new cool features.

NConsoler announce

August 03rd, 2008

Usually, parsing of arguments in console applications takes a lot of time. I the internet you can find a few solutions that make it easier to implement this task, however they are not simple and flexible, therefore I decided to design a new system based on metadata and call it NConsoler.

The main idea is to find a way to transform a set of arguments to call a particular method. In case when arguments are wrong, an application must return human understandable error message with error details to provide user friendly behavior of an application. Also it is easy to provide help information using metainfo.

Well, to get necessary method by set of arguments, method should be marked. For this functionality we could use attributes that suit ideally for this job. I implemented an Action attribute:


[Action]
public static void Method(...)...

Also, methods could have necessary and unnecessary arguments to add flexibility in development process. This could be realized including attributes as well as attribute Action for a method:


[Action]
public static void Method(
    [Required] string name,
    [Optional(true)] bool flag)...

As far as we know, method, must take all set of In parameters, so for unnecessary parameters we have to set optional values by default

The last thing to do before run an application, is to design Main method:


public static void Main(params string[] args) {
    Consolery.Run(typeof(Program), args);
}

Type that has method marked as Action, should be passed into Run method and arguments must be passed as well.

Complete source code:


using System;
using NConsoler;

public class Program {
    public static void Main(params string[] args) {
        Consolery.Run(typeof(Program), args);
    }

    [Action]
    public static void Method(
        [Required] string name,
        [Optional(true)] bool flag) {
        Console.WriteLine("name: {0}, flag: {1}", name, flag);
    }
}

Let’s run our application:

> program.exe "Max"  
name: Max, flag: true

or with inversed flag:

> program.exe "Max" /-flag
name: Max, flag: false

Certainly, in definition of Action method, user could make a mistake and in most libraries source of error is not obvious therefore we feel that adequate error message is extremely important, I has pointed this at the begin of the article. Before start an action method, NConsoler checks metainformation as well as parameters of command line and displays detail error message with eventual cause of error. This is continuation of strategy “Design by contract” that fall back usage to open help in time you work with library.