コード例 #1
0
        internal void Execute_OnError_Result_Should_Have_ErrorMessage()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();

            // Act
            mapper.Execute(new[] { "--option" }, null, errorResult => {
                // Assert
                Assert.Equal("Unknown option '--option'.", errorResult.ErrorMessage);
            });
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: akanmuratcimen/args-mapper
        private static void Main(string[] args)
        {
            var mapper = new ArgsMapper <Args>();

            mapper.AddCommand(x => x.AddUser, commandSettings => {
                commandSettings.AddOption(x => x.Username);
                commandSettings.AddOption(x => x.Password);
            });

            mapper.Execute(args, OnSuccess, OnError);
        }
コード例 #3
0
        internal void Execute_Output_Should_Be_Empty_When_OnSuccess_Method_Null()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            // Act
            mapper.Execute(Array.Empty <string>(), null);

            Assert.Equal(string.Empty, output.ToString().TrimEnd());
        }
コード例 #4
0
        internal void Execute_Output_Should_Be_Empty_When_OnError_Not_Null()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            // Act
            mapper.Execute(new[] { "--option" }, null, errorResult => {
            });

            Assert.Equal(string.Empty, output.ToString());
        }
コード例 #5
0
        internal void Execute_Should_Write_Error_Text_When_OnError_Null()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            // Act
            mapper.Execute(new[] { "--option" }, null);

            // Assert
            Assert.Equal($"Unknown option '--option'.{Environment.NewLine}", output.ToString());
        }
コード例 #6
0
        internal void Output_Should_Be_1_0_0_0_Version_Text(params string[] args)
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            // Act
            mapper.Execute(args, null);

            // Assert
            Assert.Equal(new Version(1, 0, 0, 0).ToString(), output.ToString());
        }
コード例 #7
0
        internal void Mapper_Output_Should_Be_Empty_When_Introduction_And_Usage_Not_Defined()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddOption(x => x.Option);

            // Act
            mapper.Execute(Array.Empty <string>(), null);

            // Assert
            Assert.Equal(string.Empty, output.ToString());
        }
コード例 #8
0
        internal void Mapper_Output_Should_Unknown_Option_Error(params string[] args)
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddOption(x => x.Option);

            // Act
            mapper.Execute(args, null);

            // Assert
            Assert.StartsWith("Unknown option", output.ToString());
        }
コード例 #9
0
        internal void Mapper_Output_Should_Unknown_Option_Error()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command);

            // Act
            mapper.Execute(new[] { "command", "--help" }, null);

            // Assert
            Assert.StartsWith("Unknown 'command' command option", output.ToString());
        }
コード例 #10
0
        internal void Mapper_Output_Should_Be_Command_Usage_Content()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.Usage.AddText("sample command usage text.");
            });

            // Act
            mapper.Execute(new[] { "command", "--help" }, null);

            // Assert
            Assert.Equal("sample command usage text.", output.ToString().TrimEnd());
        }
コード例 #11
0
        internal void Execute_Output_Should_Be_Unknown_Command_Message()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneClassWithOneBoolOption>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.AddSubcommand(x => x.Command);
            });

            // Act
            mapper.Execute(new[] { "command", "unknown-command" }, null);

            // Assert
            Assert.StartsWith("'unknown-command' is not a valid command.", output.ToString());
        }
コード例 #12
0
        internal void Mapper_Output_Should_Be_Usage_Content(params string[] args)
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddOption(x => x.Option);

            mapper.Usage.AddText("sample usage text.");

            // Act
            mapper.Execute(args, null);

            // Assert
            Assert.Equal("sample usage text.", output.ToString().TrimEnd());
        }
コード例 #13
0
        internal void Mapper_Output_Should_Be_Version_Option()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.Usage.AddVersionOption(contentOptionSettings => {
                contentOptionSettings.Description = "sample version description.";
            });

            // Act
            mapper.Execute(new[] { "--help" }, null);

            // Assert
            Assert.Equal("-v|--version  sample version description.", output.ToString().TrimEnd());
        }
コード例 #14
0
        internal void Mapper_Output_Should_Be_Usage_Content_When_Introduction_Not_Defined()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddOption(x => x.Option);

            mapper.Usage.AddText("sample usage text.");

            // Act
            mapper.Execute(Array.Empty <string>(), null);

            // Assert
            Assert.Equal("sample usage text.", output.ToString().TrimEnd());
        }
コード例 #15
0
        internal void Mapper_Output_Should_Be_Help_Option()
        {
            // Arrange
            var mapper = new ArgsMapper <OneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.Introduction.AddHelpOption(contentOptionSettings => {
                contentOptionSettings.Description = "sample help description.";
            });

            // Act
            mapper.Execute(Array.Empty <string>(), null);

            // Assert
            Assert.Equal("-h|--help  sample help description.", output.ToString().TrimEnd());
        }
