Example #1
0
            public void Should_Create_Unset_Instance_With_Null_For_Nullable_Value_Type_If_Flag_Was_Not_Set()
            {
                // Given
                var app = new CommandAppFixture();

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

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "foo",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <FlagSettingsWithNullableValueType>().And(flag =>
                {
                    flag.Serve.IsSet.ShouldBeFalse();
                    flag.Serve.Value.ShouldBeNull();
                });
            }
Example #2
0
            public void Should_Set_Flag_And_Value_If_Both_Were_Provided()
            {
                // Given
                var app = new CommandAppFixture();

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

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "foo", "--serve", "123",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <FlagSettings>().And(flag =>
                {
                    flag.Serve.IsSet.ShouldBeTrue();
                    flag.Serve.Value.ShouldBe(123);
                });
            }
            public void Should_Only_Output_Command_Examples_Defined_On_Command(string expected)
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                    configurator.AddBranch <AnimalSettings>("animal", animal =>
                    {
                        animal.SetDescription("The animal command.");
                        animal.AddExample(new[] { "animal", "--help" });

                        animal.AddCommand <DogCommand>("dog")
                        .WithExample(new[] { "animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
                        animal.AddCommand <HorseCommand>("horse")
                        .WithExample(new[] { "animal", "horse", "--name", "Brutus" });
                    });
                });

                // When
                var(_, output, _, _) = fixture.Run("animal", "--help");

                // Then
                output.ShouldBe(expected);
            }
Example #4
0
            public void Should_Set_Value_To_Default_Value_If_None_Was_Explicitly_Set()
            {
                // Given
                var app = new CommandAppFixture();

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

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

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <FlagSettingsWithDefaultValue>().And(flag =>
                {
                    flag.Serve.IsSet.ShouldBeTrue();
                    flag.Serve.Value.ShouldBe(987);
                });
            }
        public void Should_Treat_Short_Options_As_Case_Sensitive()
        {
            // Given
            var app = new CommandAppFixture();

            app.Configure(config =>
            {
                config.UseStrictParsing();
                config.PropagateExceptions();
                config.AddCommand <GenericCommand <StringOptionSettings> >("command");
            });

            // When
            var result = Record.Exception(() => app.Run(new[]
            {
                "command", "-F", "bar",
            }));

            // Then
            result.ShouldNotBeNull();
            result.ShouldBeOfType <CommandParseException>().And(ex =>
            {
                ex.Message.ShouldBe("Unknown option 'F'.");
            });
        }
        public void Should_Suppress_Case_Sensitivity_If_Specified()
        {
            // Given
            var app = new CommandAppFixture();

            app.Configure(config =>
            {
                config.UseStrictParsing();
                config.PropagateExceptions();
                config.CaseSensitivity(CaseSensitivity.None);
                config.AddCommand <GenericCommand <StringOptionSettings> >("command");
            });

            // When
            var(result, _, _, settings) = app.Run(new[]
            {
                "Command", "--Foo", "bar",
            });

            // Then
            result.ShouldBe(0);
            settings.ShouldBeOfType <StringOptionSettings>().And(vec =>
            {
                vec.Foo.ShouldBe("bar");
            });
        }
            public void Should_Pass_Case_2()
            {
                // Given
                var app = new CommandAppFixture();

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

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

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <DogSettings>().And(dog =>
                {
                    dog.Legs.ShouldBe(12);
                    dog.Age.ShouldBe(4);
                    dog.GoodBoy.ShouldBe(true);
                    dog.Name.ShouldBe("Rufus");
                    dog.IsAlive.ShouldBe(true);
                });
            }
