dotnet / dotnet/command-line-api

Command.SetHandler failing to bind Arguments

Aperta
#1,685 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C#
Stelle
3.7k
Fork
428
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

```
static int Main(string[] args)
{
var additionCommand = new Command("Addition", "Add two numbers together")
{
new Argument("FirstNumber", "The first integer for addition"),
new Argument("SecondNumber", "The Second integer for addition"),
new Option(new[] { "--format", "-f" }, "format with line breaks")
};

additionCommand.SetHandler((int x, int y, bool format) => PerformAddition(x, y, format));

var cmd = new RootCommand
{
additionCommand
};

return cmd.Invoke(args);
}

private static void PerformAddition(int x, int y, bool format)
{
if (format) { Console.WriteLine("=========="); }
Console.WriteLine(x + y);
if (format) { Console.WriteLine("=========="); }
}
```

Command Line Command: "dotnet run -- Addition 1 2"

Command Line Error: "Unhandled exception: System.ArgumentException: The SetHandler call for command 'Addition' is missing an Argument or Option for the parameter at position 0. Did you mean to pass one of these?"

System.CommandLine version: 2.0.0-beta3.22114.1

Issue Description: When running the above command I get the error that my arguments under the "Addition" command are missing. I know this is due to a binding issue somewhere but I am not sure how to resolve this issue

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con la riproduzione minima in C# nell’issue e analizza il binding di Command.SetHandler per i parametri posizionali rispetto alle definizioni di Argument. Esegui l’invocazione mostrata `dotnet run -- Addition 1 2` mentre tracci il percorso di binding; il lavoro è completato quando gli argomenti vengono associati senza l’eccezione missing-Argument e il comando raggiunge il suo handler.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
cli
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.