Exemplo n.º 1
0
        void should_parse_flag_successfully_by_abbrevation_when_only_define_abbrevation()
        {
            var parser = new ArgsParserBuilder().BeginDefaultCommand().AddFlagOption(null, 'f', "This is a flag").EndCommand().Build();
            ArgsParsingResult result = parser.Parse(new[] { "-f" });

            Assert.True(result.IsSuccess);
            Assert.Null(result.Error);
            Assert.True(result.GetFlagValue("-f"));
            Assert.False(result.GetFlagValue("--flag"));
        }
Exemplo n.º 2
0
        void should_parse_flag_successfully_by_full_name_when_only_define_full_name()
        {
            var parser = new ArgsParserBuilder().BeginDefaultCommand().AddFlagOption("_Fla26--182-g-", null, "This is a flag").EndCommand().Build();
            ArgsParsingResult result = parser.Parse(new[] { "--_Fla26--182-g-" });

            Assert.True(result.IsSuccess);
            Assert.Null(result.Error);
            Assert.False(result.GetFlagValue("-f"));
            Assert.True(result.GetFlagValue("--_Fla26--182-g-"));
        }
Exemplo n.º 3
0
        void should_parse_flag_successfully_by_full_name()
        {
            var parser = new ArgsParserBuilder().BeginDefaultCommand().AddFlagOption("flag1", 'f', "This is a flag").EndCommand().Build();
            ArgsParsingResult result = parser.Parse(new[] { "--flag1" });

            Assert.True(result.IsSuccess);
            Assert.Null(result.Error);
            Assert.NotNull(result.Command);
            Assert.Null(result.Command.Symbol);
            Assert.True(result.GetFlagValue("--flag1"));
            Assert.True(result.GetFlagValue("--FlAg1"));
            Assert.True(result.GetFlagValue("-f"));
            Assert.True(result.GetFlagValue("-F"));
        }
        public void should_recognize_some_odd_flag_with_number()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFlagOption("1", 'o', string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "command", "--1" });

            result.AssertSuccess();
            Assert.True(result.GetFlagValue("--1"));
            Assert.True(result.GetFlagValue("-o"));
        }
        public void should_parse_success_when_input_combined_flags()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("read", 'r')
                                .AddFlagOption("force", 'f')
                                .AddFlagOption("check", 'c')
                                .EndCommand()
                                .Build();

            ArgsParsingResult argsParsingResult = parser.Parse(new [] { "-rf" });

            Assert.True(argsParsingResult.IsSuccess);
            Assert.True(argsParsingResult.GetFlagValue("--read"));
            Assert.True(argsParsingResult.GetFlagValue("--force"));
            Assert.False(argsParsingResult.GetFlagValue("--check"));
        }
        public void should_get_multiple_flag_for_specified_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFlagOption("flag-a", 'a', string.Empty)
                                .AddFlagOption("flag-b", 'b', string.Empty)
                                .AddFlagOption("flag-c", 'c', string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "command", "-a", "-b" });

            result.AssertSuccess();
            Assert.True(result.GetFlagValue("--flag-a"));
            Assert.True(result.GetFlagValue("--flag-b"));
            Assert.False(result.GetFlagValue("--flag-c"));
        }
        public void should_get_mixed_options_for_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("key-a", 'a', string.Empty)
                                .AddFlagOption("flag-b", 'b', string.Empty)
                                .AddFlagOption("flag-c", 'c', string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new[] { "--key-a", "value", "-b" });

            result.AssertSuccess();
            Assert.Equal("value", result.GetOptionRawValue("-a").Single());
            Assert.True(result.GetFlagValue("--flag-b"));
            Assert.False(result.GetFlagValue("--flag-c"));
        }
        public void should_get_flag_for_non_specified_flags_with_default_command()
        {
            // start-(flag-option)->(EoA)->other flag option should be false.

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f', string.Empty)
                                .AddFlagOption("other-flag", 'o', string.Empty)
                                .EndCommand()
                                .Build();

            string[]          args   = { "-f" };
            ArgsParsingResult result = parser.Parse(args);

            result.AssertSuccess();
            Assert.True(result.GetFlagValue("-f"));
            Assert.False(result.GetFlagValue("-o"));
        }
