dotnet / dotnet/command-line-api
InvalidOperationException ReadOnlySpan<int> cannot be bound
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I am able to reproduce it when I am trying to use use an optional decimal and ComplexTypes. This doesn't seem to occur if I do not use ComplexTypes.
I am using System.CommandLine 2.0.0-beta1.21215.1
Target Framework is net5.0 version 5.0.202
Providing the option on the command line is successful.
```
$ dotnet run -- --opt-decimal 123.456
ParseResult: [ system-commandline-decimal-exception [ --opt-decimal <123.456> ] ]
--opt-decimal 123.456
```
Not including the optional parameter on the command line results in the exception.
```
$ dotnet run
Unhandled exception: System.InvalidOperationException: The type System.ReadOnlySpan`1[System.Int32] cannot be bound
at System.CommandLine.Binding.ModelBinder.CreateInstanceInternal(BindingContext bindingContext)
at System.CommandLine.Binding.ModelBinder.GetBoundValue(IValueSource valueSource, BindingContext bindingContext, IValueDescriptor valueDescriptor, Boolean includeMissingValues, Type parentType)
at System.CommandLine.Binding.ModelBinder.GetBoundValues(IDictionary`2 bindingSources, BindingContext bindingContext, IReadOnlyList`1 valueDescriptors, Boolean enforceExplicitBinding, Type parentType, Boolean includeMissingValues)
at System.CommandLine.Binding.ModelBinder.GetBestConstructorAndArgs(BindingContext bindingContext)
at System.CommandLine.Binding.ModelBinder.CreateInstanceInternal(BindingContext bindingContext)
at System.CommandLine.Binding.ModelBinder.GetBoundValue(IValueSource valueSource, BindingContext bindingContext, IValueDescriptor valueDescriptor, Boolean includeMissingValues, Type parentType)
at System.CommandLine.Binding.ModelBinder.GetBoundValues(IDictionary`2 bindingSources, BindingContext bindingContext, IReadOnlyList`1 valueDescriptors, Boolean enforceExplicitBinding, Type parentType, Boolean includeMissingValues)
at System.CommandLine.Binding.ModelBinder.UpdateInstanceInternalNotifyIfNonDefaultsUsed[T](T instance, BindingContext bindingContext)
at System.CommandLine.Binding.ModelBinder.InstanceFromSpecificConstructor(BindingContext bindingContext, ConstructorDescriptor constructor, IReadOnlyList`1 boundValues, Boolean& nonDefaultsUsed)
at System.CommandLine.Binding.ModelBinder.CreateInstanceInternal(BindingContext bindingContext)
at System.CommandLine.Binding.ModelBinder.GetBoundValue(IValueSource valueSource, BindingContext bindingContext, IValueDescriptor valueDescriptor, Boolean includeMissingValues, Type parentType)
at System.CommandLine.Binding.ModelBinder.GetBoundValues(IDictionary`2 bindingSources, BindingContext bindingContext, IReadOnlyList`1 valueDescriptors, Boolean enforceExplicitBinding, Type parentType, Boolean includeMissingValues)
at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<b__1>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__24_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<b__0>d.MoveNext()
```
Here is a quick sample program to reproduce.
```
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
namespace system_commandline_decimal_exception
{
///
/// Successful example
///
/// $ dotnet run -- --opt-decimal 123.456
/// ParseResult: [ system-commandline-decimal-exception [ --opt-decimal <123.456> ] ]
/// --opt-decimal 123.456
///
/// Exception
///
/// $ dotnet run --
///
class Program
{
static void Main(string[] args)
{
var rootCommand = new RootCommand()
{
Description = "Console app to demonstrate System.CommandLine complex type exception"
};
rootCommand.Add(new Option(new[] { "--opt-decimal" }, description: "A decimal option"));
rootCommand.Handler = CommandHandler.Create((ParseResult parseResult, IConsole console, ComplexType options) =>
{
console.Out.WriteLine($"{parseResult}");
console.Out.WriteLine($"--opt-decimal {options.OptDecimal}");
});
rootCommand.InvokeAsync(args);
}
}
public class ComplexType
{
public decimal OptDecimal { get; set; }
}
}
```
_Originally posted by @iesoftwaredeveloper in https://github.com/dotnet/command-line-api/issues/1137#issuecomment-846255778_
Contributor guide
Assessment
This issue has not been assessed yet.