dotnet / dotnet/command-line-api

Complex object binding sends in null if any argument is evaluates to null (even w/ default)

Aperta
#652 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Area-Parser and Binder
Lingua principale
C#
Stelle
3.7k
Fork
428
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hi there, thanks for adding support for complex object binding -- makes the handler code SO much cleaner.

During my refactor to complex, I noticed that whenever a single argument is null w/o a default supplied (or default null), the entire complex type gets passed in as null.

I don't think passing in null would ever be desirable -- either the parser needs to throw or pass in the "default" version of the object (using `default(T)` for each argument that comes in as null maybe?)

We have plenty of use cases where we allow the user to not provide an input and we pull it from a saved setting.

Here's a working example of the behavior:

```csharp
class Program
{
static void Main(string[] args)
{
var root = new RootCommand();

root.AddCommand(TestCommand.Instance);

root.InvokeAsync(args).Wait();
}
}

public class TestRequest
{
public TestRequest(string test1)
{
this.Test1 = test1;
}
public string Test1 { get; }
}

public static class TestCommand
{
public static Command Instance = GetTestCommand();
private static Command GetTestCommand()
{
var test = new Command("test");

//broken w/ default null
//test.AddOption(new Option("--test1")
//{
// Argument = new Argument(() => null)
//});

////broken w/o default
//test.AddOption(new Option("--test1")
//{
// Argument = new Argument()
//});

//works w/ empty string
test.AddOption(new Option("--test1")
{
Argument = new Argument(() => "")
});

test.Handler = CommandHandler.Create((TestRequest request) =>
{
var msg = request == null ? "I'm broken!" : "I work!";

Console.WriteLine(msg);
});

return test;
}
}
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.