Parsing customization
Sometime the functionality that is provided out of the box is not enough and needs to be tuned. argparse allows customizing of every step of command line parsing:
Pre-validation – argument values are validated as raw strings.
Parsing – raw argument values are converted to a different type (usually the type of the receiving data member).
Validation – converted value is validated.
Action – depending on a type of the receiving data member, for example, it can be an assignment of converted value to a data member, or appending value if type is an array.
In case if argument does not have any value to parse, then the only one step is involved in parsing:
Action if no value – similar to Action step above but without converted value.
Each of the steps above can be customized with UDA modifiers below.
Pre-validation
PreValidation modifier can be used to customize the validation of raw string values. It accepts a function with one of the following signatures:
bool validate(string value)Result validate(string value)bool validate(string[] value)Result validate(string[] value)bool validate(RawParam param)Result validate(RawParam param)
Parameters:
value/paramvalues to be parsed.
Return value:
true/Result.Successif validation passed orfalse/Result.Errorotherwise.
Parsing
Parse modifier allows providing custom conversion from raw string to a typed value. It accepts a function with one of the following signatures:
ParseType parse(string value)ParseType parse(string[] value)ParseType parse(RawParam param)void parse(ref ParseType receiver, RawParam param)bool parse(ref ParseType receiver, RawParam param)Result parse(ref ParseType receiver, RawParam param)
Parameters:
value/paramraw (string) values to be parsed.receiveris an output variable for parsed value.
Return value for functions that return bool or Result (in other cases parsing is always considered successful):
true/Result.Successif parsing was successful orfalse/Result.Errorotherwise.
Validation
Validation modifier can be used to validate parsed value. It accepts a function with one of the following signatures:
bool validate(ParseType value)Result validate(ParseType value)bool validate(Param!ParseType param)Result validate(Param!ParseType param)
Parameters:
value/paramcontains a value returned fromParsestep.
Return value:
true/Result.Successif validation passed orfalse/Result.Errorotherwise.
Action
Action modifier allows customizing a logic of how "destination" should be changed when argument has a value in command line. It accepts a function with one of the following signatures:
void action(ref T receiver, ParseType value)bool action(ref T receiver, ParseType value)Result action(ref T receiver, ParseType value)void action(ref T receiver, Param!ParseType param)bool action(ref T receiver, Param!ParseType param)Result action(ref T receiver, Param!ParseType param)
Parameters:
receiveris a receiver (destination) which is supposed to be changed based on avalue/param.value/paramhas a value returned fromParsestep.
Return value:
true/Result.Successif operation was successful orfalse/Result.Errorotherwise.
Example
All the above modifiers can be combined in any way: