Search

Advertising

Home Downloads Components

Components

Folder Path: \ Components \

File: DOM CommandLine Parser

file.png
Uploaded:
April.15.09
Modified:
November.30.09
File Size:
13 KB
Downloads:
605
Version
1.0

A command line parser that doesn't try to interpret the command line for you like so many other "parsers", but presents it to your code in an object model comparable to how XML DOM parsers do. Highly efficient code that can cope with huge command line strings. Consistently handles any exotic or broken command line you can think of.

Details

Sometimes, you still find yourself writing command line utilities, even in .NET. If you need a small program that can be integrated into any build process, be it MSBuild, NAnt or Make, the command line is the most versatile interface you can design for.

Now there are already various command line parsing libraries for .NET out there, so why add another?

First and foremost, because the parsers I've found all hack together all parts of command-line processing into one single big soup of code. Compare this with XML: if you combined XML parsing, interpretation and response all into one tightly-integrated codebase, it would become a big mess. Instead, you've got an XML parser, an XML binder and your data structures, all separate things. My goal for this command line parser therefore was to at least separate the command line parsing code from the binding code.

Second, the parsers I've found either had no unit tests or were far from achieving 100% test coverage.

Third, nearly all parsers used a bunch of regular expressions that covered most cases you will encounter, but still showed some quirks when malformed or unusual forms of arguments were provided on the command line.

Design Goals

My design goals for the new parser were:

  • The parser should merely provide programmer-friendly access to the command line, without any interpretation or binding code woven into it (basically a command line DOM that can be used to parse and format command line documents)
  • Complicated command lines should be parseable. This includes quoted arguments, initiators in quoted arguments, assignments and modifiers
  • Malformed command lines should be parsed with best effort
  • It should be easy to implement dedicated binding code and interpreters on top of the parser

Examples

Using the parser to extract parameters from a command line is pretty straightforward

// Parse an example command line
CommandLine cmd = CommandLine.Parse(
  "-test somefile.txt \"this is a test\" --option=\"some value\""
);

// Print out some informations we've parsed
Console.WriteLine("Number of arguments: " + cmd.Arguments.Count);
Console.WriteLine("  Name of argument 1: " + cmd.Arguments[0].Name);
Console.WriteLine("  Value of argument 2: " + cmd.Arguments[1].Value);
Console.WriteLine("  Initiator in argument 4: " + cmd.Arguments[3].Initiator);

It can also be used to construct command lines programmatically

CommandLine cmd = new CommandLine();

cmd.AddValue("hello world");
cmd.AddOption("modifier");
cmd.AddAssignment("foo", "bar");

Console.WriteLine("Formatted command line: " + cmd.ToString());

Features

  • 100% unit test coverage for everything
  • Recognizes command line switches in Unix and Windows format
  • Consistent behavior even for the most exotic argument formats
  • Robust parsing of broken and malformed command lines
  • Fast and efficient parsing without using regular expressions

The command line parser is part of the Nuclex.Support library from the Nuclex Framework. You can find the most recent release of the code on the framework's CodePlex site: http://nuclexframework.codeplex.com/



Joomla Template by Joomlashack