Skip to content
Giacomo Stelluti Scala edited this page Aug 7, 2015 · 15 revisions

IMPORTANT NOTE: This wiki refers to latest stables, if a beta version is available in github master branch please refer to Latest Version.

Strict Parsing

If your application cannot recover after a failed parsing, entrust strict parsing.

var options = new Options();
// Parse in 'strict mode', success or quit
if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options))
{
  // Domain logic here
}

This is not only a better formalism. With strict parsing you don't need to define any GetUsage(...), HelpText::AutoBuild(...) will be invoked for you. As for other features, if GetUsage(...) is defined the library will execute it.

If your failing logic differs from default, just use the overload that accepts an Action delegate:

if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options,
  () => {
    ProperlyDisposeYourResources((ex) => Log.Error(ex.Message));
    Environment.Exit(-2);
  }))
{
  // Domain logic here
}

Map Heterogeneous Values

Before Version 1.9.4.201 beta all options non mapped with a short or long name could have been read with ValueListAttribute. If values are of different type and you will handle these individually, embrace ValueOptionAttribute.

class Options
{
  [ValueOption(0)]
  public uint Count { get; set; }

  [ValueOption(1)]
  public double? Size { get; set; }

  [ValueOption(2)]
  public string Content { get; set; }
}

Values mapped with this attribute are mapped to properties correlating input and index order defined in constructor. ValueOptionAttribute can live side by side with ValueListAttribute and takes precedence over the latter.

If an input value cannot be converted to target property, parsing will fail.