dotnet / dotnet/command-line-api

A random argument value is passed in place of an optional argument

Open
#2,502 0 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

This very simple command takes four arguments, the first three being mandatory.

When the last argument (arg4) is omitted, the handler receives the value of arg2 instead of null.

```cs
var root = new RootCommand();
var arg1 = new Argument("arg1") { Arity = ArgumentArity.ExactlyOne };
var arg2 = new Argument("arg2") { Arity = ArgumentArity.ExactlyOne };
var arg3 = new Argument("arg3") { Arity = ArgumentArity.ExactlyOne };
var arg4 = new Argument("arg4") { Arity = ArgumentArity.ZeroOrOne };
root.AddArgument(arg1);
root.AddArgument(arg2);
root.AddArgument(arg3);
root.AddArgument(arg4);

root.SetHandler((v1, v2, v3, v4) =>
{
Console.WriteLine($"arg1: {v1}\narg2: {v2}\narg3: {v3}\narg4: {v4}");
}, arg1, arg2, arg3, arg4);

root.Invoke(["one", "TWO", "three"]);
```

This prints:

```
arg1: one
arg2: TWO
arg3: three
arg4: TWO
```

Expected: arg4 = null. Or, if I misunderstood how to set up such a syntax, a runtime exception. But certainly not "TWO".

It's possible to work around this by adding an explicit default to arg4:

```cs
var arg4 = new Argument("arg4", () => null) { Arity = ArgumentArity.ZeroOrOne };
```

Version: 2.0.0-beta4.22272.1

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.