Value parser

Go back


Since raw command line is a string, a parsing is needed to populate options and arguments with non-string values.

Command line parser supports parsing of any custom types via IValueParser<T>.

You may set a value parser for CliOption<T>/CliRepeatableOption<T>/CliArgument<T>/CliRepeatableArgument<T> with UseParser() methods:

var option2 = parser.Option<MyCustomType>("option")
                    .UseParser(new MyCustomTypeValueParser());

Built-in types

You don’t need to set custom value parser if your option have one of the following value types:

Enum types

Built-in enum value parser supports few ways to configure parsing.

See an example below:

public enum MyEnum
{
    // This enum member won't be parsed
    [IgnoreDataMember]
    IgnoreMe,

    // This enum member won't be parsed either
    [CliParserIgnore]
    IgnoreMeToo,

    // This enum member will be parsed as "Value1`
    Value1,

    // This enum member will be parsed as "V2`
    [DataMember(Name = "V2")]
    Value2,

    // This enum member will be parsed as "Value3`
    [DataMember]
    Value3,

    // This enum member will be parsed as "V4`
    [CliParserMember("V4")]
    Value4,

    // This enum member will be parsed as "V4`
    // Note that CliParserMemberAttribute takes priority
    [CliParserMember("V5")]
    [DataMember(Name = "Not_V5")]
    Value5,
}

Enum value parser is case-insensitive, so it will handle Value1, value1 and VALUE1 as the same value.