Console Extension Methods 2/2

In the first post on console extension methods I showed how to parse console arguments in an extremely simple way. However this is not quite optimal for formatting exceptions for missing argument exceptions because the distinctions are erased.

Therefore I devised a new way to deal with this where these cases are surfaced as exceptions. It’s pretty simple but nice because since the parameter name is known, it can be used to give very helpful error messages in a catch block. So the idea is to keep the entire console application parsing code in a try block and keep the exceptions separate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tests
{
    using Extensia.Console;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var price = args.Argument<decimal>("price");
            }
            catch (Exception e)
            {
                // we get nice exceptions here a la
                // - argument missing for "speed"
                // - unsupported format for "speed"
                // - format error for "speed"
                Console.WriteLine(e.Message);
            }
        }
    }
}

This is a nice and simple model that is even better than the previous one.

The solution (up on extensia.codeplex.com) – feel free to download and use it.

Advertisements

About xosfaere

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

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