dotnet / dotnet/command-line-api

ArgumentNullException while Invoking command with Argument which type implements IEnumerable and takes single string in constructor

Open
#1,429 0 comments 0 reactions 0 assignees View on GitHub
Area-Parser and Binder bug
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

How we can see from [documentation](https://github.com/dotnet/command-line-api/blob/main/docs/How-To.md#argument-validation-and-binding):
> whereby any type that has a constructor taking a single string parameter can be bound without having to write any custom code

So I decided to add Argument with type which implements IEnumerable (actually, it was NpgsqlConnectionStringBuilder which implements IDictionary, but it does not matter how you will see in example) and takes string as a single string parameter in constructor and i got ArgumentNullException with stack trace:

> at System.RuntimeType.MakeGenericType(Type[] instantiation)
> at System.CommandLine.Binding.ArgumentConverter.g__CreateList|3_0(Type itemType, Int32 capacity)
> at System.CommandLine.Binding.ArgumentConverter.ConvertStrings(IArgument argument, Type type, IReadOnlyList1 tokens, ArgumentResult argumentResult)
> at System.CommandLine.Binding.ArgumentConverter.TryConvertArgument(ArgumentResult argumentResult, Object& value)
> at System.CommandLine.Parsing.ArgumentResult.Convert(IArgument argument)
> at System.CommandLine.Parsing.ArgumentResult.GetArgumentConversionResult()
> at System.CommandLine.Parsing.ParseResultVisitor.ValidateAndConvertArgumentResult(ArgumentResult argumentResult)
> at System.CommandLine.Parsing.ParseResultVisitor.Stop(SyntaxNode node)
> at System.CommandLine.Parsing.SyntaxVisitor.Visit(SyntaxNode node)
> at System.CommandLine.Parsing.Parser.Parse(IReadOnlyList1 arguments, String rawInput)
> at System.CommandLine.CommandExtensions.GetInvocationPipeline(Command command, String[] args)
> at System.CommandLine.CommandExtensions.Invoke(Command command, String[] args, IConsole console)
> at CommandLineParserTest.Program.Main(String[] args) in Folder\CommandLineParserTest\Program.cs:line 23

And message:
> System.ArgumentNullException: 'Value cannot be null.'

An example to cause such case is represented below.

```
class Program
{
static int Main(string[] args)
{
var cmd = new RootCommand
{
new Argument("value", "String to be splitted to chars array.")
};
cmd.Handler = CommandHandler.Create((MyCustomCollection value) =>
{
foreach (var item in value.Chars)
{
Console.WriteLine(item);
}
});

return cmd.Invoke(args); // fails here
}

public class MyCustomCollection : IEnumerable
{
public char[] Chars { get; }

public MyCustomCollection(string s)
{
Debug.Assert(s is not null);

Chars = s.ToCharArray();
}

public IEnumerator GetEnumerator() => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.