Skip to content
This repository was archived by the owner on Jul 11, 2018. It is now read-only.

Latest Beta

cosmo0 edited this page Nov 25, 2014 · 21 revisions

Version 2.0 beta is now released.

At first I suggest you to refer to the unit tests of Parser class to check how it's used.

Options

Version 2.0 uses only two attributes to describe option syntax, Option and Value·

Option works much like in previous version but it can be applied to scalar or sequence value (IEnumerable<T>).

When applied to sequences you can define also Min and Max properties to define a range of values.

Value resembles previous ValueOption and ValueList. Like new Option attribute can be applied to sequences and now supports Required property.

Values are partitioned using index. Let's see an example:

class Options {
  [Value(0)]
  public int IntValue { get; set; }

  [Value(1, Min=1, Max=3)]
  public IEnumerable<string> StringSeq { get; set; }

  [Value(2)]
  public DoubleValue { get; set; }
}

As long as you supply values they will be set to corresponding properties:

$ app 10 str1 str2 str3 1.1

If you omit Min and Max constraint, all available values will be captured by the sequence. So there's no sense to define Value attribute with higher index after a sequence value without range constraint:

class Options {
  [Value(0)]
  public int IntValue { get; set; }

  [Value(1)]
  public IEnumerable<string> StringSeq { get; set; }

  // all values captured by previous specifications,
  // this property will never receive a value
  [Value(2)]
  public DoubleValue { get; set; }
}

Parsing

Parsing is easy as writing one line of code:

var result = Parser.Default.ParseArguments<Options>(args)

This is a basic scenario (without verbs); here we're using the default pre-built instance to parse input arguments using rules engraved in Options type (as described above).

The result variable (here defined used implicit typing) is of type ParserResult<T> or better ParserResult<Options> in this specific case.

ParserResult<T> is a monadic type that exposes an instance of T through its Value property. When its Errors sequence (IEnumerable<Error>) is empty, than Value is properly loaded with parsed values.

Even if it's possible examine the errors sequence, it's recommended quit with appropriate error code. The library help subsystem will print out usage and error descriptions automatically. The parser default instance is configured to output this text to Console.Error.

TODO: to complete