Matthew Johnson

command Line Utilities

Latest Stable Version - 1.2

Command Line Utilities is a library which provides advanced command line argument parsing for .NET Framework 2.0 programs along with automatic usage information generation and a command line progress bar. All are presented in an easy to use and extensible package, fully commented and with a help document. Here is an example of building and using an argument parser in C#:

//We build up each argument separately, and then add them to the
//parser. First we'll build some data arguments, which take
//arguments themselves.
 
DataArgument foo = new DataArgument("foo", 1, "arg0 foos");
 
 
//Another kind of argument is a switch argument, one which can
//have several distinct values, much like an enumeration
 
SwitchArgument bar = new SwitchArgument("bar");
bar.AddOption("foo", "Bars with foo");
bar.AddOption("frob", "Bars with frob");
 
 
//The final kind of argument is a flag, a boolean on/off
//switch. If you want to implement your own arguments,
//look at the IArgument interface.
 
FlagArgument baz = new FlagArgument("baz", "use baz when fooing");
 
 
//Now we build up the parser.
ArgumentParser parser = new ArgumentParser();
parser.AddArgument(foo);
parser.AddArgument(bar);
parser.AddArgument(baz);
 
 
//The parser will return a Dictionary containing key/value
//pairs corresponding to the arguments it knows. If there
//is a problem, it will throw an exception whose Message
//property will describe what is wrong.
 
Dictionary<string,object> options;
try
{
options = parser.Parse(args);
}
catch (Exception e)
{
Console.Error.WriteLine("{0}\n", e.Message);
Console.Error.WriteLine(parser.Usage());
Environment.Exit(1);
}
string fooArg = (string)options["foo"];
string barSwitch = (string)options["bar"];
if(barSwitch == "foo" && (bool)options["baz"])
Console.WriteLine("Frobnosticate");

Revision Information

Version 1.2 (3/23/2007)

  • Added the IntArgument and Double Argument Types
  • Added the Pinwheel type
  • Fixed various small bugs, completed the documentation

Version 1.1 (2/14/2007)

  • First public release
Snapshot