Option Parsing: The Option Parsing Pipeline

There are three main phases during option parsing:

  1. Lexing
  2. Parsing
  3. Validation

The Lexing phase deals with the escaping and quoting of special characters and splitting the command line string into a sequence of strings. The Parsing phase evaluates the sequence of strings from the lexing phase, and interprets them as options and arguments; this includes parsing arguments as necessary, e.g., converting a string argument “3” into the numeric argument value 3. The Validation phase determines if the options and arguments represent a valid command for the program to perform.

The Nito.KitchenSink.OptionParser library does not have a lexer, but does have a parser and hooks for validation. The easiest way to use the library is by calling a single method:

var options = OptionParser.Parse<MyOptionArguments>();

This single method wraps all the phases of the option parsing pipeline:

  1. The command line for the process is lexed using the default .NET lexing.
  2. The option and argument definitions are inferred from properties and attributes on the MyOptionArguments type.
  3. These definitions are used to parse the lexed command line, saving the results into properties on a default-constructed MyOptionsArguments object.
  4. Validation is performed on the MyOptionsArguments object, which is then returned.

Future posts will show how each of these steps may be configured (or replaced).