From 35bf49b1e76dabe5a013173dabb3bce3093f50f4 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Mar 2023 15:20:09 +0100 Subject: [PATCH 1/5] remove SetHandler overloads --- ...ommandLine_api_is_not_changed.approved.txt | 18 -- .../CommandLine/Perf_Parser_Simple.cs | 6 +- .../EndToEndTestApp/Program.cs | 16 +- src/System.CommandLine.Tests/ArgumentTests.cs | 2 +- .../Binding/SetHandlerTests.cs | 209 +----------------- .../EnvironmentVariableDirectiveTests.cs | 14 +- .../GlobalOptionTests.cs | 4 +- .../Invocation/InvocationExtensionsTests.cs | 8 +- .../Invocation/InvocationPipelineTests.cs | 22 +- .../ParserTests.MultiplePositions.cs | 2 +- .../ParsingValidationTests.cs | 8 +- .../UseExceptionHandlerTests.cs | 4 +- src/System.CommandLine.Tests/UseHelpTests.cs | 2 +- .../VersionOptionTests.cs | 18 +- src/System.CommandLine/Handler.Action.cs | 185 ---------------- src/System.CommandLine/Handler.Func.cs | 185 ---------------- src/System.CommandLine/Handler.cs | 26 --- 17 files changed, 59 insertions(+), 670 deletions(-) delete mode 100644 src/System.CommandLine/Handler.cs diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index 73e49a9e72..abc07ddebf 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -110,25 +110,7 @@ System.CommandLine .ctor() public static class Handler public static System.Void SetHandler(this Command command, System.Action handle) - public static System.Void SetHandler(this Command command, System.Action handle) - public static System.Void SetHandler(this Command command, System.Func handle) public static System.Void SetHandler(this Command command, System.Func handle) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6, IValueDescriptor symbol7) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6, IValueDescriptor symbol7) - public static System.Void SetHandler(this Command command, Action handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6, IValueDescriptor symbol7, IValueDescriptor symbol8) - public static System.Void SetHandler(this Command command, Func handle, IValueDescriptor symbol1, IValueDescriptor symbol2, IValueDescriptor symbol3, IValueDescriptor symbol4, IValueDescriptor symbol5, IValueDescriptor symbol6, IValueDescriptor symbol7, IValueDescriptor symbol8) public interface ICommandHandler public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context) public System.Threading.Tasks.Task InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null) diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Simple.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Simple.cs index ffe283f11b..9c0cb8bc83 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Simple.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Simple.cs @@ -33,7 +33,11 @@ private static RootCommand BuildCommand() stringOption }; - command.SetHandler(static (bool _, string _) => { }, boolOption, stringOption); + command.SetHandler(ctx => + { + bool boolean = ctx.ParseResult.GetValue(boolOption); + string text = ctx.ParseResult.GetValue(stringOption); + }); return command; } diff --git a/src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs b/src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs index 02f4da77fd..85626fc381 100644 --- a/src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs +++ b/src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs @@ -1,4 +1,5 @@ using System.CommandLine; +using System.CommandLine.Invocation; using System.CommandLine.Parsing; using System.Threading.Tasks; using System.Threading; @@ -22,12 +23,15 @@ static async Task Main(string[] args) durianOption, }; - rootCommand.SetHandler( - (string apple, string banana, string cherry, string durian, CancellationToken cancellationToken) => Task.CompletedTask, - appleOption, - bananaOption, - cherryOption, - durianOption); + rootCommand.SetHandler((InvocationContext ctx, CancellationToken cancellationToken) => + { + string apple = ctx.ParseResult.GetValue(appleOption); + string banana = ctx.ParseResult.GetValue(bananaOption); + string cherry = ctx.ParseResult.GetValue(cherryOption); + string durian = ctx.ParseResult.GetValue(durianOption); + + return Task.CompletedTask; + }); var commandLine = new CommandLineBuilder(rootCommand) .UseDefaults() diff --git a/src/System.CommandLine.Tests/ArgumentTests.cs b/src/System.CommandLine.Tests/ArgumentTests.cs index 6bc7d9d649..b32dcbd796 100644 --- a/src/System.CommandLine.Tests/ArgumentTests.cs +++ b/src/System.CommandLine.Tests/ArgumentTests.cs @@ -378,7 +378,7 @@ public async Task Custom_argument_parser_is_only_called_once() }; var command = new RootCommand(); - command.SetHandler((int value) => handlerWasCalled = true, option); + command.SetHandler((ctx) => handlerWasCalled = true); command.Options.Add(option); await command.InvokeAsync("--value 42"); diff --git a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs index beb956865d..9b0dc3a409 100644 --- a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs +++ b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.CommandLine.Binding; +using System.CommandLine.Invocation; using System.CommandLine.IO; using System.CommandLine.Parsing; using System.Linq; @@ -15,31 +16,6 @@ namespace System.CommandLine.Tests.Binding { public class SetHandlerTests { - [Fact] - public void Custom_types_can_be_bound() - { - var boolOption = new Option("-i"); - var stringArg = new Argument("value"); - - var command = new RootCommand - { - boolOption, - stringArg - }; - - CustomType boundInstance = default; - command.SetHandler( - (CustomType instance) => boundInstance = instance, - new CustomBinder(boolOption, stringArg)); - - var console = new TestConsole(); - command.Invoke("-i 123 hi", console); - - boundInstance.IntValue.Should().Be(123); - boundInstance.StringValue.Should().Be("hi"); - boundInstance.Console.Should().NotBeNull(); - } - public class CustomType { public string StringValue { get; set; } @@ -71,187 +47,6 @@ protected override CustomType GetBoundValue(BindingContext bindingContext) } } - [Theory] - [InlineData(1)] - [InlineData(2)] - [InlineData(3)] - [InlineData(4)] - [InlineData(5)] - [InlineData(6)] - [InlineData(7)] - [InlineData(8)] - public void Binding_is_correct_for_Action_overload_having_arity_(int arity) - { - var command = new RootCommand(); - var commandLine = ""; - - for (var i = 1; i <= arity; i++) - { - command.Arguments.Add(new Argument($"i{i}")); - - commandLine += $" {i}"; - } - - var receivedValues = new List(); - Delegate handlerFunc = arity switch - { - 1 => new Action( - i1 => - Received(i1)), - 2 => new Action( - (i1, i2) => - Received(i1, i2)), - 3 => new Action( - (i1, i2, i3) => - Received(i1, i2, i3)), - 4 => new Action( - (i1, i2, i3, i4) => - Received(i1, i2, i3, i4)), - 5 => new Action( - (i1, i2, i3, i4, i5) => - Received(i1, i2, i3, i4, i5)), - 6 => new Action( - (i1, i2, i3, i4, i5, i6) => - Received(i1, i2, i3, i4, i5, i6)), - 7 => new Action( - (i1, i2, i3, i4, i5, i6, i7) => - Received(i1, i2, i3, i4, i5, i6, i7)), - 8 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8) => - Received(i1, i2, i3, i4, i5, i6, i7, i8)), - - _ => throw new ArgumentOutOfRangeException() - }; - - // build up the method invocation - var genericMethodDef = typeof(Handler) - .GetMethods() - .Where(m => m.Name == nameof(Handler.SetHandler)) - .Where(m => m.IsGenericMethod /* symbols + handler Func */) - .Where(m => m.GetParameters().ElementAt(1).ParameterType.Name.StartsWith("Action")) - .Single(m => m.GetGenericArguments().Length == arity); - - var genericParameterTypes = Enumerable.Range(1, arity) - .Select(_ => typeof(int)) - .ToArray(); - - var setHandler = genericMethodDef.MakeGenericMethod(genericParameterTypes); - - var parameters = new List - { - command, - handlerFunc - }; - - parameters.AddRange(command.Arguments); - - setHandler.Invoke(null, parameters.ToArray()); - - var exitCode = command.Invoke(commandLine); - - receivedValues.Should().BeEquivalentTo( - Enumerable.Range(1, arity), - config => config.WithStrictOrdering()); - - exitCode.Should().Be(0); - - Task Received(params int[] values) - { - receivedValues.AddRange(values); - return Task.CompletedTask; - } - } - - [Theory] - [InlineData(1)] - [InlineData(2)] - [InlineData(3)] - [InlineData(4)] - [InlineData(5)] - [InlineData(6)] - [InlineData(7)] - [InlineData(8)] - public void Binding_is_correct_for_Func_overload_having_arity_(int arity) - { - var command = new RootCommand(); - var commandLine = ""; - - for (var i = 1; i <= arity; i++) - { - command.Arguments.Add(new Argument($"i{i}")); - - commandLine += $" {i}"; - } - - var receivedValues = new List(); - Delegate handlerFunc = arity switch - { - 1 => new Func( - (i1, cancellationToken) => - Received(i1)), - 2 => new Func( - (i1, i2, cancellationToken) => - Received(i1, i2)), - 3 => new Func( - (i1, i2, i3, cancellationToken) => - Received(i1, i2, i3)), - 4 => new Func( - (i1, i2, i3, i4, cancellationToken) => - Received(i1, i2, i3, i4)), - 5 => new Func( - (i1, i2, i3, i4, i5, cancellationToken) => - Received(i1, i2, i3, i4, i5)), - 6 => new Func( - (i1, i2, i3, i4, i5, i6, cancellationToken) => - Received(i1, i2, i3, i4, i5, i6)), - 7 => new Func( - (i1, i2, i3, i4, i5, i6, i7, cancellationToken) => - Received(i1, i2, i3, i4, i5, i6, i7)), - 8 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, cancellationToken) => - Received(i1, i2, i3, i4, i5, i6, i7, i8)), - - _ => throw new ArgumentOutOfRangeException() - }; - - // build up the method invocation - var genericMethodDef = typeof(Handler) - .GetMethods() - .Where(m => m.Name == nameof(Handler.SetHandler)) - .Where(m => m.IsGenericMethod /* symbols + handler Func */) - .Where(m => m.GetParameters().ElementAt(1).ParameterType.Name.StartsWith("Func")) - .Single(m => m.GetGenericArguments().Length == arity); - - var genericParameterTypes = Enumerable.Range(1, arity) - .Select(_ => typeof(int)) - .ToArray(); - - var setHandler = genericMethodDef.MakeGenericMethod(genericParameterTypes); - - var parameters = new List - { - command, - handlerFunc, - }; - parameters.AddRange(command.Arguments); - - setHandler.Invoke(null, parameters.ToArray()); - - var exitCode = command.Invoke(commandLine); - - receivedValues.Should().BeEquivalentTo( - Enumerable.Range(1, arity), - config => config.WithStrictOrdering()); - - exitCode.Should().Be(123); - - Task Received(params int[] values) - { - receivedValues.AddRange(values); - return Task.FromResult(123); - } - } - [Fact] public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_was_thrown() { @@ -259,7 +54,7 @@ public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_ var command = new Command("wat"); - var handle = (CancellationToken cancellationToken) => + var handle = (InvocationContext ctx, CancellationToken cancellationToken) => { wasCalled = true; return Task.FromResult(new { NovelType = true }); diff --git a/src/System.CommandLine.Tests/EnvironmentVariableDirectiveTests.cs b/src/System.CommandLine.Tests/EnvironmentVariableDirectiveTests.cs index 0ca5fa347b..b66b53dabe 100644 --- a/src/System.CommandLine.Tests/EnvironmentVariableDirectiveTests.cs +++ b/src/System.CommandLine.Tests/EnvironmentVariableDirectiveTests.cs @@ -19,7 +19,7 @@ public async Task Sets_environment_variable_to_value() string variable = test_variable; const string value = "This is a test"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; Environment.GetEnvironmentVariable(variable).Should().Be(value); @@ -41,7 +41,7 @@ public async Task Trims_environment_variable_name() string variable = test_variable; const string value = "This is a test"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; Environment.GetEnvironmentVariable(variable).Should().Be(value); @@ -63,7 +63,7 @@ public async Task Trims_environment_variable_value() string variable = test_variable; const string value = "This is a test"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; Environment.GetEnvironmentVariable(variable).Should().Be(value); @@ -85,7 +85,7 @@ public async Task Sets_environment_variable_value_containing_equals_sign() string variable = test_variable; const string value = "This is = a test containing equals"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; Environment.GetEnvironmentVariable(variable).Should().Be(value); @@ -106,7 +106,7 @@ public async Task Ignores_environment_directive_without_equals_sign() bool asserted = false; string variable = test_variable; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; Environment.GetEnvironmentVariable(variable).Should().BeNull(); @@ -127,7 +127,7 @@ public static async Task Ignores_environment_directive_with_empty_variable_name( bool asserted = false; string value = $"This is a test, random: {randomizer.Next()}"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; var env = Environment.GetEnvironmentVariables(); @@ -149,7 +149,7 @@ public static async Task Ignores_environment_directive_with_whitespace_variable_ bool asserted = false; string value = $"This is a test, random: {randomizer.Next()}"; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => + rootCommand.SetHandler((_) => { asserted = true; var env = Environment.GetEnvironmentVariables(); diff --git a/src/System.CommandLine.Tests/GlobalOptionTests.cs b/src/System.CommandLine.Tests/GlobalOptionTests.cs index ee4212bfe1..63e068b72c 100644 --- a/src/System.CommandLine.Tests/GlobalOptionTests.cs +++ b/src/System.CommandLine.Tests/GlobalOptionTests.cs @@ -29,7 +29,7 @@ public void When_a_required_global_option_is_omitted_it_results_in_an_error() { var command = new Command("child"); var rootCommand = new RootCommand { command }; - command.SetHandler(() => { }); + command.SetHandler((_) => { }); var requiredOption = new Option("--i-must-be-set") { IsRequired = true, @@ -69,7 +69,7 @@ public void When_a_required_global_option_is_present_on_child_of_command_it_was_ { var command = new Command("child"); var rootCommand = new RootCommand { command }; - command.SetHandler(() => { }); + command.SetHandler((_) => { }); var requiredOption = new Option("--i-must-be-set") { IsRequired = true, diff --git a/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs b/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs index 9308314cc1..c1f315b753 100644 --- a/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs +++ b/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs @@ -52,7 +52,7 @@ public async Task RootCommand_InvokeAsync_returns_0_when_handler_is_successful() var wasCalled = false; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => wasCalled = true); + rootCommand.SetHandler((_) => wasCalled = true); var result = await rootCommand.InvokeAsync(""); @@ -66,7 +66,7 @@ public void RootCommand_Invoke_returns_0_when_handler_is_successful() var wasCalled = false; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => wasCalled = true); + rootCommand.SetHandler((_) => wasCalled = true); int result = rootCommand.Invoke(""); @@ -80,7 +80,7 @@ public async Task RootCommand_InvokeAsync_returns_1_when_handler_throws() var wasCalled = false; var rootCommand = new RootCommand(); - rootCommand.SetHandler(_ => + rootCommand.SetHandler((_, __) => { wasCalled = true; throw new Exception("oops!"); @@ -103,7 +103,7 @@ public void RootCommand_Invoke_returns_1_when_handler_throws() var wasCalled = false; var rootCommand = new RootCommand(); - rootCommand.SetHandler(_ => + rootCommand.SetHandler((_, __) => { wasCalled = true; throw new Exception("oops!"); diff --git a/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs b/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs index 98b014bd9a..1076923f85 100644 --- a/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs +++ b/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs @@ -39,10 +39,10 @@ public async Task InvokeAsync_chooses_the_appropriate_command() var secondWasCalled = false; var first = new Command("first"); - first.SetHandler(() => firstWasCalled = true); + first.SetHandler((_) => firstWasCalled = true); var second = new Command("second"); - second.SetHandler(() => secondWasCalled = true); + second.SetHandler((_) => secondWasCalled = true); var config = new CommandLineBuilder(new RootCommand { @@ -64,10 +64,10 @@ public void Invoke_chooses_the_appropriate_command() var secondWasCalled = false; var first = new Command("first"); - first.SetHandler(() => firstWasCalled = true); + first.SetHandler((_) => firstWasCalled = true); var second = new Command("second"); - second.SetHandler(() => secondWasCalled = true); + second.SetHandler((_) => secondWasCalled = true); var config = new CommandLineBuilder(new RootCommand { @@ -120,14 +120,14 @@ public void When_middleware_throws_then_Invoke_does_not_handle_the_exception() public void When_command_handler_throws_then_InvokeAsync_does_not_handle_the_exception() { var command = new Command("the-command"); - command.SetHandler(_ => - { - throw new Exception("oops!"); - // Help the compiler pick a CommandHandler.Create overload. + command.SetHandler((_, __) => + { + throw new Exception("oops!"); + // Help the compiler pick a CommandHandler.Create overload. #pragma warning disable CS0162 // Unreachable code detected - return Task.FromResult(0); + return Task.FromResult(0); #pragma warning restore CS0162 - }); + }); var config = new CommandLineBuilder(new RootCommand { @@ -149,7 +149,7 @@ public void When_command_handler_throws_then_InvokeAsync_does_not_handle_the_exc public void When_command_handler_throws_then_Invoke_does_not_handle_the_exception() { var command = new Command("the-command"); - command.SetHandler(_ => + command.SetHandler((_, __) => { throw new Exception("oops!"); // Help the compiler pick a CommandHandler.Create overload. diff --git a/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs b/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs index 85739ef226..77b7266a2d 100644 --- a/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs +++ b/src/System.CommandLine.Tests/ParserTests.MultiplePositions.cs @@ -118,7 +118,7 @@ public void A_command_can_be_specified_in_more_than_one_position( string expectedParent) { var reusedCommand = new Command("reused"); - reusedCommand.SetHandler(() => { }); + reusedCommand.SetHandler((_) => { }); reusedCommand.Add(new Option("--the-option")); var outer = new Command("outer") diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs index 07119ea452..c773d772ec 100644 --- a/src/System.CommandLine.Tests/ParsingValidationTests.cs +++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs @@ -514,9 +514,9 @@ public async Task A_custom_validator_added_to_a_global_option_is_checked(string rootCommand.Options.Add(globalOption); - rootCommand.SetHandler((int i) => handlerWasCalled = true, globalOption); - childCommand.SetHandler((int i) => handlerWasCalled = true, globalOption); - grandchildCommand.SetHandler((int i) => handlerWasCalled = true, globalOption); + rootCommand.SetHandler((ctx) => handlerWasCalled = true); + childCommand.SetHandler((ctx) => handlerWasCalled = true); + grandchildCommand.SetHandler((ctx) => handlerWasCalled = true); var result = await rootCommand.InvokeAsync(commandLine); @@ -1145,7 +1145,7 @@ public void A_command_with_subcommands_is_valid_to_invoke_if_it_has_a_handler() { var outer = new Command("outer"); var inner = new Command("inner"); - inner.SetHandler(() => { }); + inner.SetHandler((_) => { }); var innerer = new Command("inner-er"); outer.Subcommands.Add(inner); inner.Subcommands.Add(innerer); diff --git a/src/System.CommandLine.Tests/UseExceptionHandlerTests.cs b/src/System.CommandLine.Tests/UseExceptionHandlerTests.cs index c1fd66824a..744185c856 100644 --- a/src/System.CommandLine.Tests/UseExceptionHandlerTests.cs +++ b/src/System.CommandLine.Tests/UseExceptionHandlerTests.cs @@ -53,7 +53,7 @@ public async Task UseExceptionHandler_catches_middleware_exceptions_and_writes_d public async Task UseExceptionHandler_catches_command_handler_exceptions_and_sets_result_code_to_1() { var command = new Command("the-command"); - command.SetHandler(_ => + command.SetHandler((_, __) => { throw new Exception("oops!"); // Help the compiler pick a CommandHandler.Create overload. @@ -78,7 +78,7 @@ public async Task UseExceptionHandler_catches_command_handler_exceptions_and_set public async Task UseExceptionHandler_catches_command_handler_exceptions_and_writes_details_to_standard_error() { var command = new Command("the-command"); - command.SetHandler(_ => + command.SetHandler((_, __) => { throw new Exception("oops!"); // Help the compiler pick a CommandHandler.Create overload. diff --git a/src/System.CommandLine.Tests/UseHelpTests.cs b/src/System.CommandLine.Tests/UseHelpTests.cs index 76cf5e05b3..353770b564 100644 --- a/src/System.CommandLine.Tests/UseHelpTests.cs +++ b/src/System.CommandLine.Tests/UseHelpTests.cs @@ -46,7 +46,7 @@ public async Task UseHelp_interrupts_execution_of_the_specified_command() var wasCalled = false; var command = new Command("command"); var subcommand = new Command("subcommand"); - subcommand.SetHandler(() => wasCalled = true); + subcommand.SetHandler((_) => wasCalled = true); command.Subcommands.Add(subcommand); var config = diff --git a/src/System.CommandLine.Tests/VersionOptionTests.cs b/src/System.CommandLine.Tests/VersionOptionTests.cs index 73ff3655b4..9624286caa 100644 --- a/src/System.CommandLine.Tests/VersionOptionTests.cs +++ b/src/System.CommandLine.Tests/VersionOptionTests.cs @@ -37,7 +37,7 @@ public async Task When_the_version_option_is_specified_then_invocation_is_short_ { var wasCalled = false; var rootCommand = new RootCommand(); - rootCommand.SetHandler(() => wasCalled = true); + rootCommand.SetHandler((_) => wasCalled = true); var config = new CommandLineBuilder(rootCommand) .UseVersionOption() @@ -78,7 +78,7 @@ public async Task When_the_version_option_is_specified_and_there_are_default_opt DefaultValueFactory = (_) => true } }; - rootCommand.SetHandler(() => { }); + rootCommand.SetHandler((_) => { }); var configuration = new CommandLineBuilder(rootCommand) .UseVersionOption() @@ -98,7 +98,7 @@ public async Task When_the_version_option_is_specified_and_there_are_default_arg { new Argument("x") { DefaultValueFactory =(_) => true } }; - rootCommand.SetHandler(() => { }); + rootCommand.SetHandler((_) => { }); var configuration = new CommandLineBuilder(rootCommand) .UseVersionOption() @@ -117,13 +117,13 @@ public async Task When_the_version_option_is_specified_and_there_are_default_arg public void Version_is_not_valid_with_other_tokens(string commandLine) { var subcommand = new Command("subcommand"); - subcommand.SetHandler(() => { }); + subcommand.SetHandler((_) => { }); var rootCommand = new RootCommand { subcommand, new Option("-x") }; - rootCommand.SetHandler(() => { }); + rootCommand.SetHandler((_) => { }); var configuration = new CommandLineBuilder(rootCommand) .UseVersionOption() @@ -138,13 +138,13 @@ public void Version_is_not_valid_with_other_tokens(string commandLine) public void Version_option_is_not_added_to_subcommands() { var childCommand = new Command("subcommand"); - childCommand.SetHandler(() => { }); + childCommand.SetHandler((_) => { }); var rootCommand = new RootCommand { childCommand, }; - rootCommand.SetHandler(() => { }); + rootCommand.SetHandler((_) => { }); var configuration = new CommandLineBuilder(rootCommand) .UseVersionOption() @@ -198,12 +198,12 @@ public async Task Version_can_specify_additional_alias() public void Version_is_not_valid_with_other_tokens_uses_custom_alias() { var childCommand = new Command("subcommand"); - childCommand.SetHandler(() => { }); + childCommand.SetHandler((_) => { }); var rootCommand = new RootCommand { childCommand }; - rootCommand.SetHandler(() => { }); + rootCommand.SetHandler((_) => { }); var configuration = new CommandLineBuilder(rootCommand) .UseVersionOption("-v") diff --git a/src/System.CommandLine/Handler.Action.cs b/src/System.CommandLine/Handler.Action.cs index ed7039beb0..2812339f3f 100644 --- a/src/System.CommandLine/Handler.Action.cs +++ b/src/System.CommandLine/Handler.Action.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.CommandLine.Binding; using System.CommandLine.Invocation; namespace System.CommandLine; @@ -18,188 +17,4 @@ public static void SetHandler( this Command command, Action handle) => command.Handler = new AnonymousCommandHandler(handle); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle) => - command.Handler = new AnonymousCommandHandler(_ => handle()); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol, context); - - handle(value1!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - - handle(value1!, value2!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - - handle(value1!, value2!, value3!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - - handle(value1!, value2!, value3!, value4!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - - handle(value1!, value2!, value3!, value4!, value5!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6, - IValueDescriptor symbol7) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - var value7 = GetValueForHandlerParameter(symbol7, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6, - IValueDescriptor symbol7, - IValueDescriptor symbol8) => - command.Handler = new AnonymousCommandHandler( - context => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - var value7 = GetValueForHandlerParameter(symbol7, context); - var value8 = GetValueForHandlerParameter(symbol8, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!); - }); } \ No newline at end of file diff --git a/src/System.CommandLine/Handler.Func.cs b/src/System.CommandLine/Handler.Func.cs index b791fecc53..0fd930c8a2 100644 --- a/src/System.CommandLine/Handler.Func.cs +++ b/src/System.CommandLine/Handler.Func.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.CommandLine.Binding; using System.CommandLine.Invocation; using System.Threading; using System.Threading.Tasks; @@ -13,14 +12,6 @@ namespace System.CommandLine; /// public static partial class Handler { - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle) => - command.Handler = new AnonymousCommandHandler((ctx, cancellationToken) => handle(cancellationToken)); - /// /// Sets a command's handler based on a . /// @@ -28,180 +19,4 @@ public static void SetHandler( this Command command, Func handle) => command.Handler = new AnonymousCommandHandler(handle); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol, context); - - return handle(value1!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - - return handle(value1!, value2!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - - return handle(value1!, value2!, value3!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - - return handle(value1!, value2!, value3!, value4!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - - return handle(value1!, value2!, value3!, value4!, value5!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6, - IValueDescriptor symbol7) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - var value7 = GetValueForHandlerParameter(symbol7, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, cancellationToken); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - IValueDescriptor symbol1, - IValueDescriptor symbol2, - IValueDescriptor symbol3, - IValueDescriptor symbol4, - IValueDescriptor symbol5, - IValueDescriptor symbol6, - IValueDescriptor symbol7, - IValueDescriptor symbol8) => - command.Handler = new AnonymousCommandHandler( - (context, cancellationToken) => - { - var value1 = GetValueForHandlerParameter(symbol1, context); - var value2 = GetValueForHandlerParameter(symbol2, context); - var value3 = GetValueForHandlerParameter(symbol3, context); - var value4 = GetValueForHandlerParameter(symbol4, context); - var value5 = GetValueForHandlerParameter(symbol5, context); - var value6 = GetValueForHandlerParameter(symbol6, context); - var value7 = GetValueForHandlerParameter(symbol7, context); - var value8 = GetValueForHandlerParameter(symbol8, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, cancellationToken); - }); } \ No newline at end of file diff --git a/src/System.CommandLine/Handler.cs b/src/System.CommandLine/Handler.cs deleted file mode 100644 index a60a41d361..0000000000 --- a/src/System.CommandLine/Handler.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.CommandLine.Binding; -using System.CommandLine.Invocation; - -namespace System.CommandLine; - -public static partial class Handler -{ - private static T? GetValueForHandlerParameter( - IValueDescriptor symbol, - InvocationContext context) - { - if (symbol is IValueSource valueSource && - valueSource.TryGetValue(symbol, context.BindingContext, out var boundValue) && - boundValue is T value) - { - return value; - } - else - { - return context.ParseResult.GetValueFor(symbol); - } - } -} \ No newline at end of file From a34ed2dd6457c72701c13b597760515129062d63 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Mar 2023 15:49:13 +0100 Subject: [PATCH 2/5] merge Command.SetHandler extension methods into Command --- ...ommandLine_api_is_not_changed.approved.txt | 5 ++--- src/System.CommandLine/Command.cs | 17 +++++++++++++- src/System.CommandLine/Handler.Action.cs | 20 ----------------- src/System.CommandLine/Handler.Func.cs | 22 ------------------- 4 files changed, 18 insertions(+), 46 deletions(-) delete mode 100644 src/System.CommandLine/Handler.Action.cs delete mode 100644 src/System.CommandLine/Handler.Func.cs diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index abc07ddebf..aefba634a5 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -50,6 +50,8 @@ System.CommandLine public System.Collections.Generic.IEnumerator GetEnumerator() public ParseResult Parse(System.Collections.Generic.IReadOnlyList args, CommandLineConfiguration configuration = null) public ParseResult Parse(System.String commandLine, CommandLineConfiguration configuration = null) + public System.Void SetHandler(System.Action handle) + public System.Void SetHandler(System.Func handle) public static class CommandExtensions public static System.Int32 Invoke(this Command command, System.String[] args, IConsole console = null) public static System.Int32 Invoke(this Command command, System.String commandLine, IConsole console = null) @@ -108,9 +110,6 @@ System.CommandLine public System.Void SetSynchronousHandler(System.Action handler) public class EnvironmentVariablesDirective : Directive .ctor() - public static class Handler - public static System.Void SetHandler(this Command command, System.Action handle) - public static System.Void SetHandler(this Command command, System.Func handle) public interface ICommandHandler public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context) public System.Threading.Tasks.Task InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null) diff --git a/src/System.CommandLine/Command.cs b/src/System.CommandLine/Command.cs index 753d5938c1..c0d43bd14e 100644 --- a/src/System.CommandLine/Command.cs +++ b/src/System.CommandLine/Command.cs @@ -4,9 +4,12 @@ using System.Collections; using System.Collections.Generic; using System.CommandLine.Completions; +using System.CommandLine.Invocation; using System.CommandLine.Parsing; using System.ComponentModel; using System.Linq; +using System.Threading.Tasks; +using System.Threading; namespace System.CommandLine { @@ -123,7 +126,7 @@ public void Add(Symbol symbol) /// that will be performed when the command is invoked. /// /// - /// Use one of the overloads to construct a handler. + /// Use one of the overloads to construct a handler. /// If the handler is not specified, parser errors will be generated for command line input that /// invokes this command. public ICommandHandler? Handler { get; set; } @@ -251,6 +254,18 @@ void AddCompletionsFor(Symbol identifier, AliasSet? aliases) } } + /// + /// Sets a command's handler based on an . + /// + public void SetHandler(Action handle) + => Handler = new AnonymousCommandHandler(handle); + + /// + /// Sets a command's handler based on a . + /// + public void SetHandler(Func handle) + => Handler = new AnonymousCommandHandler(handle); + internal bool EqualsNameOrAlias(string name) => Name.Equals(name, StringComparison.Ordinal) || (_aliases is not null && _aliases.Contains(name)); } diff --git a/src/System.CommandLine/Handler.Action.cs b/src/System.CommandLine/Handler.Action.cs deleted file mode 100644 index 2812339f3f..0000000000 --- a/src/System.CommandLine/Handler.Action.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.CommandLine.Invocation; - -namespace System.CommandLine; - -/// -/// Provides methods for creating and working with command handlers. -/// -public static partial class Handler -{ - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle) => - command.Handler = new AnonymousCommandHandler(handle); -} \ No newline at end of file diff --git a/src/System.CommandLine/Handler.Func.cs b/src/System.CommandLine/Handler.Func.cs deleted file mode 100644 index 0fd930c8a2..0000000000 --- a/src/System.CommandLine/Handler.Func.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.CommandLine.Invocation; -using System.Threading; -using System.Threading.Tasks; - -namespace System.CommandLine; - -/// -/// Provides methods for creating and working with command handlers. -/// -public static partial class Handler -{ - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle) => - command.Handler = new AnonymousCommandHandler(handle); -} \ No newline at end of file From 2bb2102a784ef15bdbc005fae20334a4e2ad3f96 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Mar 2023 16:03:02 +0100 Subject: [PATCH 3/5] remove IValueDescriptor --- ....System_CommandLine_api_is_not_changed.approved.txt | 7 +++---- src/System.CommandLine/Argument{T}.cs | 3 +-- src/System.CommandLine/Binding/BinderBase{T}.cs | 2 +- src/System.CommandLine/Binding/IValueDescriptor{T}.cs | 10 ---------- src/System.CommandLine/Option{T}.cs | 3 +-- src/System.CommandLine/ParseResult.cs | 8 -------- 6 files changed, 6 insertions(+), 27 deletions(-) delete mode 100644 src/System.CommandLine/Binding/IValueDescriptor{T}.cs diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index aefba634a5..a4596a15f2 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -9,7 +9,7 @@ System.CommandLine public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.Object GetDefaultValue() public System.String ToString() - public class Argument : Argument, IValueDescriptor, System.CommandLine.Binding.IValueDescriptor + public class Argument : Argument, System.CommandLine.Binding.IValueDescriptor .ctor(System.String name) public Func CustomParser { get; set; } public Func DefaultValueFactory { get; set; } @@ -125,7 +125,7 @@ System.CommandLine public System.Collections.Generic.List> Validators { get; } public System.Type ValueType { get; } public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) - public class Option : Option, IValueDescriptor, System.CommandLine.Binding.IValueDescriptor + public class Option : Option, System.CommandLine.Binding.IValueDescriptor .ctor(System.String name, System.String[] aliases) public Func CustomParser { get; set; } public Func DefaultValueFactory { get; set; } @@ -172,7 +172,7 @@ System.CommandLine public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.String ToString() System.CommandLine.Binding - public abstract class BinderBase, IValueDescriptor, IValueDescriptor, IValueSource + public abstract class BinderBase, IValueDescriptor, IValueSource protected T GetBoundValue(BindingContext bindingContext) public class BindingContext, System.IServiceProvider public System.CommandLine.IConsole Console { get; } @@ -190,7 +190,6 @@ System.CommandLine.Binding public System.String ValueName { get; } public System.Type ValueType { get; } public System.Object GetDefaultValue() - public interface IValueDescriptor : IValueDescriptor public interface IValueSource public System.Boolean TryGetValue(IValueDescriptor valueDescriptor, BindingContext bindingContext, ref System.Object& boundValue) System.CommandLine.Completions diff --git a/src/System.CommandLine/Argument{T}.cs b/src/System.CommandLine/Argument{T}.cs index dc10e5331e..324e5e5091 100644 --- a/src/System.CommandLine/Argument{T}.cs +++ b/src/System.CommandLine/Argument{T}.cs @@ -1,14 +1,13 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.CommandLine.Binding; using System.CommandLine.Parsing; using System.IO; namespace System.CommandLine { /// - public class Argument : Argument, IValueDescriptor + public class Argument : Argument { private Func? _customParser; diff --git a/src/System.CommandLine/Binding/BinderBase{T}.cs b/src/System.CommandLine/Binding/BinderBase{T}.cs index 94f034acd3..40f6cdac3e 100644 --- a/src/System.CommandLine/Binding/BinderBase{T}.cs +++ b/src/System.CommandLine/Binding/BinderBase{T}.cs @@ -5,7 +5,7 @@ /// /// The type to be bound. public abstract class BinderBase : - IValueDescriptor, + IValueDescriptor, IValueSource { /// diff --git a/src/System.CommandLine/Binding/IValueDescriptor{T}.cs b/src/System.CommandLine/Binding/IValueDescriptor{T}.cs deleted file mode 100644 index 1c68dc078f..0000000000 --- a/src/System.CommandLine/Binding/IValueDescriptor{T}.cs +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace System.CommandLine.Binding -{ - /// - public interface IValueDescriptor : IValueDescriptor - { - } -} \ No newline at end of file diff --git a/src/System.CommandLine/Option{T}.cs b/src/System.CommandLine/Option{T}.cs index 5a7ae32fee..99f3325fbd 100644 --- a/src/System.CommandLine/Option{T}.cs +++ b/src/System.CommandLine/Option{T}.cs @@ -1,14 +1,13 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.CommandLine.Binding; using System.CommandLine.Parsing; namespace System.CommandLine { /// /// The that the option's arguments are expected to be parsed as. - public class Option : Option, IValueDescriptor + public class Option : Option { internal readonly Argument _argument; diff --git a/src/System.CommandLine/ParseResult.cs b/src/System.CommandLine/ParseResult.cs index 1c9ec067a8..b8ccaed431 100644 --- a/src/System.CommandLine/ParseResult.cs +++ b/src/System.CommandLine/ParseResult.cs @@ -105,14 +105,6 @@ CommandLineText is null ? new TokenCompletionContext(this) : new TextCompletionContext(this, CommandLineText); - internal T? GetValueFor(IValueDescriptor symbol) => - symbol switch - { - Argument argument => GetValue(argument), - Option option => GetValue(option), - _ => throw new ArgumentOutOfRangeException() - }; - /// /// Gets the parsed or default value for the specified argument. /// From c64bc509e481934b495cff2d20dcd891b3603a2b Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Mar 2023 16:43:00 +0100 Subject: [PATCH 4/5] remove BinderBase --- ...ommandLine_api_is_not_changed.approved.txt | 2 -- .../Binding/SetHandlerTests.cs | 31 ------------------- .../Binding/BinderBase{T}.cs | 31 ------------------- 3 files changed, 64 deletions(-) delete mode 100644 src/System.CommandLine/Binding/BinderBase{T}.cs diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index a4596a15f2..f48fa8eeb6 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -172,8 +172,6 @@ System.CommandLine public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.String ToString() System.CommandLine.Binding - public abstract class BinderBase, IValueDescriptor, IValueSource - protected T GetBoundValue(BindingContext bindingContext) public class BindingContext, System.IServiceProvider public System.CommandLine.IConsole Console { get; } public System.CommandLine.ParseResult ParseResult { get; } diff --git a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs index 9b0dc3a409..ed9ef18e5a 100644 --- a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs +++ b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs @@ -16,37 +16,6 @@ namespace System.CommandLine.Tests.Binding { public class SetHandlerTests { - public class CustomType - { - public string StringValue { get; set; } - - public int IntValue { get; set; } - - public IConsole Console { get; set; } - } - - public class CustomBinder : BinderBase - { - private readonly Option _intOption; - private readonly Argument _stringArg; - - public CustomBinder(Option intOption, Argument stringArg) - { - _intOption = intOption; - _stringArg = stringArg; - } - - protected override CustomType GetBoundValue(BindingContext bindingContext) - { - return new CustomType - { - Console = bindingContext.Console, - IntValue = bindingContext.ParseResult.GetValue(_intOption), - StringValue = bindingContext.ParseResult.GetValue(_stringArg), - }; - } - } - [Fact] public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_was_thrown() { diff --git a/src/System.CommandLine/Binding/BinderBase{T}.cs b/src/System.CommandLine/Binding/BinderBase{T}.cs deleted file mode 100644 index 40f6cdac3e..0000000000 --- a/src/System.CommandLine/Binding/BinderBase{T}.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace System.CommandLine.Binding; - -/// -/// Supports binding of custom types. -/// -/// The type to be bound. -public abstract class BinderBase : - IValueDescriptor, - IValueSource -{ - /// - /// Gets a value from the binding context. - /// - /// - /// - protected abstract T GetBoundValue(BindingContext bindingContext); - - string IValueDescriptor.ValueName => GetType().Name; - - Type IValueDescriptor.ValueType => typeof(T); - - bool IValueDescriptor.HasDefaultValue => false; - - object? IValueDescriptor.GetDefaultValue() => default(T); - - bool IValueSource.TryGetValue(IValueDescriptor valueDescriptor, BindingContext bindingContext, out object? boundValue) - { - boundValue = GetBoundValue(bindingContext); - return true; - } -} \ No newline at end of file From e3406008d048c254bbc5ec1db1c5329bb2607214 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Mar 2023 18:34:42 +0100 Subject: [PATCH 5/5] address code review feedback: fix the comment --- src/System.CommandLine/Command.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.CommandLine/Command.cs b/src/System.CommandLine/Command.cs index c0d43bd14e..e95b9af290 100644 --- a/src/System.CommandLine/Command.cs +++ b/src/System.CommandLine/Command.cs @@ -261,7 +261,7 @@ public void SetHandler(Action handle) => Handler = new AnonymousCommandHandler(handle); /// - /// Sets a command's handler based on a . + /// Sets a command's handler based on a . /// public void SetHandler(Func handle) => Handler = new AnonymousCommandHandler(handle);