Example #8
0
            public void Should_Map_ReadOnly_Dictionary_Values()
            {
                // Given
                var app = new CommandAppFixture();

                app.WithDefaultCommand <GenericCommand <ReadOnlyDictionarySettings> >();
                app.Configure(config =>
                {
                    config.PropagateExceptions();
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "--var", "foo=bar",
                    "--var", "baz=qux",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <ReadOnlyDictionarySettings>().And(pair =>
                {
                    pair.Values.ShouldNotBeNull();
                    pair.Values.Count.ShouldBe(2);
                    pair.Values["foo"].ShouldBe("bar");
                    pair.Values["baz"].ShouldBe("qux");
                });
            }
Example #9
0
            public void Should_Map_Latest_Value_Of_Same_Key_When_Mapping_To_Dictionary()
            {
                // Given
                var app = new CommandAppFixture();

                app.WithDefaultCommand <GenericCommand <DictionarySettings> >();
                app.Configure(config =>
                {
                    config.PropagateExceptions();
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "--var", "foo=bar",
                    "--var", "foo=qux",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <DictionarySettings>().And(pair =>
                {
                    pair.Values.ShouldNotBeNull();
                    pair.Values.Count.ShouldBe(1);
                    pair.Values["foo"].ShouldBe("qux");
                });
            }
            public void Should_Bind_Using_Custom_Type_Converter_If_Specified()
            {
                // Given
                var app = new CommandAppFixture();

                app.Configure(config =>
                {
                    config.PropagateExceptions();
                    config.AddCommand <CatCommand>("cat");
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "cat", "--name", "Tiger",
                    "--agility", "FOOBAR",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <CatSettings>().And(cat =>
                {
                    cat.Agility.ShouldBe(6);
                });
            }
        public void Should_Pass_Case_6()
        {
            // Given
            var app = new CommandAppFixture();

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

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

            // Then
            result.ShouldBe(0);
            settings.ShouldBeOfType <ArgumentVectorSettings>().And(vec =>
            {
                vec.Foo.Length.ShouldBe(3);
                vec.Foo.ShouldBe(new[] { "a", "b", "c" });
            });
        }
Example #12
0
            public void Should_Map_Pairs_To_Pair_Deconstructable_Collection_Using_Default_Deconstructort()
            {
                // Given
                var app = new CommandAppFixture();

                app.WithDefaultCommand <GenericCommand <DefaultPairDeconstructorSettings> >();
                app.Configure(config =>
                {
                    config.PropagateExceptions();
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "--var", "foo=1",
                    "--var", "foo=3",
                    "--var", "bar=4",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <DefaultPairDeconstructorSettings>().And(pair =>
                {
                    pair.Values.ShouldNotBeNull();
                    pair.Values.Count.ShouldBe(2);
                    pair.Values["foo"].ShouldBe(3);
                    pair.Values["bar"].ShouldBe(4);
                });
            }
            public void Should_Register_Remaining_Raw_Arguments_With_Context()
            {
                // Given
                var app = new CommandAppFixture();

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

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

                // Then
                ctx.Remaining.Raw.Count.ShouldBe(5);
                ctx.Remaining.Raw[0].ShouldBe("--foo");
                ctx.Remaining.Raw[1].ShouldBe("bar");
                ctx.Remaining.Raw[2].ShouldBe("-bar");
                ctx.Remaining.Raw[3].ShouldBe("baz");
                ctx.Remaining.Raw[4].ShouldBe("qux");
            }
        public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_Mode()
        {
            // Given
            var app = new CommandAppFixture();

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

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

            // Then
            ctx.ShouldNotBeNull();
            ctx.Remaining.Parsed.Count.ShouldBe(1);
            ctx.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
        }
            public void Should_Register_Remaining_Parsed_Arguments_With_Context()
            {
                // Given
                var app = new CommandAppFixture();

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

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

                // Then
                ctx.Remaining.Parsed.Count.ShouldBe(4);
                ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
                ctx.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
                ctx.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
                ctx.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
            }
        public void Should_Be_Able_To_Use_Command_Alias()
        {
            // Given
            var app = new CommandAppFixture();

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

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

            // Then
            result.ShouldBe(0);
            settings.ShouldBeOfType <OptionVectorSettings>().And(vec =>
            {
                vec.Foo.Length.ShouldBe(1);
                vec.Foo.ShouldBe(new[] { "a" });
            });
        }
Example #17
0
            public void Should_Map_Lookup_Values()
            {
                // Given
                var app = new CommandAppFixture();

                app.WithDefaultCommand <GenericCommand <LookupSettings> >();
                app.Configure(config =>
                {
                    config.PropagateExceptions();
                });

                // When
                var(result, _, _, settings) = app.Run(new[]
                {
                    "--var", "foo=bar",
                    "--var", "foo=qux",
                });

                // Then
                result.ShouldBe(0);
                settings.ShouldBeOfType <LookupSettings>().And(pair =>
                {
                    pair.Values.ShouldNotBeNull();
                    pair.Values.Count.ShouldBe(1);
                    pair.Values["foo"].ToList().Count.ShouldBe(2);
                });
            }
        public void Should_Pass_Case_5()
        {
            // Given
            var app = new CommandAppFixture();

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

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

            // Then
            result.ShouldBe(0);
            settings.ShouldBeOfType <OptionVectorSettings>().And(vec =>
            {
                vec.Foo.Length.ShouldBe(3);
                vec.Foo.ShouldBe(new[] { "a", "b", "c" });
                vec.Bar.Length.ShouldBe(2);
                vec.Bar.ShouldBe(new[] { 1, 2 });
            });
        }
        public void Should_Pass_Case_3()
        {
            // Given
            var app = new CommandAppFixture();

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

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

            // Then
            result.ShouldBe(0);
            settings.ShouldBeOfType <DogSettings>().And(dog =>
            {
                dog.Age.ShouldBe(12);
                dog.GoodBoy.ShouldBe(true);
                dog.Name.ShouldBe("Rufus");
                dog.IsAlive.ShouldBe(false);
            });
        }
            public void Should_Not_Propagate_Exceptions_If_Not_Explicitly_Told_To_Do_So()
            {
                // Given
                var app = new CommandAppFixture();

                app.Configure(config =>
                {
                    config.AddCommand <ThrowingCommand>("throw");
                });

                // When
                var(result, _, _, _) = app.Run(new[] { "throw" });

                // Then
                result.ShouldBe(-1);
            }
            public Task Should_Dump_Correct_Model_For_Case_2()
            {
                // Given
                var fixture = new CommandAppFixture();

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

                // When
                var(_, output, _, _) = fixture.Run(Constants.XmlDocCommand);

                // Then
                return(Verifier.Verify(output));
            }
            public Task Should_Dump_Correct_Model_For_Model_With_Default_Command()
            {
                // Given
                var fixture = new CommandAppFixture().WithDefaultCommand <DogCommand>();

                fixture.Configure(config =>
                {
                    config.AddCommand <HorseCommand>("horse");
                });

                // When
                var(_, output, _, _) = fixture.Run(Constants.XmlDocCommand);

                // Then
                return(Verifier.Verify(output));
            }
            public void Should_Dump_Correct_Model_For_Model_With_Default_Command(string expected)
            {
                // Given
                var fixture = new CommandAppFixture().WithDefaultCommand <DogCommand>();

                fixture.Configure(config =>
                {
                    config.AddCommand <HorseCommand>("horse");
                });

                // When
                var(_, output, _, _) = fixture.Run(Constants.XmlDocCommand);

                // Then
                output.ShouldBe(expected);
            }
            public void Should_Dump_Correct_Model_For_Case_2(string expected)
            {
                // Given
                var fixture = new CommandAppFixture();

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

                // When
                var(_, output, _, _) = fixture.Run(Constants.XmlDocCommand);

                // Then
                output.ShouldBe(expected);
            }
Example #25
0
            public Task Should_List_Arguments_In_Correct_Order()
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.WithDefaultCommand <GenericCommand <ArgumentOrderSettings> >();
                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                });

                // When
                var(_, output, _, _) = fixture.Run("--help");

                // Then
                return(Verifier.Verify(output));
            }
Example #26
0
            public Task Should_Not_Show_Truncated_Command_Table_If_Commands_Are_Missing_Description()
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                    configurator.AddCommand <NoDescriptionCommand>("bar");
                });

                // When
                var(_, output, _, _) = fixture.Run("--help");

                // Then
                return(Verifier.Verify(output));
            }
Example #27
0
            public Task Should_Output_Default_Command_Correctly()
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.WithDefaultCommand <LionCommand>();
                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                });

                // When
                var(_, output, _, _) = fixture.Run("--help");

                // Then
                return(Verifier.Verify(output));
            }
            public void Should_Output_Default_Command_Correctly(string expected)
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.WithDefaultCommand <LionCommand>();
                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                });

                // When
                var(_, output, _, _) = fixture.Run("--help");

                // Then
                output.ShouldBe(expected);
            }
            public void Should_Throw_If_Value_Is_Not_In_A_Valid_Format_Using_Default_Deconstructor(
                string input, string expected)
            {
                // Given
                var app = new CommandAppFixture();

                app.WithDefaultCommand <GenericCommand <DefaultPairDeconstructorSettings> >();

                // When
                var(result, output, _, settings) = app.Run(new[]
                {
                    "--var", input,
                });

                // Then
                result.ShouldBe(-1);
                output.ShouldBe(expected);
            }
Example #30
0
            public Task Should_Output_Root_Examples_If_Default_Command_Is_Specified()
            {
                // Given
                var fixture = new CommandAppFixture();

                fixture.WithDefaultCommand <LionCommand>();
                fixture.Configure(configurator =>
                {
                    configurator.SetApplicationName("myapp");
                    configurator.AddExample(new[] { "12", "-c", "3" });
                });

                // When
                var(_, output, _, _) = fixture.Run("--help");

                // Then
                return(Verifier.Verify(output));
            }