6

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 ???

SteveC
  • 15,808
  • 23
  • 102
  • 173
  • 1
    I'm guessing you need to cast and then you have the value [here](https://github.com/commandlineparser/commandline/blob/master/src/CommandLine/ParserResult.cs#L109). – Nico Schertler Apr 08 '18 at 16:06
  • @NicoSchertler Thanks for responding, but could you give actual code, as I'm not understanding how ??? – SteveC Apr 08 '18 at 16:08

1 Answers1

9

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>
Evk
  • 98,527
  • 8
  • 141
  • 191