Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Related Posts

Command Line Switch Parser

Posted on April 10th

I didn't write it (Peter Hallam did), but I was just using it and thought "wow, this is cool, it needs to be found by more people!"

If you build console apps that take multiple arguments (generate.exe /vroot:vbasic /target:c:\files\ ... etc.) then this makes it very easy. To use it, just compile the provided code into a library and reference from your own C#, VB.NET, etc. application.

From the readme;

Command Line Argument Parser
----------------------------

Author: peterhal@microsoft.com

Parsing command line arguments to a console application is a common problem.
This library handles the common task of reading arguments from a command line
and filling in the values in a type.

To use this library, define a class whose fields represent the data that your
application wants to receive from arguments on the command line. Then call
Utilities.Utility.ParseCommandLineArguments() to fill the object with the data
from the command line. Each field in the class defines a command line argument.
The type of the field is used to validate the data read from the command line.
The name of the field defines the name of the command line option.

The parser can handle fields of the following types:

- string
- int
- uint
- bool
- enum
- array of the above type

 



Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

NConsoler is an open source library that provides command line parser functionality based on attribute metadata attached to type.
Library is very easy to add and use in your application. NConsoler gives an ability to display help and 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);
}
}

and use it:

program.exe "Maxim" /-flag