diff --git a/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingByName.cs b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingByName.cs new file mode 100644 index 0000000000..eb3060009b --- /dev/null +++ b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingByName.cs @@ -0,0 +1,167 @@ +// // 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.CommandLine.IO; +using System.IO; +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace System.CommandLine.Tests.Binding +{ + public partial class ModelBindingCommandHandlerTests + { + public class BindingByName + { + [Theory] + [InlineData(typeof(bool), "--value", true)] + [InlineData(typeof(bool), "--value false", false)] + [InlineData(typeof(string), "--value hello", "hello")] + [InlineData(typeof(int), "--value 123", 123)] + public async Task Option_arguments_are_bound_by_name_to_method_parameters( + Type type, + string commandLine, + object expectedValue) + { + var targetType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(type); + + var handlerMethod = targetType.GetMethod(nameof(ClassWithMethodHavingParameter.HandleAsync)); + + var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) + .GetCommandHandler(); + + var command = new Command("the-command") + { + new Option("--value", argumentType: type) + }; + + var console = new TestConsole(); + + await handler.InvokeAsync( + new InvocationContext(command.Parse(commandLine), console)); + + console.Out.ToString().Should().Be(expectedValue.ToString()); + } + + [Theory] + [InlineData(typeof(bool), "--value", true)] + [InlineData(typeof(bool), "--value false", false)] + [InlineData(typeof(string), "--value hello", "hello")] + [InlineData(typeof(int), "--value 123", 123)] + public async Task Option_arguments_are_bound_by_name_to_the_properties_of_method_parameters( + Type type, + string commandLine, + object expectedValue) + { + var complexParameterType = typeof(ClassWithSetter<>).MakeGenericType(type); + + var handlerType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(complexParameterType); + + var handlerMethod = handlerType.GetMethod("HandleAsync"); + + var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) + .GetCommandHandler(); + + var command = new Command("the-command") + { + new Option("--value", argumentType: type) + }; + + var console = new TestConsole(); + + await handler.InvokeAsync( + new InvocationContext(command.Parse(commandLine), console)); + + console.Out.ToString().Should().Be($"ClassWithSetter<{type.Name}>: {expectedValue}"); + } + + [Theory] + [InlineData(typeof(bool), "--value", true)] + [InlineData(typeof(bool), "--value false", false)] + [InlineData(typeof(string), "--value hello", "hello")] + [InlineData(typeof(int), "--value 123", 123)] + public async Task Option_arguments_are_bound_by_name_to_the_constructor_parameters_of_method_parameters( + Type type, + string commandLine, + object expectedValue) + { + var complexParameterType = typeof(ClassWithCtorParameter<>).MakeGenericType(type); + + var handlerType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(complexParameterType); + + var handlerMethod = handlerType.GetMethod("HandleAsync"); + + var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) + .GetCommandHandler(); + + var command = new Command("the-command") + { + new Option("--value", argumentType: type) + }; + + var console = new TestConsole(); + + await handler.InvokeAsync( + new InvocationContext(command.Parse(commandLine), console)); + + console.Out.ToString().Should().Be($"ClassWithCtorParameter<{type.Name}>: {expectedValue}"); + } + + [Theory] + [InlineData(typeof(string), "hello", "hello")] + [InlineData(typeof(int), "123", 123)] + public async Task Command_arguments_are_bound_by_name_to_handler_method_parameters( + Type type, + string commandLine, + object expectedValue) + { + var targetType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(type); + + var handlerMethod = targetType.GetMethod(nameof(ClassWithMethodHavingParameter.HandleAsync)); + + var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) + .GetCommandHandler(); + + var command = new Command("the-command") + { + new Argument + { + Name = "value", + ArgumentType = type + } + }; + + var console = new TestConsole(); + + await handler.InvokeAsync( + new InvocationContext(command.Parse(commandLine), console)); + + console.Out.ToString().Should().Be(expectedValue.ToString()); + } + + [Fact] + public void When_argument_type_is_more_specific_than_parameter_type_then_parameter_is_bound_correctly() + { + FileSystemInfo received = null; + + var root = new RootCommand + { + new Option("-f") + }; + root.Handler = CommandHandler.Create(f => received = f); + var path = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}"; + + root.Invoke($"-f {path}"); + + received.Should() + .BeOfType() + .Which + .FullName + .Should() + .Be(path); + } + } + } +} \ No newline at end of file diff --git a/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingBySymbol.cs b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingBySymbol.cs new file mode 100644 index 0000000000..f86b3bf91a --- /dev/null +++ b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.BindingBySymbol.cs @@ -0,0 +1,138 @@ +// 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.Collections.Generic; +using System.CommandLine.Invocation; +using System.Linq; +using FluentAssertions; +using System.Threading.Tasks; +using Xunit; + +namespace System.CommandLine.Tests.Binding +{ + public partial class ModelBindingCommandHandlerTests + { + public class BindingBySymbol + { + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(4)] + [InlineData(5)] + [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_overload_having_arity_(int arity) + { + var command = new RootCommand(); + var commandLine = ""; + + for (var i = 1; i <= arity; i++) + { + command.AddArgument(new Argument($"i{i}")); + + commandLine += $" {i}"; + } + + var receivedValues = new List(); + Delegate handlerFunc = arity switch + { + 1 => new Func( + i1 => + Received(i1)), + 2 => new Func( + (i1, i2) => + Received(i1, i2)), + 3 => new Func( + (i1, i2, i3) => + Received(i1, i2, i3)), + 4 => new Func( + (i1, i2, i3, i4) => + Received(i1, i2, i3, i4)), + 5 => new Func( + (i1, i2, i3, i4, i5) => + Received(i1, i2, i3, i4, i5)), + 6 => new Func( + (i1, i2, i3, i4, i5, i6) => + Received(i1, i2, i3, i4, i5, i6)), + 7 => new Func( + (i1, i2, i3, i4, i5, i6, i7) => + Received(i1, i2, i3, i4, i5, i6, i7)), + 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() + }; + + // build up the method invocation + var genericMethodDef = typeof(CommandHandler) + .GetMethods() + .Where(m => m.Name == nameof(CommandHandler.Create)) + .Where(m => m.IsGenericMethod /* symbols + handler Func */) + .Single(m => m.GetParameters().Length == arity + 1); + + var genericParameterTypes = Enumerable.Range(1, arity) + .Select(_ => typeof(int)) + .ToArray(); + + var createMethod = genericMethodDef.MakeGenericMethod(genericParameterTypes); + + var parameters = new List(); + + parameters.AddRange(command.Arguments); + parameters.Add(handlerFunc); + + var handler = (ICommandHandler) createMethod.Invoke(null, parameters.ToArray()); + + command.Handler = handler; + + command.Invoke(commandLine); + + receivedValues.Should().BeEquivalentTo( + Enumerable.Range(1, arity), + config => config.WithStrictOrdering()); + + Task Received(params int[] values) + { + receivedValues.AddRange(values); + return Task.CompletedTask; + } + } + } + } +} \ No newline at end of file diff --git a/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.cs b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.cs index feb14d999e..3899c396a3 100644 --- a/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.cs +++ b/src/System.CommandLine.Tests/Binding/ModelBindingCommandHandlerTests.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; -using System.CommandLine.Binding; using System.CommandLine.Invocation; using System.CommandLine.IO; using System.IO; @@ -15,134 +14,8 @@ namespace System.CommandLine.Tests.Binding { - public class ModelBindingCommandHandlerTests + public partial class ModelBindingCommandHandlerTests { - [Theory] - [InlineData(typeof(bool), "--value", true)] - [InlineData(typeof(bool), "--value false", false)] - [InlineData(typeof(string), "--value hello", "hello")] - [InlineData(typeof(int), "--value 123", 123)] - public async Task Option_arguments_are_bound_by_name_to_method_parameters( - Type type, - string commandLine, - object expectedValue) - { - var targetType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(type); - - var handlerMethod = targetType.GetMethod(nameof(ClassWithMethodHavingParameter.HandleAsync)); - - var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) - .GetCommandHandler(); - - var command = new Command("the-command") - { - new Option("--value", argumentType: type) - }; - - var console = new TestConsole(); - - await handler.InvokeAsync( - new InvocationContext(command.Parse(commandLine), console)); - - console.Out.ToString().Should().Be(expectedValue.ToString()); - } - - [Theory] - [InlineData(typeof(bool), "--value", true)] - [InlineData(typeof(bool), "--value false", false)] - [InlineData(typeof(string), "--value hello", "hello")] - [InlineData(typeof(int), "--value 123", 123)] - public async Task Option_arguments_are_bound_by_name_to_the_properties_of_method_parameters( - Type type, - string commandLine, - object expectedValue) - { - var complexParameterType = typeof(ClassWithSetter<>).MakeGenericType(type); - - var handlerType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(complexParameterType); - - var handlerMethod = handlerType.GetMethod("HandleAsync"); - - var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) - .GetCommandHandler(); - - var command = new Command("the-command") - { - new Option("--value", argumentType: type) - }; - - var console = new TestConsole(); - - await handler.InvokeAsync( - new InvocationContext(command.Parse(commandLine), console)); - - console.Out.ToString().Should().Be($"ClassWithSetter<{type.Name}>: {expectedValue}"); - } - - [Theory] - [InlineData(typeof(bool), "--value", true)] - [InlineData(typeof(bool), "--value false", false)] - [InlineData(typeof(string), "--value hello", "hello")] - [InlineData(typeof(int), "--value 123", 123)] - public async Task Option_arguments_are_bound_by_name_to_the_constructor_parameters_of_method_parameters( - Type type, - string commandLine, - object expectedValue) - { - var complexParameterType = typeof(ClassWithCtorParameter<>).MakeGenericType(type); - - var handlerType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(complexParameterType); - - var handlerMethod = handlerType.GetMethod("HandleAsync"); - - var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) - .GetCommandHandler(); - - var command = new Command("the-command") - { - new Option("--value", argumentType: type) - }; - - var console = new TestConsole(); - - await handler.InvokeAsync( - new InvocationContext(command.Parse(commandLine), console)); - - console.Out.ToString().Should().Be($"ClassWithCtorParameter<{type.Name}>: {expectedValue}"); - } - - [Theory] - [InlineData(typeof(string), "hello", "hello")] - [InlineData(typeof(int), "123", 123)] - public async Task Command_arguments_are_bound_by_name_to_handler_method_parameters( - Type type, - string commandLine, - object expectedValue) - { - var targetType = typeof(ClassWithMethodHavingParameter<>).MakeGenericType(type); - - var handlerMethod = targetType.GetMethod(nameof(ClassWithMethodHavingParameter.HandleAsync)); - - var handler = HandlerDescriptor.FromMethodInfo(handlerMethod) - .GetCommandHandler(); - - var command = new Command("the-command") - { - new Argument - { - Name = "value", - ArgumentType = type - } - }; - - var console = new TestConsole(); - - await handler.InvokeAsync( - new InvocationContext(command.Parse(commandLine), console)); - - console.Out.ToString().Should().Be(expectedValue.ToString()); - } - [Theory] [InlineData(typeof(string), "")] [InlineData(typeof(FileInfo), null)] @@ -212,28 +85,6 @@ public async Task When_argument_type_is_not_known_until_binding_then_int_paramet received.Should().Be(123); } - [Fact] - public void When_argument_type_is_more_specific_than_parameter_type_then_parameter_is_bound_correctly() - { - FileSystemInfo received = null; - - var root = new RootCommand - { - new Option("-f") - }; - root.Handler = CommandHandler.Create(f => received = f); - var path = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}"; - - root.Invoke($"-f {path}"); - - received.Should() - .BeOfType() - .Which - .FullName - .Should() - .Be(path); - } - [Theory] [InlineData(typeof(ClassWithCtorParameter), false)] [InlineData(typeof(ClassWithCtorParameter), true)] @@ -292,7 +143,7 @@ public async Task Handler_method_receives_option_arguments_bound_to_the_specifie var @delegate = createCaptureDelegate.Invoke(null, null); - handler = CommandHandler.Create((dynamic)@delegate); + handler = CommandHandler.Create((Delegate)@delegate); } var command = new Command("command") diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs index 576956c9ad..902a97ba97 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs @@ -28,8 +28,7 @@ public void Help_describes_default_values_for_complex_root_command_scenario() }, new Argument("the-root-arg-enum-default", () => FileAccess.Read) { - Description = "the-root-arg-enum-default-description", - ArgumentType = typeof(FileAccess) + Description = "the-root-arg-enum-default-description" }, new Option(aliases: new string[] {"--the-root-option-no-arg", "-trna"}) { Description = "the-root-option-no-arg-description", diff --git a/src/System.CommandLine/Argument{T}.cs b/src/System.CommandLine/Argument{T}.cs index a77e9a8414..7f2acc80e9 100644 --- a/src/System.CommandLine/Argument{T}.cs +++ b/src/System.CommandLine/Argument{T}.cs @@ -1,19 +1,19 @@ // 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 { - /// - public class Argument : Argument + /// + public class Argument : Argument, IValueDescriptor { /// /// Initializes a new instance of the Argument class. /// public Argument() { - ArgumentType = typeof(T); } /// @@ -25,7 +25,6 @@ public Argument( string name, string? description = null) : base(name) { - ArgumentType = typeof(T); Description = description; } @@ -122,5 +121,12 @@ public Argument( public Argument(ParseArgument parse, bool isDefault = false) : this(null, parse, isDefault) { } + + /// + public override Type ArgumentType + { + get => typeof(T); + set => throw new NotImplementedException(); + } } } diff --git a/src/System.CommandLine/Binding/HandlerDescriptor.cs b/src/System.CommandLine/Binding/HandlerDescriptor.cs index 64c8cbb5c5..0edaf4b707 100644 --- a/src/System.CommandLine/Binding/HandlerDescriptor.cs +++ b/src/System.CommandLine/Binding/HandlerDescriptor.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.CommandLine.Invocation; -using System.Linq; using System.Reflection; namespace System.CommandLine.Binding diff --git a/src/System.CommandLine/Binding/IValueDescriptor.cs b/src/System.CommandLine/Binding/IValueDescriptor.cs index 4468f42063..6a344d9759 100644 --- a/src/System.CommandLine/Binding/IValueDescriptor.cs +++ b/src/System.CommandLine/Binding/IValueDescriptor.cs @@ -13,4 +13,9 @@ public interface IValueDescriptor object? GetDefaultValue(); } + + public interface IValueDescriptor : IValueDescriptor + { + // FIX: (IValueDescriptor) + } } diff --git a/src/System.CommandLine/Invocation/CommandHandler.NameBasedBinding.cs b/src/System.CommandLine/Invocation/CommandHandler.NameBasedBinding.cs new file mode 100644 index 0000000000..d2757ba52e --- /dev/null +++ b/src/System.CommandLine/Invocation/CommandHandler.NameBasedBinding.cs @@ -0,0 +1,282 @@ +// 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.Reflection; +using System.Threading.Tasks; + +namespace System.CommandLine.Invocation +{ + public static partial class CommandHandler + { + public static ICommandHandler Create(Delegate @delegate) => + HandlerDescriptor.FromDelegate(@delegate).GetCommandHandler(); + + public static ICommandHandler Create(MethodInfo method, object? target = null) => + HandlerDescriptor.FromMethodInfo(method, target).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Action action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create(Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create(Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create(Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + + public static ICommandHandler Create( + Func> action) => + HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + } +} \ No newline at end of file diff --git a/src/System.CommandLine/Invocation/CommandHandler.cs b/src/System.CommandLine/Invocation/CommandHandler.cs index 6b6464852e..26962c1e0c 100644 --- a/src/System.CommandLine/Invocation/CommandHandler.cs +++ b/src/System.CommandLine/Invocation/CommandHandler.cs @@ -2,285 +2,379 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CommandLine.Binding; -using System.Reflection; using System.Threading.Tasks; namespace System.CommandLine.Invocation { - public static class CommandHandler + public static partial class CommandHandler { - public static ICommandHandler Create(Delegate @delegate) => - HandlerDescriptor.FromDelegate(@delegate).GetCommandHandler(); - - public static ICommandHandler Create(MethodInfo method, object? target = null) => - HandlerDescriptor.FromMethodInfo(method, target).GetCommandHandler(); - public static ICommandHandler Create(Action action) => HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + public static ICommandHandler Create( + IValueDescriptor symbol1, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + IValueDescriptor symbol12, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!, + context.ParseResult.ValueFor(symbol12)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + IValueDescriptor symbol12, + IValueDescriptor symbol13, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!, + context.ParseResult.ValueFor(symbol12)!, + context.ParseResult.ValueFor(symbol13)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + IValueDescriptor symbol12, + IValueDescriptor symbol13, + IValueDescriptor symbol14, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!, + context.ParseResult.ValueFor(symbol12)!, + context.ParseResult.ValueFor(symbol13)!, + context.ParseResult.ValueFor(symbol14)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + IValueDescriptor symbol12, + IValueDescriptor symbol13, + IValueDescriptor symbol14, + IValueDescriptor symbol15, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!, + context.ParseResult.ValueFor(symbol12)!, + context.ParseResult.ValueFor(symbol13)!, + context.ParseResult.ValueFor(symbol14)!, + context.ParseResult.ValueFor(symbol15)!)); public static ICommandHandler Create( - Action action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create(Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create(Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create(Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); - - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + IValueDescriptor symbol1, + IValueDescriptor symbol2, + IValueDescriptor symbol3, + IValueDescriptor symbol4, + IValueDescriptor symbol5, + IValueDescriptor symbol6, + IValueDescriptor symbol7, + IValueDescriptor symbol8, + IValueDescriptor symbol9, + IValueDescriptor symbol10, + IValueDescriptor symbol11, + IValueDescriptor symbol12, + IValueDescriptor symbol13, + IValueDescriptor symbol14, + IValueDescriptor symbol15, + IValueDescriptor symbol16, + Func handle) => + new AnonymousCommandHandler( + async context => await handle( + context.ParseResult.ValueFor(symbol1)!, + context.ParseResult.ValueFor(symbol2)!, + context.ParseResult.ValueFor(symbol3)!, + context.ParseResult.ValueFor(symbol4)!, + context.ParseResult.ValueFor(symbol5)!, + context.ParseResult.ValueFor(symbol6)!, + context.ParseResult.ValueFor(symbol7)!, + context.ParseResult.ValueFor(symbol8)!, + context.ParseResult.ValueFor(symbol9)!, + context.ParseResult.ValueFor(symbol10)!, + context.ParseResult.ValueFor(symbol11)!, + context.ParseResult.ValueFor(symbol12)!, + context.ParseResult.ValueFor(symbol13)!, + context.ParseResult.ValueFor(symbol14)!, + context.ParseResult.ValueFor(symbol15)!, + context.ParseResult.ValueFor(symbol16)!)); + + private class AnonymousCommandHandler : ICommandHandler + { + private readonly Func _getResult; - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + public AnonymousCommandHandler(Func getResult) + { + _getResult = getResult; + } - public static ICommandHandler Create( - Func> action) => - HandlerDescriptor.FromDelegate(action).GetCommandHandler(); + public Task InvokeAsync(InvocationContext context) => + GetExitCodeAsync(_getResult(context), context); + } internal static async Task GetExitCodeAsync(object value, InvocationContext context) { diff --git a/src/System.CommandLine/Option.cs b/src/System.CommandLine/Option.cs index d9139b0ccd..ac1e36dda1 100644 --- a/src/System.CommandLine/Option.cs +++ b/src/System.CommandLine/Option.cs @@ -140,7 +140,7 @@ public string ArgumentHelpName /// /// Gets or sets the arity of the option. /// - public IArgumentArity Arity + public virtual IArgumentArity Arity { get => Argument.Arity; init diff --git a/src/System.CommandLine/Option{T}.cs b/src/System.CommandLine/Option{T}.cs index dbdea0ebb7..218dacd9da 100644 --- a/src/System.CommandLine/Option{T}.cs +++ b/src/System.CommandLine/Option{T}.cs @@ -1,11 +1,12 @@ // 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 { - public class Option : Option + public class Option : Option, IValueDescriptor { public Option( string alias, @@ -47,8 +48,15 @@ public Option( public Option( string[] aliases, Func getDefaultValue, - string? description = null) + string? description = null) : base(aliases, description, new Argument(getDefaultValue ?? throw new ArgumentNullException(nameof(getDefaultValue)))) - { } + { + } + + public override IArgumentArity Arity + { + get => base.Arity; + init => Argument.Arity = value; + } } } \ No newline at end of file diff --git a/src/System.CommandLine/Parsing/ParseResult.cs b/src/System.CommandLine/Parsing/ParseResult.cs index e4704f245f..4818237436 100644 --- a/src/System.CommandLine/Parsing/ParseResult.cs +++ b/src/System.CommandLine/Parsing/ParseResult.cs @@ -84,6 +84,16 @@ internal ParseResult( public IReadOnlyList UnparsedTokens => _unparsedTokens.Select(t => t.Value).ToArray(); + [return: MaybeNull] + internal T ValueFor(IValueDescriptor symbol) => + symbol switch + { + Argument argument => ValueForArgument(argument), + Option option => ValueForOption(option), + _ => throw new ArgumentOutOfRangeException() + }; + + [Obsolete("This method is obsolete and will be removed in a future version. Please use ParseResult.ValueForOption(Option) instead. For details see https://github.com/dotnet/command-line-api/issues/1127")] public object? ValueForOption(string alias) => ValueForOption(alias);