예제 #1
0
        public void WriteGlobalCommands()
        {
            // Arrange
            var application = new KingpinApplication(console);

            application.Command("cmd1", "command1 help");
            application.Command("cmd2", "command2 help");
            // Act
            var subject = new HelpGenerator(application, console);
            var appText = subject.ReadResourceInExecutingAssembly("KingpinNet.Help.ApplicationHelp.liquid");

            subject.Generate(writer, appText);
            // Assert
            var result = writer.ToString();

            output.WriteLine(result);
            Assert.Contains($"usage:  <command>", result);
            Assert.Contains($"Commands:", result);
            Assert.Contains($"  cmd1", result);
            Assert.Contains($"    command1 help", result);
            Assert.Contains($"  cmd2", result);
            Assert.Contains($"    command2 help", result);
        }
예제 #2
0
        public void ParseSimpleCommand()
        {
            // Arrange
            string[] args        = new[] { "run" };
            var      application = new KingpinApplication(consoleMock.Object);

            application.Command("run", "This is a command");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.Equal("run", result.Result["Command"]);
        }
예제 #3
0
        public void ParseHelpFlagOnCommand()
        {
            // Arrange
            string[] args        = new[] { "run", "--help" };
            var      application = new KingpinApplication(console);

            application.Initialize();
            application.Command("run", "This is a command");

            // Act
            var result = application.Parse(args);

            // Assert
            Assert.Equal("true", result.Result["run:help"]);
        }
예제 #4
0
        public void ParseNestedCommand()
        {
            // Arrange
            string[] args        = new[] { "cmd1", "cmd2" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("cmd1", "command1 help");

            command.Command("cmd2", "command2 help");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.Equal("cmd1:cmd2", result.Result["Command"]);
        }
예제 #5
0
        public void ParseSimpleCommandWithFlag()
        {
            // Arrange
            string[] args        = new[] { "run", "--myflag=danish" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("run", "This is a command");

            command.Flag("myflag", "This is the flag of the person");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.Equal("run", result.Result["Command"]);
            Assert.Equal("danish", result.Result["run:myflag"]);
        }
예제 #6
0
        public void ParseNestedDefaultFlag()
        {
            // Arrange
            string[] args        = { "cmd1", "cmd2" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("cmd1", "command1 help");
            var      cmd2        = command.Command("cmd2", "command2 help");

            cmd2.Flag("flg", "").Default("1234");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.Equal("1234", result.Result["cmd1:cmd2:flg"]);
        }
예제 #7
0
        public void ParseSimpleCommandWithArgument()
        {
            // Arrange
            string[] args        = new[] { "run", "an_argument" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("run", "This is a command");

            command.Argument("argument", "This is an argument");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.Equal("run", result.Result["Command"]);
            Assert.Equal("an_argument", result.Result["run:argument"]);
        }
예제 #8
0
        public void SuggestCommandSubstring()
        {
            // Arrange
            string[] args        = new[] { "suggest", "cmd1", "cm" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("cmd1", "command1 help");

            command.Command("cmd2", "command2 help");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.True(result.IsSuggestion);
            Assert.Equal("cmd2", result.Suggestions.First());
        }
예제 #9
0
        public void SuggestDoubleMinusShowFlags()
        {
            // Arrange
            string[] args        = new[] { "suggest", "cmd1", "--" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("cmd1");

            command.Command("cmd2");
            command.Flag("myflag");

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.True(result.IsSuggestion);
            Assert.Equal("--myflag", result.Suggestions[0]);
        }
예제 #10
0
        public void SuggestCommandShowShortNamedFlag()
        {
            // Arrange
            string[] args        = new[] { "suggest", "m" };
            var      application = new KingpinApplication(consoleMock.Object);
            var      command     = application.Command("cmd1");

            application.Flag("myflag").Short('m');

            // Act
            var subject = new Parser(application);
            var result  = subject.Parse(args);

            // Assert
            Assert.True(result.IsSuggestion);
            Assert.Equal("cmd1", result.Suggestions[0]);
            Assert.Equal("--myflag", result.Suggestions[1]);
            Assert.Equal("-m", result.Suggestions[2]);
        }
예제 #11
0
        public void WriteGlobalCommandWithFlagAndDefaultValue()
        {
            // Arrange
            var application = new KingpinApplication(console);
            var command     = application.Command("cmd", "command help");

            command.Flag("flag", "flag help").Default("1234.5678");
            // Act
            var subject = new HelpGenerator(application, console);
            var appText = subject.ReadResourceInExecutingAssembly("KingpinNet.Help.ApplicationHelp.liquid");

            subject.Generate(writer, appText);
            // Assert
            var result = writer.ToString();

            output.WriteLine(result);
            Assert.Contains($"usage:  <command>", result);
            Assert.Contains($"Commands:", result);
            Assert.Contains($"  cmd --flag=<1234.5678> flag help      command help", result);
        }
예제 #12
0
        public void WriteGlobalCommandWithFlagAndExamples()
        {
            // Arrange
            var application = new KingpinApplication(console);
            var command     = application.Command("cmd", "command help");

            command.Flag("flag", "flag help").SetExamples("1", "2");
            // Act
            var subject = new HelpGenerator(application, console);
            var appText = subject.ReadResourceInExecutingAssembly("KingpinNet.Help.CommandHelp.liquid");

            subject.Generate(command, writer, appText);
            // Assert
            var result = writer.ToString();

            output.WriteLine(result);
            Assert.Contains($"usage:  cmd [<flags>]", result);
            Assert.Contains($"  command help", result);
            Assert.Contains($"Flags:", result);
            Assert.Contains($"  --flag=<value>                        flag help (e.g. 1, 2)", result);
        }
예제 #13
0
        public void WriteFlagWithValueName()
        {
            // Arrange
            var application = new KingpinApplication(console);

            application.ApplicationName("x");
            var command = application.Command("cmd", "command help");

            command.Flag("flag", "flag help").ValueName("!name!");
            // Act
            var subject = new HelpGenerator(application, console);
            var appText = subject.ReadResourceInExecutingAssembly("KingpinNet.Help.ApplicationHelp.liquid");

            subject.Generate(writer, appText);
            // Assert
            var result = writer.ToString();

            output.WriteLine(result);
            Assert.Contains($"usage: x <command>", result);
            Assert.Contains($"Commands:", result);
            Assert.Contains($"  cmd --flag=!name!", result);
            Assert.Contains($"    command help", result);
        }
예제 #14
0
        public void ShowDetailedError()
        {
            // Arrange
            string[] args = new[] { "--integer=x1x" };

            var application = new KingpinApplication(console);

            application.Initialize();
            application.ShowHelpOnParsingErrors();
            application.Command("integer", "This is an int").IsInt();

            // Act
            try
            {
                application.Parse(args);
            }
            catch { }

            // Assert
            var result = writer.ToString();

            output.WriteLine(result);
            Assert.Contains("Illegal flag", result);
        }