ArgumentList is a command-line program argument parser that exposes named items (i.e. /name=value or -name:value) and unnamed items in an easy to use collection format. I invariably rely on this utility when witting command-line tools. It’s usage is strait-forward, simply construction one with the array of arguments:

    string[] rawArgs = new string[] { "-x=y", "noname", "/novalue" };
    ArgumentList args = new ArgumentList(rawArgs);

    //"x" is defined and has value of "y":
    Assert.IsTrue(args.Contains("x"));
    Assert.AreEqual("y", args["x"].Value);
    //"/novalue" is defined:
    Assert.IsTrue(args.Contains("novalue"));
    //"noname" is not a switch:
    Assert.IsFalse(args.Contains("noname"));
    Assert.AreEqual(1, args.Unnamed.Count);
    Assert.AreEqual("noname", args.Unnamed[0]);

Then you can simply index the collection by name or use any of the other standard IDictionary methods to access it’s contents. The only additional item would be the ‘Unnamed’ collection that allows access to the un-switched arguments (not preceeded by ‘/’ or ‘-’). For more usage examples see the test code (and coverage!).

View the code at:

http://csharptest.net/browse/src/Shared/ArgumentList.cs

Updated: If your looking for a complete command-line solution that supports parsing arguments, displaying usage help, and invocation of commands see the article entitled:
Using the CommandInterpreter.

Comments