diff --git a/src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs b/src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs index be2fda93dd..631712ca4a 100644 --- a/src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs +++ b/src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs @@ -415,5 +415,96 @@ public async Task Method_parameters_on_the_invoked_member_method_are_bound_to_ma testClass.boundName.Should().Be("Gandalf"); testClass.boundAge.Should().Be(425); } + + [Fact] + public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names() + { + string boundName = default; + int boundAge = default; + + void Execute(string name, int age) + { + boundName = name; + boundAge = age; + } + + var command = new Command("command"); + command.AddArgument(new Argument("age")); + command.AddArgument(new Argument("name")); + command.Handler = CommandHandler.Create(Execute); + + await command.InvokeAsync("command 425 Gandalf", _console); + + boundName.Should().Be("Gandalf"); + boundAge.Should().Be(425); + } + + + [Fact] + public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_hyphenated_argument_names() + { + string boundFirstName = default; + + void Execute(string firstName) + { + boundFirstName = firstName; + } + + var command = new Command("command") + { + new Argument("first-name") + }; + command.Handler = CommandHandler.Create(Execute); + + await command.InvokeAsync("command Gandalf", _console); + + boundFirstName.Should().Be("Gandalf"); + } + + [Fact] + public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_argument_names_case_insensitively() + { + string boundName = default; + int boundAge = default; + + void Execute(string name, int AGE) + { + boundName = name; + boundAge = AGE; + } + + var command = new Command("command"); + command.AddArgument(new Argument("AGE")); + command.AddArgument(new Argument("Name")); + command.Handler = CommandHandler.Create(Execute); + + await command.InvokeAsync("command 425 Gandalf", _console); + + boundName.Should().Be("Gandalf"); + boundAge.Should().Be(425); + } + + [Fact] + public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names_with_pipe_in() + { + string boundName = default; + int boundAge = default; + + void Execute(string fullnameOrNickname, int age) + { + boundName = fullnameOrNickname; + boundAge = age; + } + + var command = new Command("command"); + command.AddArgument(new Argument("age")); + command.AddArgument(new Argument("fullname|nickname")); + command.Handler = CommandHandler.Create(Execute); + + await command.InvokeAsync("command 425 Gandalf", _console); + + boundName.Should().Be("Gandalf"); + boundAge.Should().Be(425); + } } } diff --git a/src/System.CommandLine/Binding/Binder.cs b/src/System.CommandLine/Binding/Binder.cs index 00137bf03e..604990a949 100644 --- a/src/System.CommandLine/Binding/Binder.cs +++ b/src/System.CommandLine/Binding/Binder.cs @@ -9,7 +9,8 @@ internal static class Binder { internal static bool IsMatch(this string parameterName, string alias) => string.Equals(alias?.RemovePrefix() - .Replace("-", ""), + .Replace("-", "") + .Replace("|", "or"), parameterName, StringComparison.OrdinalIgnoreCase); diff --git a/src/System.CommandLine/Parsing/CommandResultExtensions.cs b/src/System.CommandLine/Parsing/CommandResultExtensions.cs index 2520b48189..0fbfa5d3b2 100644 --- a/src/System.CommandLine/Parsing/CommandResultExtensions.cs +++ b/src/System.CommandLine/Parsing/CommandResultExtensions.cs @@ -84,4 +84,4 @@ internal static bool TryGetValueForOption( return false; } } -} \ No newline at end of file +}