Assertfail

Command line option parsing

24 Apr 2011

NDesk Options

I started writing an argument parser without knowing about: NDesk.Options Where you have the following kind of syntax:

string data = null;
   bool help   = false;
   int verbose = 0;

var p = new OptionSet () {
    { "file=",      v => data = v },
    { "v|verbose",  v => { ++verbose } },
    { "h|?|help",   v => help = v != null },
   };
   List extra = p.Parse (args);

FubuCore

Since this post, there is a new option: Command line in FubuCore.

Powershell

If you’re using powershell it just might be simpler to load your program as an assembly and invoke the classes directly. Looking at stackoverflow there is a command Add-Type. Otherwise you could use standard .net assembly load.

Isop

All of the solutions above are not really what I’m after. Thus I’ve written another argument parser loosely based on SimpleConsole : Isop I didn’t really think views in an console app was that useful. Instead I’ve tried to do some of the plumbing in order for you to write console applications more like this:

static void Main(string[] args)
{
   new Build()
      .Parameter("server",arg=>server=arg)
      .Recognize(typeof(CustomerController))
      .Parse(args)
      .Invoke(Console.Out);

}

There is some simple syntax to extend this. To Set culture, TypeConverter et.c.

So you would invoke the method “Add” on the class “CustomerController” with the arguments id and name:

CustomerConsoleApp.exe Customer Add --id 1234 --name Arne

Tags


Comments

Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.

Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.