dotnet / dotnet/command-line-api

Command.SetHandler failing to bind Arguments

Open
#1,685 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

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

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.