dotnet / dotnet/command-line-api

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

Abierto
#652 5 comentarios 0 reacciones 0 asignados Ver en GitHub
Area-Parser and Binder
Lenguaje dominante
C#
Estrellas
3.7k
Forks
428
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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;
}
}
```

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Comienza ejecutando el ejemplo de Program proporcionado con TestRequest, TestCommand y CommandHandler.Create para reproducir el comportamiento de vinculación de null. Inspecciona la ruta de vinculación de objetos complejos y añade cobertura de regresión para los argumentos ausentes y los argumentos cuyo valor predeterminado es null; la finalización requiere un comportamiento acordado, ya sea rechazar la entrada o construir la solicitud con valores de argumento predeterminados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
csharp
Área
cli
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.