public async Task Unspecified_option_arguments_with_no_default_value_are_bound_to_type_default(
        Type parameterType,
        object expectedValue)
    {
        var captureMethod = GetType()
                            .GetMethod(nameof(CaptureMethod), BindingFlags.NonPublic | BindingFlags.Static)
                            .MakeGenericMethod(parameterType);

        var handler = CommandHandler.Create(captureMethod);

        var command = new Command("command")
        {
            OptionBuilder.CreateOption("-x", parameterType)
        };

        command.Handler = handler;

        var parseResult = command.Parse("");

        var invocationContext = new InvocationContext(parseResult);

        await handler.InvokeAsync(invocationContext);

        var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;

        boundValue.Should().Be(expectedValue);
    }
        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")
            {
                OptionBuilder.CreateOption("--value", type)
            };

            var console = new TestConsole();

            await handler.InvokeAsync(
                new InvocationContext(command.Parse(commandLine), console));

            console.Out.ToString().Should().Be($"ClassWithCtorParameter<{type.Name}>: {expectedValue}");
        }
        public void Max_arity_greater_than_1_converts_to_enumerable_types(
            int minArity,
            int maxArity,
            Type argumentType)
        {
            var option = OptionBuilder.CreateOption("--items", valueType: argumentType);

            option.Arity = new ArgumentArity(minArity, maxArity);

            var command = new RootCommand
            {
                option
            };

            var result = command.Parse("--items one --items two --items three");

            result.Errors.Should().BeEmpty();
            result.FindResultFor(option).GetValueOrDefault().Should().BeAssignableTo(argumentType);
        }
Esempio n. 4
0
        public static Option BuildOption(this ParameterDescriptor parameter)
        {
            Func <object> getDefaultValue = null;

            if (parameter.HasDefaultValue)
            {
                getDefaultValue = parameter.GetDefaultValue;
            }

            var option = OptionBuilder.CreateOption(parameter.BuildAlias(), parameter.ValueType);

            option.Description = parameter.ValueName;

            if (getDefaultValue is not null)
            {
                option.SetDefaultValueFactory(getDefaultValue);
            }

            return(option);
        }
    public void Option_arguments_are_bound_by_name_to_constructor_parameters(
        Type type,
        string commandLine,
        object expectedValue)
    {
        var targetType = typeof(ClassWithCtorParameter <>).MakeGenericType(type);
        var binder     = new ModelBinder(targetType);

        var command = new Command("the-command")
        {
            OptionBuilder.CreateOption("--value", type)
        };

        var bindingContext = new InvocationContext(command.Parse(commandLine)).BindingContext;

        var instance = binder.CreateInstance(bindingContext);

        object valueReceivedValue = ((dynamic)instance).Value;

        valueReceivedValue.Should().Be(expectedValue);
    }