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 56a0a185ff..35bddbdbe2 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 @@ -103,40 +103,26 @@ public System.Collections.Generic.IEnumerator>> GetEnumerator() public System.Boolean TryGetValues(System.String name, ref System.Collections.Generic.IReadOnlyList values) 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, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Action handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) - public static System.Void SetHandler(this Command command, Func handle, System.CommandLine.Binding.IValueDescriptor[] symbols) + 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 IConsole : System.CommandLine.IO.IStandardError, System.CommandLine.IO.IStandardIn, System.CommandLine.IO.IStandardOut public abstract class IdentifierSymbol : Symbol, System.CommandLine.Completions.ICompletionSource public System.Collections.Generic.IReadOnlyCollection Aliases { get; } @@ -234,7 +220,6 @@ System.CommandLine.Binding public System.Void AddService(Func factory) public System.Object GetService(System.Type serviceType) public struct BoundValue : System.ValueType - public static BoundValue DefaultForValueDescriptor(IValueDescriptor valueDescriptor) public System.Object Value { get; } public IValueDescriptor ValueDescriptor { get; } public IValueSource ValueSource { get; } diff --git a/src/System.CommandLine.NamingConventionBinder/ModelBinder.cs b/src/System.CommandLine.NamingConventionBinder/ModelBinder.cs index 9839a997c4..56885c878a 100644 --- a/src/System.CommandLine.NamingConventionBinder/ModelBinder.cs +++ b/src/System.CommandLine.NamingConventionBinder/ModelBinder.cs @@ -287,7 +287,7 @@ internal static (BoundValue? boundValue, bool usedNonDefault) GetBoundValue( { if (valueDescriptor.HasDefaultValue) { - return (BoundValue.DefaultForValueDescriptor(valueDescriptor), false); + return (DefaultForValueDescriptor(valueDescriptor, bindingContext), false); } if (valueDescriptor.ValueType != parentType) // Recursive models aren't allowed @@ -312,12 +312,26 @@ internal static (BoundValue? boundValue, bool usedNonDefault) GetBoundValue( return (new BoundValue(parameterDescriptor.GetDefaultValue(), valueDescriptor, valueSource), false); } - return (BoundValue.DefaultForValueDescriptor(valueDescriptor), false); + return (DefaultForValueDescriptor(valueDescriptor, bindingContext), false); } return (null, false); } + private static BoundValue DefaultForValueDescriptor( + IValueDescriptor valueDescriptor, + BindingContext context) + { + var valueSource = ValueDescriptorDefaultValueSource.Instance; + + valueSource.TryGetValue(valueDescriptor, context, out var value); + + return new BoundValue( + value, + valueDescriptor, + valueSource); + } + private protected IValueDescriptor FindModelPropertyDescriptor(Type propertyType, string propertyName) { return ModelDescriptor.PropertyDescriptors diff --git a/src/System.CommandLine.Rendering.Tests/TerminalModeTests.cs b/src/System.CommandLine.Rendering.Tests/TerminalModeTests.cs index 74cb6d3d71..f4a81ef22d 100644 --- a/src/System.CommandLine.Rendering.Tests/TerminalModeTests.cs +++ b/src/System.CommandLine.Rendering.Tests/TerminalModeTests.cs @@ -44,7 +44,7 @@ public async Task Sets_output_mode_to_Ansi_when_specified_by_output_directive(Ou OutputMode detectedOutputMode = OutputMode.Auto; var command = new Command("hello"); - command.SetHandler((InvocationContext ctx) => + command.SetHandler(ctx => { detectedOutputMode = ctx.Console.DetectOutputMode(); return Task.FromResult(0); diff --git a/src/System.CommandLine.Suggest/SuggestionDispatcher.cs b/src/System.CommandLine.Suggest/SuggestionDispatcher.cs index 242cff0fbf..a048320e48 100644 --- a/src/System.CommandLine.Suggest/SuggestionDispatcher.cs +++ b/src/System.CommandLine.Suggest/SuggestionDispatcher.cs @@ -32,17 +32,16 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug { shellTypeArgument }; - CompleteScriptCommand.SetHandler((InvocationContext context) => + CompleteScriptCommand.SetHandler(context => { SuggestionShellScriptHandler.Handle(context.Console, context.ParseResult.GetValueForArgument(shellTypeArgument)); - return Task.FromResult(0); }); ListCommand = new Command("list") { Description = "Lists apps registered for suggestions", }; - ListCommand.SetHandler((InvocationContext ctx) => + ListCommand.SetHandler(ctx => { ctx.Console.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)); return Task.FromResult(0); @@ -53,7 +52,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug ExecutableOption, PositionOption }; - GetCommand.SetHandler((InvocationContext context) => Get(context)); + GetCommand.SetHandler(context => Get(context)); var commandPathOption = new Option("--command-path", "The path to the command for which to register suggestions"); @@ -63,7 +62,7 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug new Option("--suggestion-command", "The command to invoke to retrieve suggestions") }; - RegisterCommand.SetHandler((InvocationContext context) => + RegisterCommand.SetHandler(context => { Register(context.ParseResult.GetValueForOption(commandPathOption), context.Console); return Task.FromResult(0); diff --git a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs index af87d84660..04c0c35b3e 100644 --- a/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs +++ b/src/System.CommandLine.Tests/Binding/SetHandlerTests.cs @@ -9,214 +9,11 @@ using System.Threading.Tasks; using FluentAssertions; using Xunit; -using static System.Environment; namespace System.CommandLine.Tests.Binding { public class SetHandlerTests { - [Theory] - [InlineData(1)] - [InlineData(2)] - [InlineData(3)] - public void Instances_from_service_provider_can_be_injected_at_any_position_relative_to_symbol_parameters_with_Action_overloads(int @case) - { - var option = new Option("-o"); - var argument = new Argument("value"); - - var command = new RootCommand - { - option, - argument - }; - - ParseResult boundParseResult = default; - bool boundBoolValue = default; - string boundStringValue = default; - switch (@case) - { - case 1: - command.SetHandler((ParseResult parseResult, bool boolValue, string stringValue) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - }, option, argument); - break; - case 2: - command.SetHandler((bool boolValue, ParseResult parseResult, string stringValue) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - }, option, argument); - break; - case 3: - command.SetHandler((bool boolValue, string stringValue, ParseResult parseResult) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - }, option, argument); - break; - } - - command.Invoke("-o hi"); - - boundParseResult.Should().NotBeNull(); - boundBoolValue.Should().BeTrue(); - boundStringValue.Should().Be("hi"); - } - - [Theory] - [InlineData(1)] - [InlineData(2)] - [InlineData(3)] - public void Instances_from_service_provider_can_be_injected_at_any_position_relative_to_symbol_parameters_with_Func_overloads(int @case) - { - var option = new Option("-o"); - var argument = new Argument("value"); - - var command = new RootCommand - { - option, - argument - }; - - ParseResult boundParseResult = default; - bool boundBoolValue = default; - string boundStringValue = default; - switch (@case) - { - case 1: - command.SetHandler((ParseResult parseResult, bool boolValue, string stringValue) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - return Task.FromResult(123); - }, option, argument); - break; - case 2: - command.SetHandler((bool boolValue, ParseResult parseResult, string stringValue) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - return Task.FromResult(123); - }, option, argument); - break; - case 3: - command.SetHandler((bool boolValue, string stringValue, ParseResult parseResult) => - { - boundParseResult = parseResult; - boundBoolValue = boolValue; - boundStringValue = stringValue; - return Task.FromResult(123); - }, option, argument); - break; - } - - command.Invoke("-o hi"); - - boundParseResult.Should().NotBeNull(); - boundBoolValue.Should().BeTrue(); - boundStringValue.Should().Be("hi"); - } - - [Fact] - public void If_parameter_order_does_not_match_symbol_order_then_an_error_results() - { - var boolOption = new Option("-o"); - var stringArg = new Argument("value"); - - var command = new RootCommand - { - boolOption, - stringArg - }; - - var wasCalled = false; - - command.SetHandler((bool boolValue, string stringValue) => wasCalled = true, - stringArg, boolOption); - - var exitCode = command.Invoke("-o hi"); - - wasCalled.Should().BeFalse(); - exitCode.Should().Be(1); - } - - [Fact] - public void If_service_is_not_found_then_an_error_results() - { - var command = new RootCommand(); - - var wasCalled = false; - command.SetHandler((ClassWithMultipleCtor instance) => wasCalled = true); - - var exitCode = command.Invoke(""); - - wasCalled.Should().BeFalse(); - exitCode.Should().Be(1); - } - - [Fact] - public void If_no_symbol_was_passed_for_binding_then_the_error_message_suggests_a_fix_for_the_first_missing_symbol() - { - var boolOption = new Option("-o"); - var stringArg = new Argument("value"); - - var subcommand = new Command("TheCommand") - { - boolOption, - stringArg - }; - - var command = new RootCommand - { - subcommand - }; - - subcommand.SetHandler((bool boolValue, string stringValue) => { }); - - var console = new TestConsole(); - - command.Invoke("TheCommand -o hi", console); - - console.Error.ToString().Should() - .Contain( - $"The SetHandler call for command 'TheCommand' is missing an Argument or Option for the parameter at position 0. Did you mean to pass one of these?{NewLine}Option -o"); - } - - [Fact] - public void If_no_symbol_was_passed_for_binding_subsequent_parameter_then_the_error_message_suggests_a_fix_for_the_first_missing_symbol() - { - var boolOption = new Option("-o"); - var stringArg = new Argument("value"); - - var subcommand = new Command("TheCommand") - { - boolOption, - stringArg - }; - - var command = new RootCommand - { - subcommand - }; - - subcommand.SetHandler((bool boolValue, string stringValue) => { }, boolOption); - - var console = new TestConsole(); - - command.Invoke("TheCommand -o hi", console); - - console.Error.ToString().Should() - .Contain( - $"The SetHandler call for command 'TheCommand' is missing an Argument or Option for the parameter at position 1. Did you mean to pass one of these?{NewLine}Argument value"); - } - [Fact] public void Custom_types_can_be_bound() { @@ -282,14 +79,6 @@ protected override CustomType GetBoundValue(BindingContext bindingContext) [InlineData(6)] [InlineData(7)] [InlineData(8)] - [InlineData(9)] - [InlineData(10)] - [InlineData(11)] - [InlineData(12)] - [InlineData(13)] - [InlineData(14)] - [InlineData(15)] - [InlineData(16)] public void Binding_is_correct_for_Action_overload_having_arity_(int arity) { var command = new RootCommand(); @@ -329,31 +118,7 @@ public void Binding_is_correct_for_Action_overload_having_arity_(int arity) 8 => new Action( (i1, i2, i3, i4, i5, i6, i7, i8) => Received(i1, i2, i3, i4, i5, i6, i7, i8)), - 9 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9)), - 10 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)), - 11 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11)), - 12 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12)), - 13 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13)), - 14 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14)), - 15 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15)), - 16 => new Action( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16)), - + _ => throw new ArgumentOutOfRangeException() }; @@ -374,10 +139,11 @@ public void Binding_is_correct_for_Action_overload_having_arity_(int arity) var parameters = new List { command, - handlerFunc, - command.Arguments.ToArray() + handlerFunc }; + parameters.AddRange(command.Arguments); + setHandler.Invoke(null, parameters.ToArray()); var exitCode = command.Invoke(commandLine); @@ -404,14 +170,6 @@ Task Received(params int[] values) [InlineData(6)] [InlineData(7)] [InlineData(8)] - [InlineData(9)] - [InlineData(10)] - [InlineData(11)] - [InlineData(12)] - [InlineData(13)] - [InlineData(14)] - [InlineData(15)] - [InlineData(16)] public void Binding_is_correct_for_Func_overload_having_arity_(int arity) { var command = new RootCommand(); @@ -451,31 +209,7 @@ public void Binding_is_correct_for_Func_overload_having_arity_(int arity) 8 => new Func( (i1, i2, i3, i4, i5, i6, i7, i8) => Received(i1, i2, i3, i4, i5, i6, i7, i8)), - 9 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9)), - 10 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)), - 11 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11)), - 12 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12)), - 13 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13)), - 14 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14)), - 15 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15)), - 16 => new Func( - (i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16) => - Received(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16)), - + _ => throw new ArgumentOutOfRangeException() }; @@ -497,8 +231,8 @@ public void Binding_is_correct_for_Func_overload_having_arity_(int arity) { command, handlerFunc, - command.Arguments.ToArray() }; + parameters.AddRange(command.Arguments); setHandler.Invoke(null, parameters.ToArray()); @@ -524,7 +258,7 @@ public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_ var command = new Command("wat"); - var handle = (ParseResult _) => + var handle = () => { wasCalled = true; return Task.FromResult(new { NovelType = true }); diff --git a/src/System.CommandLine.Tests/CommandExtensionsTests.cs b/src/System.CommandLine.Tests/CommandExtensionsTests.cs index d7e52d7c2b..a04d072137 100644 --- a/src/System.CommandLine.Tests/CommandExtensionsTests.cs +++ b/src/System.CommandLine.Tests/CommandExtensionsTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.CommandLine.Builder; +using System.CommandLine.Invocation; using System.CommandLine.IO; using System.CommandLine.Parsing; using System.Threading.Tasks; @@ -87,7 +88,7 @@ public void Invoke_extension_method_reuses_implicit_parser_instance() List parsers = new(); var command = new Command("x"); - command.SetHandler((ParseResult result) => parsers.Add(result.Parser)); + command.SetHandler(context => parsers.Add(context.ParseResult.Parser)); command.Invoke(""); command.Invoke(""); @@ -106,7 +107,7 @@ public void Parse_and_Invoke_extension_methods_use_different_implicit_parsers() Parser implicitParserForInvoking = null; Parser implicitParserForParsing = null; - command.SetHandler((ParseResult result) => implicitParserForInvoking = result.Parser); + command.SetHandler(context => implicitParserForInvoking = context.ParseResult.Parser); command.Invoke(""); diff --git a/src/System.CommandLine.Tests/CompilationTests.cs b/src/System.CommandLine.Tests/CompilationTests.cs index d7072a3a92..2694e94e0a 100644 --- a/src/System.CommandLine.Tests/CompilationTests.cs +++ b/src/System.CommandLine.Tests/CompilationTests.cs @@ -71,6 +71,7 @@ private void PublishAndValidate(string appName, string warningText, string addit }, workingDirectory); + stdOut.ToString().Should().NotContain(": error CS"); stdOut.ToString().Should().NotContain(warningText); stdErr.ToString().Should().BeEmpty(); exitCode.Should().Be(0); diff --git a/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs b/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs index cac6f5fb92..ef54677dde 100644 --- a/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs +++ b/src/System.CommandLine.Tests/Invocation/CancelOnProcessTerminationTests.cs @@ -7,7 +7,6 @@ using System.CommandLine.Tests.Utility; using System.Diagnostics; using System.Runtime.InteropServices; -using System.Threading; using System.Threading.Tasks; using FluentAssertions; using Xunit; @@ -32,7 +31,7 @@ public async Task CancelOnProcessTermination_cancels_on_process_termination(int { var command = new Command("the-command"); - command.SetHandler(async (InvocationContext context) => + command.SetHandler(async context => { var cancellationToken = context.GetCancellationToken(); diff --git a/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs b/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs index 24ce8dbbe1..947f94a7c3 100644 --- a/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.cs +++ b/src/System.CommandLine.Tests/Invocation/InvocationExtensionsTests.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.Invocation; using System.CommandLine.IO; using System.Threading.Tasks; using FluentAssertions; @@ -124,7 +123,7 @@ public async Task RootCommand_InvokeAsync_can_set_custom_result_code() { var rootCommand = new RootCommand(); - rootCommand.SetHandler((InvocationContext context) => + rootCommand.SetHandler(context => { context.ExitCode = 123; return Task.CompletedTask; @@ -140,7 +139,7 @@ public void RootCommand_Invoke_can_set_custom_result_code() { var rootCommand = new RootCommand(); - rootCommand.SetHandler((InvocationContext context) => + rootCommand.SetHandler(context => { context.ExitCode = 123; return Task.CompletedTask; diff --git a/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs b/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs index fb8a7f7249..33051dd747 100644 --- a/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs +++ b/src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs @@ -182,7 +182,7 @@ public async Task ParseResult_can_be_replaced_by_middleware() var command = new Command("the-command"); var implicitInnerCommand = new Command("implicit-inner-command"); command.AddCommand(implicitInnerCommand); - implicitInnerCommand.SetHandler((InvocationContext context) => + implicitInnerCommand.SetHandler(context => { wasCalled = true; context.ParseResult.Errors.Should().BeEmpty(); @@ -218,7 +218,7 @@ public async Task Invocation_can_be_short_circuited_by_middleware_by_not_calling var handlerWasCalled = false; var command = new Command("the-command"); - command.SetHandler((InvocationContext context) => + command.SetHandler(context => { handlerWasCalled = true; context.ParseResult.Errors.Should().BeEmpty(); @@ -249,10 +249,10 @@ public void Synchronous_invocation_can_be_short_circuited_by_async_middleware_by var handlerWasCalled = false; var command = new Command("the-command"); - command.SetHandler((ParseResult parseResult) => + command.SetHandler(context => { handlerWasCalled = true; - parseResult.Errors.Should().BeEmpty(); + context.ParseResult.Errors.Should().BeEmpty(); return Task.FromResult(0); }); @@ -279,7 +279,7 @@ public async Task When_no_help_builder_is_specified_it_uses_default_implementati bool handlerWasCalled = false; var command = new Command("help-command"); - command.SetHandler((InvocationContext context) => + command.SetHandler(context => { handlerWasCalled = true; context.HelpBuilder.Should().NotBeNull(); @@ -306,7 +306,7 @@ public async Task When_help_builder_factory_is_specified_it_is_used_to_create_th HelpBuilder createdHelpBuilder = null; var command = new Command("help-command"); - command.SetHandler((InvocationContext context) => + command.SetHandler(context => { handlerWasCalled = true; context.HelpBuilder.Should().Be(createdHelpBuilder); diff --git a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs index 46c967ac00..ca773abbde 100644 --- a/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/NativeAOT/Program.cs @@ -1,16 +1,11 @@ using System; using System.CommandLine; using System.CommandLine.Builder; +using System.CommandLine.Invocation; using System.CommandLine.Parsing; public class Program { - static void Run(bool boolean, string text) - { - Console.WriteLine($"Bool option: {text}"); - Console.WriteLine($"String option: {boolean}"); - } - private static int Main(string[] args) { Option boolOption = new Option(new[] { "--bool", "-b" }, "Bool option"); @@ -22,8 +17,14 @@ private static int Main(string[] args) stringOption }; - command.SetHandler(Run, boolOption, stringOption); + command.SetHandler(Run); return new CommandLineBuilder(command).Build().Invoke(args); + + void Run(InvocationContext context) + { + context.Console.WriteLine($"Bool option: {context.ParseResult.GetValueForOption(boolOption)}"); + context.Console.WriteLine($"String option: {context.ParseResult.GetValueForOption(stringOption)}"); + } } } \ No newline at end of file diff --git a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs index dab823ff86..f81e9d0ec7 100644 --- a/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs +++ b/src/System.CommandLine.Tests/TestApps/Trimming/Program.cs @@ -1,16 +1,16 @@ using System.CommandLine; using System.CommandLine.Invocation; -var fileOption = new Argument().LegalFileNamesOnly(); +var fileArgument = new Argument().LegalFileNamesOnly(); var command = new RootCommand { - fileOption + fileArgument }; -command.SetHandler((FileInfo file, InvocationContext ctx) => +command.SetHandler(context => { - ctx.Console.Write($"The file you chose was: {file}"); -}, fileOption); + context.Console.Write($"The file you chose was: {context.ParseResult.GetValueForArgument(fileArgument)}"); +}); command.Invoke(args); diff --git a/src/System.CommandLine/Binding/BinderBase{T}.cs b/src/System.CommandLine/Binding/BinderBase{T}.cs index 31dcb1baac..94f034acd3 100644 --- a/src/System.CommandLine/Binding/BinderBase{T}.cs +++ b/src/System.CommandLine/Binding/BinderBase{T}.cs @@ -23,14 +23,8 @@ public abstract class BinderBase : object? IValueDescriptor.GetDefaultValue() => default(T); - bool IValueSource.TryGetValue(IValueDescriptor valueDescriptor, BindingContext? bindingContext, out object? boundValue) + bool IValueSource.TryGetValue(IValueDescriptor valueDescriptor, BindingContext bindingContext, out object? boundValue) { - if (bindingContext is null) - { - boundValue = default; - return false; - } - boundValue = GetBoundValue(bindingContext); return true; } diff --git a/src/System.CommandLine/Binding/BoundValue.cs b/src/System.CommandLine/Binding/BoundValue.cs index ba50a45a4e..1b400b73d2 100644 --- a/src/System.CommandLine/Binding/BoundValue.cs +++ b/src/System.CommandLine/Binding/BoundValue.cs @@ -35,22 +35,5 @@ internal BoundValue( /// public override string ToString() => $"{ValueDescriptor}: {Value}"; - - /// - /// Gets a representing the default value for a specified . - /// - /// A value descriptor for which to get the default value. - /// A representing the default value for a specified . - public static BoundValue DefaultForValueDescriptor(IValueDescriptor valueDescriptor) - { - var valueSource = ValueDescriptorDefaultValueSource.Instance; - - valueSource.TryGetValue(valueDescriptor, null, out var value); - - return new BoundValue( - value, - valueDescriptor, - valueSource); - } } } diff --git a/src/System.CommandLine/Binding/IValueSource.cs b/src/System.CommandLine/Binding/IValueSource.cs index 6233493947..48d50413c5 100644 --- a/src/System.CommandLine/Binding/IValueSource.cs +++ b/src/System.CommandLine/Binding/IValueSource.cs @@ -17,7 +17,7 @@ public interface IValueSource /// if a matching value was found; otherwise, . bool TryGetValue( IValueDescriptor valueDescriptor, - BindingContext? bindingContext, + BindingContext bindingContext, out object? boundValue); } } diff --git a/src/System.CommandLine/Binding/ValueDescriptorDefaultValueSource.cs b/src/System.CommandLine/Binding/ValueDescriptorDefaultValueSource.cs index ce72ebd6c0..0fc367198b 100644 --- a/src/System.CommandLine/Binding/ValueDescriptorDefaultValueSource.cs +++ b/src/System.CommandLine/Binding/ValueDescriptorDefaultValueSource.cs @@ -11,7 +11,10 @@ private ValueDescriptorDefaultValueSource() { } - public bool TryGetValue(IValueDescriptor valueDescriptor, BindingContext? bindingContext, out object? boundValue) + public bool TryGetValue( + IValueDescriptor valueDescriptor, + BindingContext bindingContext, + out object? boundValue) { boundValue = valueDescriptor.GetDefaultValue(); return true; diff --git a/src/System.CommandLine/Handler.Action.cs b/src/System.CommandLine/Handler.Action.cs index f3aa9db6ed..ed7039beb0 100644 --- a/src/System.CommandLine/Handler.Action.cs +++ b/src/System.CommandLine/Handler.Action.cs @@ -4,411 +4,202 @@ using System.CommandLine.Binding; using System.CommandLine.Invocation; -namespace System.CommandLine +namespace System.CommandLine; + +/// +/// Provides methods for creating and working with command handlers. +/// +public static partial class Handler { /// - /// Provides methods for creating and working with command handlers. + /// Sets a command's handler based on an . /// - 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()); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, 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, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, 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, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + public static void SetHandler( + this Command command, + Action handle) => + command.Handler = new AnonymousCommandHandler(handle); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!); - }); - - /// - /// Sets a command's handler based on an . - /// - public static void SetHandler( - this Command command, - Action handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + /// + /// Sets a command's handler based on an . + /// + public static void SetHandler( + this Command command, + Action handle) => + command.Handler = new AnonymousCommandHandler(_ => handle()); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// 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!); + }); - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!); - }); + /// + /// 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, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + /// + /// 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!); + }); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); - var value15 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// 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!); + }); - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!, value15!); - }); + /// + /// 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, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + /// + /// 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!); + }); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); - var value15 = GetValueForHandlerParameter(symbols, ref index, context); - var value16 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// 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!); + }); - handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!, value15!, value16!); - }); - } + /// + /// 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 3fbe7bdfda..97f3a9f1e4 100644 --- a/src/System.CommandLine/Handler.Func.cs +++ b/src/System.CommandLine/Handler.Func.cs @@ -5,411 +5,202 @@ using System.CommandLine.Invocation; using System.Threading.Tasks; -namespace System.CommandLine +namespace System.CommandLine; + +/// +/// Provides methods for creating and working with command handlers. +/// +public static partial class Handler { /// - /// Provides methods for creating and working with command handlers. + /// Sets a command's handler based on a . /// - 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()); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); + public static void SetHandler( + this Command command, + Func handle) => + command.Handler = new AnonymousCommandHandler(_ => handle()); - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!); - }); - - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; - - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// Sets a command's handler based on a . + /// + 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 => + { + var value1 = GetValueForHandlerParameter(symbol, context); + + return handle(value1!); + }); - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!); - }); + /// + /// 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 => + { + var value1 = GetValueForHandlerParameter(symbol1, context); + var value2 = GetValueForHandlerParameter(symbol2, context); + + return handle(value1!, value2!); + }); - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + /// + /// 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 => + { + var value1 = GetValueForHandlerParameter(symbol1, context); + var value2 = GetValueForHandlerParameter(symbol2, context); + var value3 = GetValueForHandlerParameter(symbol3, context); + + return handle(value1!, value2!, value3!); + }); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); - var value15 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// 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 => + { + 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!); + }); - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!, value15!); - }); + /// + /// 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 => + { + 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!); + }); - /// - /// Sets a command's handler based on a . - /// - public static void SetHandler( - this Command command, - Func handle, - params IValueDescriptor[] symbols) => - command.Handler = new AnonymousCommandHandler( - context => - { - var index = 0; + /// + /// 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 => + { + 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!); + }); - var value1 = GetValueForHandlerParameter(symbols, ref index, context); - var value2 = GetValueForHandlerParameter(symbols, ref index, context); - var value3 = GetValueForHandlerParameter(symbols, ref index, context); - var value4 = GetValueForHandlerParameter(symbols, ref index, context); - var value5 = GetValueForHandlerParameter(symbols, ref index, context); - var value6 = GetValueForHandlerParameter(symbols, ref index, context); - var value7 = GetValueForHandlerParameter(symbols, ref index, context); - var value8 = GetValueForHandlerParameter(symbols, ref index, context); - var value9 = GetValueForHandlerParameter(symbols, ref index, context); - var value10 = GetValueForHandlerParameter(symbols, ref index, context); - var value11 = GetValueForHandlerParameter(symbols, ref index, context); - var value12 = GetValueForHandlerParameter(symbols, ref index, context); - var value13 = GetValueForHandlerParameter(symbols, ref index, context); - var value14 = GetValueForHandlerParameter(symbols, ref index, context); - var value15 = GetValueForHandlerParameter(symbols, ref index, context); - var value16 = GetValueForHandlerParameter(symbols, ref index, context); + /// + /// 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 => + { + 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!); + }); - return handle(value1!, value2!, value3!, value4!, value5!, value6!, value7!, value8!, value9!, value10!, value11!, value12!, value13!, value14!, value15!, value16!); - }); - } + /// + /// 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 => + { + 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!); + }); } \ No newline at end of file diff --git a/src/System.CommandLine/Handler.cs b/src/System.CommandLine/Handler.cs index 3b2a6211dc..a60a41d361 100644 --- a/src/System.CommandLine/Handler.cs +++ b/src/System.CommandLine/Handler.cs @@ -3,75 +3,24 @@ using System.CommandLine.Binding; using System.CommandLine.Invocation; -using System.CommandLine.Parsing; -using System.Linq; -using static System.Environment; namespace System.CommandLine; public static partial class Handler { private static T? GetValueForHandlerParameter( - IValueDescriptor[] symbols, - ref int index, + IValueDescriptor symbol, InvocationContext context) { - if (symbols.Length > index && - symbols[index] is IValueDescriptor symbol) + if (symbol is IValueSource valueSource && + valueSource.TryGetValue(symbol, context.BindingContext, out var boundValue) && + boundValue is T value) { - index++; - - 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); - } + return value; } - - return GetService(ref index, context); // kept in separate method to avoid jitting rare code path - } - - private static T? GetService(ref int index, InvocationContext context) - { - var service = context.BindingContext.GetService(typeof(T)); - - if (service is null) + else { - var candidates = context.ParseResult - .RootCommandResult - .AllSymbolResults() - .Where(r => r.Symbol.Parents.All(p => p is not Option)) - .Select(r => r.Symbol) - .OfType() - .ToArray(); - - if (candidates.Any()) - { - var candidatesDescription = string.Join( - NewLine, - candidates - .Where(c => typeof(T).IsAssignableFrom(c.ValueType)) - .Select(c => c switch - { - Argument argument => $"{nameof(Argument)}<{argument.ValueType.Name}> {argument.Name}", - Argument argument => $"{nameof(Argument)} {argument.Name}", - Option option => $"{nameof(Option)}<{option.ValueType.Name}> {option.Aliases.First()}", - Option option => $"{nameof(Option)} {option.Aliases.First()}", - _ => throw new ArgumentOutOfRangeException(nameof(c)) - })); - - throw new ArgumentException( - $"The {nameof(SetHandler)} call for command '{context.ParseResult.CommandResult.Command.Name}' is missing an {nameof(Argument)} or {nameof(Option)} for the parameter at position {index}. Did you mean to pass one of these?{NewLine}{candidatesDescription}"); - } - - throw new ArgumentException($"Service not found for type {typeof(T)}."); + return context.ParseResult.GetValueFor(symbol); } - - return (T)service; } } \ No newline at end of file diff --git a/src/System.CommandLine/Help/HelpBuilder.Default.cs b/src/System.CommandLine/Help/HelpBuilder.Default.cs index 61c32af136..67f19f5d84 100644 --- a/src/System.CommandLine/Help/HelpBuilder.Default.cs +++ b/src/System.CommandLine/Help/HelpBuilder.Default.cs @@ -219,7 +219,7 @@ public static HelpSectionDelegate OptionsSection() => { if ((parentCommand = parent.Symbol as Command) is not null) { - foreach (Option option in parentCommand.Options) + foreach (var option in parentCommand.Options) { // global help aliases may be duplicated, we just ignore them if (option.IsGlobal && !option.IsHidden && uniqueOptions.Add(option)) diff --git a/src/System.CommandLine/Help/HelpBuilder.cs b/src/System.CommandLine/Help/HelpBuilder.cs index d4d5cd41da..4eb16a21ca 100644 --- a/src/System.CommandLine/Help/HelpBuilder.cs +++ b/src/System.CommandLine/Help/HelpBuilder.cs @@ -121,7 +121,7 @@ IEnumerable GetUsageParts() .RecurseWhileNotNull(c => c.Parents.OfType().FirstOrDefault()) .Reverse(); - foreach (Command parentCommand in parentCommands) + foreach (var parentCommand in parentCommands) { if (!displayOptionTitle) {