示例#1
0
        public void PropertyInfo_can_be_bound_to_argument()
        {
            var command  = new Command("the-command");
            var argument = new Argument <int> {
                Arity = ArgumentArity.ExactlyOne
            };

            command.AddArgument(argument);

            var type         = typeof(ClassWithMultiLetterSetters);
            var binder       = new ModelBinder(type);
            var propertyInfo = type.GetProperty(nameof(ClassWithMultiLetterSetters.IntOption));

            binder.BindMemberFromValue(propertyInfo, argument);

            var bindingContext = new BindingContext(command.Parse("the-command 42"));

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(42);
        }
        public void PropertyExpression_can_be_bound_to_argument()
        {
            var command  = new Command("the-command");
            var argument = new Argument <int> {
                Arity = ArgumentArity.ExactlyOne
            };

            command.AddArgument(argument);

            var binder = new ModelBinder <ClassWithMultiLetterSetters>();

            binder.BindMemberFromValue(
                i => i.IntOption,
                argument);

            var bindingContext = new BindingContext(command.Parse("the-command 42"));

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(42);
        }
        public void PropertyInfo_can_be_bound_to_option()
        {
            var command = new Command("the-command");
            var option  = new Option <int>("--fred");

            command.Add(option);

            var type         = typeof(ClassWithMultiLetterSetters);
            var binder       = new ModelBinder(type);
            var propertyInfo = type.GetProperties()[0];

            binder.BindMemberFromValue(
                propertyInfo,
                option);

            var bindingContext = new BindingContext(command.Parse("the-command --fred 42"));

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(42);
        }
    public void Explicit_model_binder_binds_only_to_configured_properties()
    {
        var intOption    = new Option <int>("--int-property");
        var stringOption = new Option <string>("--string-property");
        var parser       = new Parser(new RootCommand {
            intOption, stringOption
        });

        var bindingContext = new InvocationContext(parser.Parse("--int-property 42 --string-property Hello")).BindingContext;
        var binder         = new ModelBinder <ClassWithMultiLetterSetters>
        {
            EnforceExplicitBinding = true
        };

        binder.BindMemberFromValue(obj => obj.IntOption, intOption);
        var instance = binder.CreateInstance(bindingContext) as ClassWithMultiLetterSetters;

        instance.Should().NotBeNull();
        instance.IntOption.Should().Be(42);
        instance.StringOption.Should().BeNull();
    }
        public void PropertyInfo_can_be_bound_to_command()
        {
            var command  = new Command("the-command");
            var argument = new Argument <int>();

            command.Argument = argument;

            var type         = typeof(ClassWithMultiLetterSetters);
            var binder       = new ModelBinder(type);
            var propertyInfo = type.GetProperties().First();

            binder.BindMemberFromValue(
                propertyInfo,
                command);

            var bindingContext = new BindingContext(command.Parse("the-command 42"));

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(42);
        }
示例#6
0
        public void Values_from_options_on_parent_commands_can_be_bound_regardless_of_naming()
        {
            var childCommand  = new Command("child-command");
            var option        = new Option <int>("-x");
            var parentCommand = new Command("parent-command")
            {
                option, childCommand
            };

            var binder = new ModelBinder <ClassWithMultiLetterSetters>();

            binder.BindMemberFromValue(c => c.IntOption, option);

            var bindingContext = new BindingContext(
                parentCommand.Parse("parent-command -x 123 child-command")
                );

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(123);
        }
示例#7
0
        public void PropertyExpression_can_be_bound_to_option()
        {
            var command = new Command("the-command");
            var option  = new Option("--fred")
            {
                Argument = new Argument <int>()
            };

            command.AddOption(option);

            var binder = new ModelBinder <ClassWithMultiLetterSetters>();

            binder.BindMemberFromValue(
                i => i.IntOption,
                option);

            var bindingContext = new BindingContext(command.Parse("the-command --fred 42"));

            var instance = (ClassWithMultiLetterSetters)binder.CreateInstance(bindingContext);

            instance.IntOption.Should().Be(42);
        }