command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

Inconsitency with error CS0030: Cannot convert type

Open daryl-williams opened this issue 1 year ago • 2 comments

Hello Folks,

I am having trouble understanding why my code fails to compile with the error:

Usage.cs(127,32): error CS0029: Cannot implicitly convert type 'System.CommandLine.Option<int?>' to 'int?'

The thing is I have another variable of the same type defined just above the line that produces the error, and it does not error out. Here's the code in question edited for brevity:

I Define the options:

var dpiOption = new Option<int?> (new[] { "--dpi" }, "Set image dots per inch."); var resizeOption = new Option<int?> (new[] { "--resize" }, "Resize Image.");

I want to save the values in the struct:

public struct CmdArgs { public int? dpi; public int? resize; } public CmdArgs() { dpi = 0; resize = 0; }

When I complie:

  line 126: `cmdArgs.dpi    = dpiOption;     // This line does not give an error`
  line 127: `cmdArgs.resize = resizeOption;  // This line does give the error`

I have tried casting without any luck:

  `cmdArgs.resize = (int)resizeOption;
    Usage.cs(127,32): error CS0030: Cannot convert type 'System.CommandLine.Option<int?>' to 'int'`

  `cmdArgs.resize = (int?)resizeOption;
    Usage.cs(127,32): error CS0030: Cannot convert type 'System.CommandLine.Option<int?>' to 'int?'`

Any help I can get to help me understand this issue would be greatly appreciated. I am working in a net6.0 environment if that makes any difference. If I can provide more detail please let me know.

Thanks,

Daryl

daryl-williams avatar Jul 26 '23 17:07 daryl-williams

Assuming that you are using https://www.nuget.org/packages/System.CommandLine/2.0.0-beta4.22272.1 and the code is in a handler function that you assigned to the command by calling Handler.SetHandler(this Command, Action<InvocationContext>), you can use invocationContext.ParseResult.GetValueForOption(resizeOption).

SetHandler has IValueDescriptor<T>-based overloads too, but those have already been deleted on the main branch, so I don't recommend using them on 2.0.0-beta4.22272.1 either. Actually, the InvocationContext class has also been deleted, but adapting an application to that change is not much work.

KalleOlaviNiemitalo avatar Jul 27 '23 03:07 KalleOlaviNiemitalo

Hi @KalleOlaviNiemitalo, Thank you for response. Yes, I am using System.CommandLine version 2.0.0-beta4.22272.1 and I am trying to save the option and argument values inside a SetHandler that I have defined as:

rootCommand.SetHandler(
        (dpiOption, resizeOption, rotateOption, grayscaleOption, infoOption, verboseOption, outputFileOption , inputFileListArg) =>
        {
          cmdArgs.dpi        = dpiOption;
          cmdArgs.resize     = resizeOption;
          cmdArgs.rotate     = rotateOption;
          cmdArgs.grayscale  = grayscaleOption;
          cmdArgs.info = infoOption;
          cmdArgs.verbose = verboseOption;
       },
       dpiOption, resizeOption, rotateOption, grayscaleOption, infoOption, verboseOption, outputFileOption, inputFileListArg
);

and I am working my way through the model binding examples at:

https://learn.microsoft.com/en-us/dotnet/standard/commandline/model-binding

but I'm afraid my lack of experience is showing because I'm not sure where to get the context. I guess I need to bind my cmdArgs structure to the options and arguments to get a context? I am also very confused as to why the compiler complained that one variable assignment was an error but another of identical types was not? Seems very strange.

After reworking the code a bit I have somehow managed to get the code to compile , not that it I understand why, but that particular error is not currently a problem. I want to Thank You for your help and suggestions, it helps, and I will keep on trying to better understand the ways of C#.

Regards,

Daryl

daryl-williams avatar Jul 27 '23 16:07 daryl-williams