dotnet / dotnet/command-line-api

Command.SetHandler failing to bind Arguments

Offen
#1,685 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C#
Sterne
3.7k
Forks
428
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der minimalen C#-Reproduktion im Issue und untersuche die Command.SetHandler-Bindung für Positionsparameter anhand von Argument-Definitionen. Führe die gezeigte `dotnet run -- Addition 1 2`-Invocation aus, während du den Binding-Pfad nachverfolgst; abgeschlossen ist es, wenn die Argumente ohne die missing-Argument-Ausnahme gebunden werden und der Befehl seinen Handler erreicht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.