示例#1
0
            public void Should_Register_Remaining_Parsed_Arguments_With_Context()
            {
                // Given
                var capturedContext = default(CommandContext);

                var resolver = new FakeTypeResolver();
                var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

                resolver.Register(new DogSettings());
                resolver.Register(command);

                var app = new CommandApp(new FakeTypeRegistrar(resolver));

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddBranch <AnimalSettings>("animal", animal =>
                    {
                        animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                    });
                });

                // When
                app.Run(new[] { "animal", "4", "dog", "12", "--", "--foo", "bar", "--foo", "baz", "-bar", "\"baz\"", "qux" });

                // Then
                capturedContext.Remaining.Parsed.Count.ShouldBe(4);
                capturedContext.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
                capturedContext.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
                capturedContext.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
                capturedContext.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
            }
示例#2
0
            public void Should_Register_Remaining_Raw_Arguments_With_Context()
            {
                // Given
                var capturedContext = default(CommandContext);

                var resolver = new FakeTypeResolver();
                var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

                resolver.Register(new DogSettings());
                resolver.Register(command);

                var app = new CommandApp(new FakeTypeRegistrar(resolver));

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddBranch <AnimalSettings>("animal", animal =>
                    {
                        animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                    });
                });

                // When
                app.Run(new[] { "animal", "4", "dog", "12", "--", "--foo", "bar", "-bar", "\"baz\"", "qux" });

                // Then
                capturedContext.Remaining.Raw.Count.ShouldBe(5);
                capturedContext.Remaining.Raw[0].ShouldBe("--foo");
                capturedContext.Remaining.Raw[1].ShouldBe("bar");
                capturedContext.Remaining.Raw[2].ShouldBe("-bar");
                capturedContext.Remaining.Raw[3].ShouldBe("\"baz\"");
                capturedContext.Remaining.Raw[4].ShouldBe("qux");
            }
示例#3
0
        public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
        {
            // Given
            var capturedContext = default(CommandContext);

            var resolver = new FakeTypeResolver();
            var command  = new InterceptingCommand <DogSettings>((context, _) => { capturedContext = context; });

            resolver.Register(new DogSettings());
            resolver.Register(command);

            var registrar = new FakeTypeRegistrar(resolver);
            var app       = new CommandApp(registrar);

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <InterceptingCommand <DogSettings> >("dog");
                });
            });

            // When
            var result = app.Run(new[] { "animal", "4", "dog", "12", "--foo" });

            // Then
            capturedContext.ShouldNotBeNull();
            capturedContext.Remaining.Parsed.Count.ShouldBe(1);
            capturedContext.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
        }
示例#4
0
        public void Should_Pass_Case_3()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new DogSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.AddCommand <DogCommand>("dog");
                    animal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            var result = app.Run(new[] { "animal", "dog", "12", "--good-boy", "--name", "Rufus" });

            // Then
            result.ShouldBe(0);
            settings.Age.ShouldBe(12);
            settings.GoodBoy.ShouldBe(true);
            settings.IsAlive.ShouldBe(false);
            settings.Name.ShouldBe("Rufus");
        }
示例#5
0
        public void Should_Pass_Case_2()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new DogSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <DogCommand>("dog");
            });

            // When
            var result = app.Run(new[] { "dog", "12", "4", "--good-boy", "--name", "Rufus", "--alive" });

            // Then
            result.ShouldBe(0);
            settings.Legs.ShouldBe(12);
            settings.Age.ShouldBe(4);
            settings.GoodBoy.ShouldBe(true);
            settings.IsAlive.ShouldBe(true);
            settings.Name.ShouldBe("Rufus");
        }
        public void Should_Pass_Case_5()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new OptionVectorSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <OptionVectorCommand>("multi");
            });

            // When
            var result = app.Run(new[] { "multi", "--foo", "a", "--foo", "b", "--bar", "1", "--foo", "c", "--bar", "2" });

            // Then
            result.ShouldBe(0);
            settings.Foo.Length.ShouldBe(3);
            settings.Foo.ShouldBe(new[] { "a", "b", "c" });
            settings.Bar.Length.ShouldBe(2);
            settings.Bar.ShouldBe(new[] { 1, 2 });
        }
        public void Should_Be_Able_To_Set_The_Default_Command()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new DogSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.SetDefaultCommand <DogCommand>();

            // When
            var result = app.Run(new[] { "4", "12", "--good-boy", "--name", "Rufus" });

            // Then
            result.ShouldBe(0);
            settings.Legs.ShouldBe(4);
            settings.Age.ShouldBe(12);
            settings.GoodBoy.ShouldBe(true);
            settings.Name.ShouldBe("Rufus");
        }
        public void Should_Be_Able_To_Use_Command_Alias()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new OptionVectorSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <OptionVectorCommand>("multi").WithAlias("multiple");
            });

            // When
            var result = app.Run(new[] { "multiple", "--foo", "a" });

            // Then
            result.ShouldBe(0);
            settings.Foo.Length.ShouldBe(1);
            settings.Foo.ShouldBe(new[] { "a" });
        }
        public void Should_Pass_Case_6()
        {
            // Given
            var resolver = new FakeTypeResolver();
            var settings = new ArgumentVectorSettings();

            resolver.Register(settings);

            var app = new CommandApp(new FakeTypeRegistrar(resolver));

            app.Configure(config =>
            {
                config.PropagateExceptions();
                config.AddCommand <GenericCommand <ArgumentVectorSettings> >("multi");
            });

            // When
            var result = app.Run(new[] { "multi", "a", "b", "c" });

            // Then
            result.ShouldBe(0);
            settings.Foo.Length.ShouldBe(3);
            settings.Foo.ShouldBe(new[] { "a", "b", "c" });
        }