Console Extension Methods 1/2

Console applications have one problem that occur in just about every console application: parameter parsing. And so when designing a couple of console applications, I came up with some extension methods to deal with this problem in more or less the easiest possible way there is.

I’m too lazy to explain in detail how it all works, but the code is not complex, so it is quite straightforward to see.

First let’s see how a console application might be called

consolation.exe /optimize /max:13,5

Second, some example code to project the string arguments into well-typed values

class Program
{
    static void Main(string[] args)
    {
        var optimize = args.Argued("optimize");
        var max = args.Argument<decimal>("max");
        // ...
    }
}

I cannot see how one could simplify this much further. It is also possible to just parse out parameters as strings with no type argument. I have an idea though – that’s for the next post.

Extensia.zip – The C# source code

Enjoy 🙂

Advertisements

About xosfaere

Software Developer
This entry was posted in Software, Technical and tagged , , , , , , , , , . Bookmark the permalink.

One Response to Console Extension Methods 1/2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s