Handy .NET Console Parameter Parsing

written by tobinharris on October 19th, 2007 @ 09:47 AM

Oh, I recently discovered that .NET gives us a useful library for parsing parameters in a .NET console app.

First, add a reference to System.Configuration.Install to your Console project. Then, you can work with it to parse params quite easily in this format:

c:\> example.exe cmd=loaddata folder="c:\my data" blah=xyz

Check out some example code to see how to use the library. It's quite neat!

I personally prefer this format...

c:\> example.exe --cmd loaddata --folder "c:\my data" --blah xyz

...which System.Configuration.Install doesn't support unfortunately. If anyone knows a way of handlnig this easily, then please let me know.



kick it on DotNetKicks.com



Comments

  • Faye on 22 Jun 09:17

    Looks brilliant! I will give a try!

  • Chuni-Muni on 23 Aug 16:35

    Also, to parse command line arguments you can use NConsoler – open source library. Very easy to add and use this library in your application. NConsoler gives ability to display help validation messages without any line of code.

    http://nconsoler.csharpus.com/

    Example 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);
    }

Post a comment

Options:

Size

Colors