Exemplo n.º 9
0
        void should_can_add_multiple_flags(string arg)
        {
            var parser = new ArgsParserBuilder().BeginDefaultCommand().AddFlagOption("flag", 'f').AddFlagOption("version", 'v').EndCommand().Build();
            ArgsParsingResult result = parser.Parse(new[] { arg });

            Assert.True(result.IsSuccess);
            Assert.Null(result.Error);
            Assert.True(result.GetFlagValue(arg));
        }
        public void should_get_flag_using_full_form_but_parsed_as_abbr_form_with_default_command()
        {
            // start-(abbr-flag-options)-(EoA)->ok

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("recursive", 'r', string.Empty)
                                .AddFlagOption("force", 'f', string.Empty)
                                .EndCommand()
                                .Build();

            string[]          args   = { "-rf" };
            ArgsParsingResult result = parser.Parse(args);

            result.AssertSuccess();
            Assert.True(result.GetFlagValue("--recursive"));
            Assert.True(result.GetFlagValue("--force"));
        }
        public void should_ingore_case_when_parese()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("read", 'r')
                                .AddFlagOption("force", 'f')
                                .AddFlagOption("check", 'c')
                                .AddFlagOption("amend", 'a')
                                .EndCommand()
                                .Build();

            ArgsParsingResult argsParsingResult = parser.Parse(new [] { "-C", "--ForCe", "-Ra" });

            Assert.True(argsParsingResult.IsSuccess);
            Assert.True(argsParsingResult.GetFlagValue("--read"));
            Assert.True(argsParsingResult.GetFlagValue("--force"));
            Assert.True(argsParsingResult.GetFlagValue("--check"));
            Assert.True(argsParsingResult.GetFlagValue("--amend"));
        }
Exemplo n.º 12
0
        public void should_be_false_when_input_empty_string()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .AddFlagOption("flag", 'f', "This is a description")
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "" });

            Assert.False(result.GetFlagValue("-f"));
        }
        public void should_parse_success_when_parse_twice()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("read", 'r')
                                .AddFlagOption("force", 'f')
                                .EndCommand()
                                .Build();

            ArgsParsingResult argsParsingResult = parser.Parse(new [] { "-r" });

            Assert.True(argsParsingResult.IsSuccess);
            Assert.True(argsParsingResult.GetFlagValue("--read"));
            Assert.False(argsParsingResult.GetFlagValue("-f"));

            parser.Parse(new [] { "-f" });
            Assert.True(argsParsingResult.IsSuccess);
            Assert.False(argsParsingResult.GetFlagValue("--read"));
            Assert.True(argsParsingResult.GetFlagValue("-f"));
        }
        public void should_throw_exception_when_get_value_of_undefined_flag()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("no-edit", 'n')
                                .EndCommand()
                                .Build();
            ArgsParsingResult argsParsingResult = parser.Parse(new [] { "--no-edit" });

            Assert.Throws <ArgumentException>(() => { argsParsingResult.GetFlagValue("--not-this-flag"); });
        }
        public void should_be_false_when_input_empty_string()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f')
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "" });

            Assert.False(result.GetFlagValue("-f"));
        }
        public void should_be_true_when_defined_abbr_form_flag_begin_with_single_dash_line()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption(null, 'f')
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "-f" });

            Assert.True(result.GetFlagValue("-f"));
        }
        public void should_be_true_when_input_defined_full_form_flag()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag-n", null)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "--flag-n" });

            Assert.True(result.GetFlagValue("--flag-n"));
        }
Exemplo n.º 18
0
        public void should_create_valid_symbol(string symbol, char?abbr, string argument)
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption(symbol, abbr, null)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new[] { argument });

            Assert.True(result.GetFlagValue(argument));
        }
        public void should_throw_when_getting_flag_in_a_failure_result()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f', string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "free_value" });

            Assert.False(result.IsSuccess);
            Assert.Throws <InvalidOperationException>(() => result.GetFlagValue("--flag"));
        }
        public void should_throw_exception_when_get_value_by_combined_input()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("read", 'r')
                                .AddFlagOption("check", 'c')
                                .AddFlagOption("force", 'f')
                                .EndCommand()
                                .Build();
            ArgsParsingResult argsParsingResult = parser.Parse(new [] { "-f" });

            Assert.Throws <ArgumentException>(() => { argsParsingResult.GetFlagValue("-rc"); });
        }
