Ejemplo n.º 1
0
        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 <int> .HandleAsync));

            var handler = HandlerDescriptor.FromMethodInfo(handlerMethod)
                          .GetCommandHandler();

            var command = new Command("the-command")
            {
                Argument = 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());
        }
Ejemplo n.º 2
0
        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")
                {
                    Argument = new Argument
                    {
                        Name         = "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}");
        }
Ejemplo n.º 3
0
            public void It_provides_the_types_of_the_handler_parameters(Type parameterType)
            {
                var method = typeof(HandlerDescriptorTests).GetMethod(nameof(Handler)).MakeGenericMethod(parameterType);

                var descriptor = HandlerDescriptor.FromMethodInfo(method);

                descriptor.ParameterDescriptors
                .Select(p => p.ValueType)
                .Should()
                .BeEquivalentSequenceTo(parameterType);
            }
Ejemplo n.º 4
0
        public static Command GetCommand()
        {
            var bench = new Command("benchmark", "Benchmark a single puzzle")
            {
                new Argument <string>(() => "SQ1~P13")
                {
                    Name        = "puzzle",
                    Description = "Puzzle Identifier in the form LIB~PUZ (can be regex)"
                },
                new Option <int>(new[] { "--min", "-m" }, "TimeOut in minutes")
                {
                    Name = "min",
                },
                new Option <int>(new[] { "--sec", "-s" }, "TimeOut in seconds")
                {
                    Name = "sec",
                },
                new Option <string>(new[] { "--solver", "-t" }, "Solver Strategy")
                {
                    Name = "solver",
                },
                new Option <string>(new[] { "--pool", "-p" }, "ISolverPool Type")
                {
                    Name = "pool",
                },
                new Option <double>(new[] { "--max-rating", "-maxR" }, "Max Puzzle Rating")
                {
                    Name = "maxR",
                },
                new Option <double>(new[] { "--min-rating", "-minR" }, "Min Puzzle Rating")
                {
                    Name = "minR",
                },
                new Option <string>(new[] { "--save" }, "Save tree to file")
                {
                    Name = "save",
                }
            };

            bench.Handler = HandlerDescriptor.FromMethodInfo(
                typeof(BenchmarkCommand).GetMethod("Run"),
                new BenchmarkCommand())
                            .GetCommandHandler();
            return(bench);
        }
Ejemplo n.º 5
0
        protected void AddSubCommands(Command command, IEnumerable <ICommand> subCommands)
        {
            foreach (var projectSubCommand in subCommands)
            {
                var c = projectSubCommand.BuildCommand();

                var projectSubCommandType = projectSubCommand.GetType();
                var methods = projectSubCommandType
                              .GetMethods()
                              .Where(x => x.Name == nameof(CommandAction));

                var methodInfo = methods.First();

                if (methodInfo.DeclaringType == projectSubCommandType)
                {
                    c.Handler = HandlerDescriptor.FromMethodInfo(methodInfo, projectSubCommand).GetCommandHandler();
                }

                command.AddCommand(c);
            }
        }
Ejemplo n.º 6
0
        public static IEnumerable <Option> BuildOptions(this MethodInfo method)
        {
            var descriptor = HandlerDescriptor.FromMethodInfo(method);

            var omittedTypes = new[]
            {
                typeof(IConsole),
                typeof(InvocationContext),
                typeof(BindingContext),
                typeof(ParseResult),
                typeof(CancellationToken),
            };

            foreach (var option in descriptor.ParameterDescriptors
                     .Where(d => !omittedTypes.Contains(d.ValueType))
                     .Where(d => !_argumentParameterNames.Contains(d.ValueName))
                     .Select(p => p.BuildOption()))
            {
                yield return(option);
            }
        }
 public static ICommandHandler Create(MethodInfo method, object?target = null) =>
 HandlerDescriptor.FromMethodInfo(method, target).GetCommandHandler();
 public static ICommandHandler Create(MethodInfo method) =>
 HandlerDescriptor.FromMethodInfo(method).GetCommandHandler();