1

There is something I don't understand with the NuGet package CommandLineParser. This is the first time I see this?

Let me show you first:

enter image description here

On the left in my Auto Watch frame I can see the Value property. But, on the right, in Immediate Window I cannot access it. How is this possible. How can I use this package and read my Value Path after Parsing?

result.Value
error CS1061: 'ParserResult<Options>' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'ParserResult<Options>' could be found (are you missing a using directive or an assembly reference?)

What is this watch folder doing I'm not doing?

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

2 Answers2

2

By digging into the source code, you can see that ParserResult<T> is an abstract class..

The WithParsed extension method does a check to see if the ParserResult is a concrete type of Parsed, invokes the delegate and returns it https://github.com/commandlineparser/commandline/blob/master/src/CommandLine/ParserResultExtensions.cs

So now you're working with the abstract base class instead of the implementation, that's why you can't just do result.Value as that property is not sitting on the base class but rather the implementation.

My guess is that the Auto watch can know the actual type and show you the entire object and the Immediate window can't.

To work with .Value, you can cast it to Parsed<Options>

JohanP
  • 5,252
  • 2
  • 24
  • 34
  • This should be marked as the answer for this question. Casting as Parsed does indeed provide access to the .Value in the question. – godfathr Apr 27 '22 at 19:34
0

The exact example of using this CommandLineParser is used in this project: https://www.dropbox.com/s/nhq9os8dd9fim9u/FloorplanTransformation-3D-Walls.rar?dl=0

This is a visual studio project, check that out you will get a better understanding.

By the way here is a brief explanation:

  1. you will have to create an instance of the Parser class.
Parser parser = Parser.Default; 
  1. Then to parse the arguments do the following
PraserResult<object> parser_result = parser.ParseArguments<MeshGenerateOptions, MorphologicalTransformOptions, other options>(args)

Here we have classes MeshGenerateOptions and MorphologicalTransformOptions with Attribute [Verb]

  1. Now do the following to invoke the corresponding callback functions for each parsed arguments
parserResult.WithParsed<MeshGenerateOptions>(VerbHandlers.HandleGenerateMesh);
            parserResult.WithParsed<MorphologicalTransformOptions>(VerbHandlers.HandleMorphologicalTransform);

Here, VerbHandlers is a static class not of much interest, HandleGenerateMesh and HandleMorphologicalTransform are Callback functions that are invoked when the corresponding verb is parsed.

You get the demo of How to use the above project you can watch the tutorial: https://www.youtube.com/watch?v=MNILyflAxdY&t=21s But this is just for building and using the above project.

Ravi Prakash
  • 313
  • 1
  • 8