Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rstarkov opened this issue Nov 4, 2024 · 0 comments
Open

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

rstarkov opened this issue Nov 4, 2024 · 0 comments

Comments

@rstarkov
Copy link

rstarkov commented Nov 4, 2024

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.

var root = new RootCommand();
var arg1 = new Argument<string>("arg1") { Arity = ArgumentArity.ExactlyOne };
var arg2 = new Argument<string>("arg2") { Arity = ArgumentArity.ExactlyOne };
var arg3 = new Argument<string>("arg3") { Arity = ArgumentArity.ExactlyOne };
var arg4 = new Argument<string>("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:

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

Version: 2.0.0-beta4.22272.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant