Click here to Skip to main content
6,595,444 members and growing! (19,197 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Console Programming     Intermediate License: The Code Project Open License (CPOL)

NConsoler - command line parser library for .NET

By Maxim Tihobrazov

Command line parser based on meta information.
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5), Dev
Posted:8 Sep 2008
Views:11,247
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.66 Rating: 4.32 out of 5
1 vote, 8.3%
1

2
1 vote, 8.3%
3
2 votes, 16.7%
4
8 votes, 66.7%
5

Introduction

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

Using the code

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

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

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

Also, methods could have required and optional arguments to add flexibility in the development process. This could be realized by including attributes as well as using the Action attribute for a method:

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

As far as we know, Method must take a set of input parameters, and for optional parameters, we have to set optional values, by default.

The last thing to do before running the application is to design a Main method:

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

The type that has a method marked as Action should be passed into the Run method and the arguments must be passed as well.

The complete source code is available here:

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 /flag
name: Max, flag: true

And now, with the inversed flag:

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

Certainly, in the definition of the Action method, the user could make a mistake, and in most libraries, the source of the error is not obvious; therefore, an adequate error message is extremely important. I pointed this out at the beginning of the article. Before starting an action method, NConsoler checks meta-information as well as the parameters of the command line and displays a detailed error message with the eventual cause of error. This is a continuation of the “Design by contract” strategy.

More information can be found on the NConsoler site.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Maxim Tihobrazov


Member
I am developing web applications since 2002, and develop .net applications (web, web-services, win, win-services) since 2004
Occupation: Software Developer (Senior)
Company: Csharpus
Location: Ukraine Ukraine

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberJoeWirtley14:24 18 Feb '09  
GeneralIdea for extension: Define multiple Actions PinmemberMichael B. Hansen2:08 24 Nov '08  
GeneralYou might not have seen this PinmemberdB.10:42 28 Sep '08  
GeneralAbility to determine if an optional parameter was specified Pinmembere800512:19 16 Sep '08  
GeneralRe: Ability to determine if an optional parameter was specified PinmemberMaxim Tihobrazov12:45 16 Sep '08  
GeneralRe: Ability to determine if an optional parameter was specified [modified] Pinmembere800513:21 16 Sep '08  
GeneralRe: Ability to determine if an optional parameter was specified PinmemberMaxim Tihobrazov1:18 17 Sep '08  
GeneralRe: Ability to determine if an optional parameter was specified Pinmembere80055:04 17 Sep '08  
GeneralVery cool idea PinmemberJean-Paul Mikkers5:24 9 Sep '08  
GeneralRe: Very cool idea PinmemberEgorRubansky1:01 10 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Sep 2008
Editor: Smitha Vijayan
Copyright 2008 by Maxim Tihobrazov
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project