Exemplo n.º 21
0
        public void should_throw_ArgumentException_when_get_the_third_flag_value_without_parse_the_third_flag()
        {
            var fullName1     = "flag";
            var abbreviation1 = 'f';
            var fullName2     = "flagSecond";
            var abbreviation2 = 's';
            var fullName3     = "flagThird";
            var abbreviation3 = 't';
            var parser        = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption(fullName1, abbreviation1, String.Empty)
                                .AddFlagOption(fullName2, abbreviation2, String.Empty)
                                .AddFlagOption(fullName3, abbreviation3, String.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parser(new[] { "-fs" });

            Assert.True(result.IsSuccess);
            Assert.True(result.GetFlagValue("-f"));
            Assert.True(result.GetFlagValue("-s"));
            Assert.Throws <ArgumentException>(() => result.GetFlagValue("-t"));
        }
        public void should_throw_if_flag_is_null_when_getting_flag_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .ConfigFreeValue(true)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new string[0]);

            result.AssertSuccess();

            Assert.Throws <ArgumentNullException>(() => result.GetFlagValue(null));
        }
Exemplo n.º 23
0
        public void should_can_create_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f', string.Empty)
                                .EndCommand()
                                .Build();

            string[]          args   = { "--flag" };
            ArgsParsingResult result = parser.Parse(args);

            Assert.True(result.IsSuccess);
            Assert.True(result.GetFlagValue("--flag"));
        }
Exemplo n.º 24
0
        void should_throw_ArgumentException_when_get_flag_value_with_invalid_parameter()
        {
            var fullName         = "flag";
            var abbreviationName = 'f';
            var parser           = new ArgsParserBuilder()
                                   .BeginDefaultCommand()
                                   .AddFlagOption(fullName, abbreviationName, null)
                                   .EndCommand()
                                   .Build();

            ArgsParsingResult result = parser.Parser(new[] { "-f" });

            Assert.True(result.IsSuccess);
            Assert.Throws <ArgumentException>(() => result.GetFlagValue("ff"));
        }
Exemplo n.º 25
0
        void should_success_and_get_flag_value_when_add_flag_with_description_is_null()
        {
            var fullName     = "flag";
            var abbreviation = 'f';
            var parser       = new ArgsParserBuilder()
                               .BeginDefaultCommand()
                               .AddFlagOption(fullName, abbreviation, null)
                               .EndCommand()
                               .Build();

            ArgsParsingResult result = parser.Parser(new[] { "--flag" });

            Assert.True(result.IsSuccess);
            Assert.True(result.GetFlagValue("-f"));
        }
Exemplo n.º 26
0
        public void should_get_as_free_value_after_one_free_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .ConfigFreeValue(true)
                                .AddFlagOption("flag", 'f', string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "command", "-a", "-f" });

            result.AssertSuccess();
            Assert.Equal(new [] { "-a", "-f" }, result.GetUndefinedFreeValues());
            Assert.False(result.GetFlagValue("--flag"));
        }
Exemplo n.º 27
0
        void should_throw_InvalidOperationException_when_get_flag_value_if_IsSucess_is_false()
        {
            var fullName         = "flag";
            var abbreviationName = 'f';
            var parser           = new ArgsParserBuilder()
                                   .BeginDefaultCommand()
                                   .AddFlagOption(fullName, abbreviationName, null)
                                   .EndCommand()
                                   .Build();

            ArgsParsingResult result = parser.Parser(new[] { "-S" });

            Assert.False(result.IsSuccess);
            Assert.Throws <InvalidOperationException>(() => result.GetFlagValue("-f"));
        }
Exemplo n.º 28
0
        public void should_parse_success_and_can_get_flag_value_when_flag_has_abbreviation_name()
        {
            var abbrName    = 'f';
            var description = "the first flag";

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption(null, abbrName, description)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parser(new[] { "-f" });

            Assert.True(result.IsSuccess);
            Assert.True(result.GetFlagValue("-f"));
        }
        public void should_get_flag_option_with_default_command(string argumentExpression)
        {
            // start-(flag-option)-(EoA)->ok

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f', string.Empty)
                                .EndCommand()
                                .Build();

            string[]          args   = { argumentExpression };
            ArgsParsingResult result = parser.Parse(args);

            result.AssertSuccess();
            Assert.True(result.GetFlagValue(argumentExpression));
        }
Exemplo n.º 30
0
        public void should_parse_success_and_get_flag_value_when_has_flag_full_name()
        {
            var fullName    = "flag";
            var description = "the first flag";

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption(fullName, null, description)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parser(new[] { "--flag" });

            Assert.True(result.IsSuccess);
            Assert.True(result.GetFlagValue("--flag"));
            Assert.Null(result.Error);
        }