コード例 #1
0
        public void Should_Shadow_Options_Declared_In_Parent_Command_If_Settings_Are_Of_Same_Type()
        {
            // Given
            var configurator = new Configurator(null);

            configurator.AddBranch <MammalSettings>("mammal", mammal =>
            {
                mammal.AddCommand <HorseCommand>("horse");
            });

            // When
            var model = CommandModelBuilder.Build(configurator);

            // Then
            model.Commands[0].As(mammal =>
            {
                mammal.GetOption(option => option.LongNames.Contains("name", StringComparer.Ordinal)).As(o1 =>
                {
                    o1.ShouldNotBeNull();
                    o1.IsShadowed.ShouldBe(false);
                });

                mammal.Children[0].As(horse =>
                {
                    horse.GetOption(option => option.LongNames.Contains("name", StringComparer.Ordinal)).As(o2 =>
                    {
                        o2.ShouldNotBeNull();
                        o2.IsShadowed.ShouldBe(true);
                    });
                });
            });
        }
コード例 #2
0
        public void Should_Set_Parents_Correctly()
        {
            // Given
            var configurator = new Configurator(null);

            configurator.AddBranch <AnimalSettings>("animal", animal =>
            {
                animal.AddBranch <MammalSettings>("mammal", mammal =>
                {
                    mammal.AddCommand <DogCommand>("dog");
                    mammal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            var model = CommandModelBuilder.Build(configurator);

            // Then
            model.Commands.Count.ShouldBe(1);
            model.Commands[0].As(animal =>
            {
                animal.Parent.ShouldBeNull();
                animal.Children.Count.ShouldBe(1);
                animal.Children[0].As(mammal =>
                {
                    mammal.Parent.ShouldBe(animal);
                    mammal.Children.Count.ShouldBe(2);
                    mammal.Children[0].As(dog => dog.Parent.ShouldBe(mammal));
                    mammal.Children[1].As(horse => horse.Parent.ShouldBe(mammal));
                });
            });
        }
コード例 #3
0
        public void Should_Set_Descriptions_For_Branches()
        {
            // Given
            var configurator = new Configurator(null);

            configurator.AddBranch <AnimalSettings>("animal", animal =>
            {
                animal.SetDescription("An animal");
                animal.AddBranch <MammalSettings>("mammal", mammal =>
                {
                    mammal.SetDescription("A mammal");
                    mammal.AddCommand <DogCommand>("dog");
                    mammal.AddCommand <HorseCommand>("horse");
                });
            });

            // When
            var model = CommandModelBuilder.Build(configurator);

            // Then
            model.Commands[0].As(animal =>
            {
                animal.Description.ShouldBe("An animal");
                animal.Children[0].As(mammal => mammal.Description.ShouldBe("A mammal"));
            });
        }
コード例 #4
0
        public void Should_Make_Shadowed_Options_Non_Required()
        {
            // Given
            var configurator = new Configurator(null);

            configurator.AddBranch <MammalSettings>("mammal", mammal =>
            {
                mammal.AddCommand <HorseCommand>("horse");
            });

            // When
            var model = CommandModelBuilder.Build(configurator);

            // Then
            model.Commands[0].As(mammal =>
            {
                mammal.Children[0].As(horse =>
                {
                    horse.GetOption(option => option.LongNames.Contains("name", StringComparer.Ordinal)).As(o2 =>
                    {
                        o2.ShouldNotBeNull();
                        o2.Required.ShouldBe(false);
                    });
                });
            });
        }
コード例 #5
0
    /// <summary>
    /// Runs the command line application with specified arguments.
    /// </summary>
    /// <param name="args">The arguments.</param>
    /// <returns>The exit code from the executed command.</returns>
    public async Task <int> RunAsync(IEnumerable <string> args)
    {
        try
        {
            if (!_executed)
            {
                // Add built-in (hidden) commands.
                _configurator.AddBranch(CliConstants.Commands.Branch, cli =>
                {
                    cli.HideBranch();
                    cli.AddCommand <VersionCommand>(CliConstants.Commands.Version);
                    cli.AddCommand <XmlDocCommand>(CliConstants.Commands.XmlDoc);
                    cli.AddCommand <ExplainCommand>(CliConstants.Commands.Explain);
                });

                _executed = true;
            }

            return(await _executor
                   .Execute(_configurator, args)
                   .ConfigureAwait(false));
        }
        catch (Exception ex)
        {
            // Should we always propagate when debugging?
            if (Debugger.IsAttached &&
                ex is CommandAppException appException &&
                appException.AlwaysPropagateWhenDebugging)
            {
                throw;
            }

            if (_configurator.Settings.PropagateExceptions)
            {
                throw;
            }

            if (_configurator.Settings.ExceptionHandler != null)
            {
                return(_configurator.Settings.ExceptionHandler(ex));
            }

            // Render the exception.
            var pretty = GetRenderableErrorMessage(ex);
            if (pretty != null)
            {
                _configurator.Settings.Console.SafeRender(pretty);
            }

            return(-1);
        }
    }
コード例 #6
0
        public void Should_Throw_If_Branch_Has_No_Children()
        {
            // Given
            var configurator = new Configurator(null);

            configurator.AddBranch <AnimalSettings>("animal", _ =>
            {
            });

            // When
            var result = Record.Exception(() => CommandModelBuilder.Build(configurator));

            // Then
            result.ShouldBeOfType <ConfigurationException>().And(exception =>
                                                                 exception.Message.ShouldBe("The branch 'animal' does not define any commands."));
        }
コード例 #7
0
            public void Should_Return_Correct_Text_With_Suggestion_And_No_Arguments_When_Command_Is_Unknown_And_Distance_Is_Small(string expected)
            {
                // Given
                var configurator = new Configurator(new FakeTypeRegistrar());

                configurator.AddBranch <CommandSettings>("dog", a =>
                {
                    a.AddCommand <CatCommand>("cat");
                });

                // When
                var result = Fixture.GetParseMessage(new[] { "dog", "bat" }, configurator);

                // Then
                result.ShouldBe(expected);
            }
コード例 #8
0
            public void Should_Return_Correct_Text_With_Suggestion_When_Command_After_Argument_Is_Unknown_And_Distance_Is_Small(string expected)
            {
                // Given
                var configurator = new Configurator(new FakeTypeRegistrar());

                configurator.AddBranch <FooCommandSettings>("foo", a =>
                {
                    a.AddCommand <GenericCommand <BarCommandSettings> >("bar");
                });

                // When
                var result = Fixture.GetParseMessage(new[] { "foo", "qux", "bat" }, configurator);

                // Then
                result.ShouldBe(expected);
            }
コード例 #9
0
        public void Should_Output_Leaf_Correctly(string expected)
        {
            // Given
            var configurator = new Configurator(new FakeTypeRegistrar());

            configurator.SetApplicationName("myapp");
            configurator.AddBranch <CatSettings>("cat", animal =>
            {
                animal.SetDescription("Contains settings for a cat.");
                animal.AddCommand <LionCommand>("lion");
            });

            // When
            var result = Fixture.Write(configurator, model => model.Commands[0].Children[0]);

            // Then
            result.ShouldBe(expected);
        }
コード例 #10
0
        public void Should_Create_Configured_Commands()
        {
            // Given, When
            var configurator = new Configurator(null);

            configurator.AddBranch <AnimalSettings>("animal", animal =>
            {
                animal.AddBranch <MammalSettings>("mammal", mammal =>
                {
                    mammal.AddCommand <DogCommand>("dog");
                    mammal.AddCommand <HorseCommand>("horse");
                });
            });

            // Then
            configurator.Commands.ShouldNotBeNull();
            configurator.Commands.Count.ShouldBe(1);
            configurator.Commands[0].As(animal =>
            {
                animal.ShouldBeBranch <AnimalSettings>();
                animal.Children.Count.ShouldBe(1);

                animal.Children[0].As(mammal =>
                {
                    mammal.ShouldBeBranch <MammalSettings>();
                    mammal.Children.Count.ShouldBe(2);

                    mammal.Children[0].As(dog =>
                    {
                        dog.ShouldBeCommand <DogCommand, DogSettings>();
                        dog.Children.Count.ShouldBe(0);
                    });

                    mammal.Children[1].As(horse =>
                    {
                        horse.ShouldBeCommand <HorseCommand, MammalSettings>();
                        horse.Children.Count.ShouldBe(0);
                    });
                });
            });
        }
コード例 #11
0
            public void Should_Output_Root_Examples_Defined_On_Leaves_If_No_Other_Examples_Are_Found(string expected)
            {
                // Given
                var configurator = new Configurator(new FakeTypeRegistrar());

                configurator.SetApplicationName("myapp");
                configurator.AddBranch <AnimalSettings>("animal", animal =>
                {
                    animal.SetDescription("The animal command.");

                    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 result = Fixture.Write(configurator);

                // Then
                result.ShouldBe(expected);
            }