Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you access the Value property of a CommandLine Parsed<T>?

How can you access the Value property of a CommandLine Parsed ?

Trying to use the CommandLineParser

The wiki section on Parsing says the instance of T can be access through the Value property ... If parsing succeeds, you'll get a derived Parsed type that exposes an instance of T through its Value property.

But I can't see any Value property on the parserResult, only a Tag ...

ParserResult<Options> parserResult = Parser.Default.ParseArguments<Options>(args);
WriteLine(parserResult.Tag);

And I know I'm missing something as if I debug, I can see the Value property ???

like image 858
SteveC Avatar asked Apr 08 '18 16:04

SteveC


1 Answers

To get to parsed object (or errors in case parsing failed), you can do this:

ParserResult<Options> parserResult = Parser.Default.ParseArguments<Options>(args);
if (parserResult.Tag == ParserResultType.Parsed) {
    var options = ((Parsed<Options>)parserResult).Value;
}
else {
    var errors = ((NotParsed<Options>)parserResult).Errors;
}

That's questionable design, but in general you are not expected to do it like this anyway, expected usage is more like:

Parser.Default.ParseArguments<Options>(args)
  .WithParsed(options => ...)
  .WithNotParsed(errors => ...)IEnumerable<Error>
like image 57
Evk Avatar answered Nov 09 '22 17:11

Evk