コード例 #16
0
        internal void Execute_Success_Model_Option_Should_Be_True()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneClassWithOneBoolOption>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.AddSubcommand(x => x.Command, subcommandSettings => {
                    subcommandSettings.AddPositionalOption(x => x.Option);
                });
            });

            // Act
            mapper.Execute(new[] { "command", "command", "on" }, args => {
                // Assert
                Assert.True(args.Command.Command.Option);
            });
        }
コード例 #17
0
        internal void Mapper_Output_Should_Be_Help_Option()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.Usage.AddHelpOption(contentOptionSettings => {
                    contentOptionSettings.Description = "sample help description.";
                });
            });

            // Act
            mapper.Execute(new[] { "command", "--help" }, null);

            // Assert
            Assert.Equal("-h|--help  sample help description.", output.ToString().TrimEnd());
        }
コード例 #18
0
        internal void Mapper_Output_Should_Be_Command_Usage_Content_When_ShowUsageWhenEmptyOptions_True_And_No_Option_In_Args()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneBoolOptionArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.ShowUsageWhenEmptyOptions = true;

                commandSettings.Usage.AddText("sample usage text");
            });

            // Act
            mapper.Execute(new[] { "command" }, null);

            // Assert
            Assert.Equal("sample usage text", output.ToString().Trim());
        }
コード例 #19
0
        internal void Mapper_Output_Should_Be_Unknown_Option_Error_When_Version_Alias_In_Stacked_Option()
        {
            // Arrange
            var mapper = new ArgsMapper <ThreeBoolOptionsArgs>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddOption(x => x.Option1, 'x', "option-1");
            mapper.AddOption(x => x.Option2, 'y', "option-2");
            mapper.AddOption(x => x.Option3, 'z', "option-3");

            mapper.Usage.AddText("sample usage text.");

            // Act
            mapper.Execute(new[] { "-vxyz" }, null);

            // Assert
            Assert.Equal("Unknown option '-v'.", output.ToString().TrimEnd());
        }
コード例 #20
0
        internal void Mapper_Subcommand_Output_Should_Be_Empty()
        {
            // Arrange
            var mapper = new ArgsMapper <OneCommandWithOneClassWithOneBoolOption>();
            var output = new StringBuilder();

            mapper.Settings.DefaultWriter = new StringWriter(output);

            mapper.AddCommand(x => x.Command, commandSettings => {
                commandSettings.AddSubcommand(x => x.Command, subcommandSettings => {
                    subcommandSettings.AddPositionalOption(x => x.Option);

                    subcommandSettings.Usage.AddText("sample usage text");
                });
            });

            // Act
            mapper.Execute(new[] { "command", "command" }, null);

            // Assert
            Assert.Equal(string.Empty, output.ToString());
        }
コード例 #21
0
        private static void Main(string[] args)
        {
            var mapper = new ArgsMapper <Args>();

            mapper.AddCommand(x => x.Sum, commandSettings => {
                commandSettings.AddPositionalOption(x => x.Values);
            });

            mapper.AddCommand(x => x.Multiply, commandSettings => {
                commandSettings.AddPositionalOption(x => x.Values, optionSettings => {
                    optionSettings.IsRequired = true;
                });
            });

            mapper.AddCommand(x => x.Divide, commandSettings => {
                commandSettings.AddPositionalOption(x => x.X);
                commandSettings.AddPositionalOption(x => x.Y);
            });

            mapper.AddCommand(x => x.Subtract, commandSettings => {
                commandSettings.AddPositionalOption(x => x.X);
                commandSettings.AddPositionalOption(x => x.Y);
            });

            mapper.Usage.AddText("usage: [sum] <x>...");
            mapper.Usage.AddText("usage: [multiply] <x>...");
            mapper.Usage.AddText("usage: [divide] <x> <y>");
            mapper.Usage.AddText("usage: [subtract] <x> <y>");

            mapper.Usage.AddEmptyLine();

            mapper.Usage.AddSection("commands:", sectionSettings => {
                const int nameColumnWidth = 9;

                sectionSettings.AddCommand(x => x.Sum, commandSettings => {
                    commandSettings.NameColumnWidth = nameColumnWidth;
                    commandSettings.Description     = "sums the given values.";
                });

                sectionSettings.AddCommand(x => x.Multiply, commandSettings => {
                    commandSettings.NameColumnWidth = nameColumnWidth;
                    commandSettings.Description     = "multiplies the given values.";
                });

                sectionSettings.AddCommand(x => x.Divide, commandSettings => {
                    commandSettings.NameColumnWidth = nameColumnWidth;
                    commandSettings.Description     = "divides the given two values.";
                });

                sectionSettings.AddCommand(x => x.Subtract, commandSettings => {
                    commandSettings.NameColumnWidth = nameColumnWidth;
                    commandSettings.Description     = "subtracts the given two values.";
                });
            });

            mapper.Usage.AddHelpOption(optionSettings => {
                optionSettings.NameColumnWidth = 11;
                optionSettings.Description     = "display help.";
            });

            mapper.Execute(args, OnSuccess);
        }