From 135d082c22db4b0e841ffa8883f57ff22a60b61b Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Sat, 3 Oct 2020 18:27:13 +0100 Subject: [PATCH 1/4] Replace | with or when searching for value This allows the CommandHandler to be registered with `fileOrString` for an Argument named `File|String`. #1051 --- src/System.CommandLine/Parsing/CommandResultExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.CommandLine/Parsing/CommandResultExtensions.cs b/src/System.CommandLine/Parsing/CommandResultExtensions.cs index 2520b48189..d5588d19d5 100644 --- a/src/System.CommandLine/Parsing/CommandResultExtensions.cs +++ b/src/System.CommandLine/Parsing/CommandResultExtensions.cs @@ -36,7 +36,7 @@ internal static bool TryGetValueForArgument( { foreach (var argument in commandResult.Command.Arguments) { - if (valueName.IsMatch(argument.Name)) + if (valueName.IsMatch(argument.Name.Replace("|","or"))) { value = commandResult.ArgumentConversionResults[argument.Name]?.GetValueOrDefault(); return true; @@ -84,4 +84,4 @@ internal static bool TryGetValueForOption( return false; } } -} \ No newline at end of file +} From 5a3540ffe876a013c1b89abce2597f4bb831a941 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Sat, 3 Oct 2020 18:33:26 +0100 Subject: [PATCH 2/4] Allow backwards compatibility --- src/System.CommandLine/Parsing/CommandResultExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.CommandLine/Parsing/CommandResultExtensions.cs b/src/System.CommandLine/Parsing/CommandResultExtensions.cs index d5588d19d5..84d792c25f 100644 --- a/src/System.CommandLine/Parsing/CommandResultExtensions.cs +++ b/src/System.CommandLine/Parsing/CommandResultExtensions.cs @@ -36,7 +36,7 @@ internal static bool TryGetValueForArgument( { foreach (var argument in commandResult.Command.Arguments) { - if (valueName.IsMatch(argument.Name.Replace("|","or"))) + if (valueName.IsMatch(argument.Name) || valueName.IsMatch(argument.Name.Replace("|","or"))) { value = commandResult.ArgumentConversionResults[argument.Name]?.GetValueOrDefault(); return true; From edb9f0200b87ef75fb0749c0640e09cc5ec397c7 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Sun, 4 Oct 2020 13:05:28 +0100 Subject: [PATCH 3/4] Add CommandHandler tests for root command arguments --- .../Invocation/CommandHandlerTests.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) 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); + } } } From b729c4aee95a47bc3c170100c28ad5b99c2766be Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Wed, 14 Oct 2020 13:46:06 +0100 Subject: [PATCH 4/4] Move `|` replacement to `IsMatch` method --- src/System.CommandLine/Binding/Binder.cs | 3 ++- src/System.CommandLine/Parsing/CommandResultExtensions.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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 84d792c25f..0fbfa5d3b2 100644 --- a/src/System.CommandLine/Parsing/CommandResultExtensions.cs +++ b/src/System.CommandLine/Parsing/CommandResultExtensions.cs @@ -36,7 +36,7 @@ internal static bool TryGetValueForArgument( { foreach (var argument in commandResult.Command.Arguments) { - if (valueName.IsMatch(argument.Name) || valueName.IsMatch(argument.Name.Replace("|","or"))) + if (valueName.IsMatch(argument.Name)) { value = commandResult.ArgumentConversionResults[argument.Name]?.GetValueOrDefault(); return true;