public void should_get_option_basic_metadata()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFlagOption("flag", 'f', "flag description")
                                .AddOptionWithValue("key-value", 'k', "key value description")
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();

            IOptionDefinitionMetadata[] optionDefinitionMetadatas =
                result.Command.GetRegisteredOptionsMetadata().ToArray();
            IOptionDefinitionMetadata flagMetadata = optionDefinitionMetadatas
                                                     .Single(d => d.SymbolMetadata.FullForm.Equals("flag", StringComparison.OrdinalIgnoreCase));
            IOptionDefinitionMetadata kvMetadata = optionDefinitionMetadatas
                                                   .Single(d => d.SymbolMetadata.FullForm.Equals("key-value", StringComparison.OrdinalIgnoreCase));

            Assert.Equal("flag", flagMetadata.SymbolMetadata.FullForm);
            Assert.Equal('f', flagMetadata.SymbolMetadata.Abbreviation);
            Assert.Equal("flag description", flagMetadata.Description);
            Assert.Equal(OptionType.Flag, flagMetadata.Type);
            Assert.False(flagMetadata.IsRequired);

            Assert.Equal("key-value", kvMetadata.SymbolMetadata.FullForm);
            Assert.Equal('k', kvMetadata.SymbolMetadata.Abbreviation);
            Assert.Equal("key value description", kvMetadata.Description);
            Assert.Equal(OptionType.KeyValue, kvMetadata.Type);
            Assert.False(kvMetadata.IsRequired);
        }
예제 #2
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_command_symbol(string validSymbol)
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand(validSymbol, string.Empty).EndCommand()
                                .Build();

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

            Assert.Equal(validSymbol, result.Command.Symbol);
        }
        public void should_accept_null_description()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("valid_symbol", null).EndCommand()
                                .Build();

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

            Assert.Equal(string.Empty, result.Command.Description);
        }
예제 #5
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"));
        }
예제 #6
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-"));
        }
        public void should_not_single_lined_the_description(string multiLined, string expected)
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", multiLined).EndCommand()
                                .Build();

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

            Assert.Equal(expected, result.Command.Description);
        }
예제 #8
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"));
        }
예제 #9
0
        void result_command_should_be_null_when_parse_failed()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand().EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(new[] { "-f" });

            Assert.False(result.IsSuccess);
            Assert.Null(result.Command);
        }
예제 #10
0
 public static void AssertError(
     this ArgsParsingResult result,
     ArgsParsingErrorCode code,
     string trigger)
 {
     Assert.False(result.IsSuccess);
     Assert.Null(result.Command);
     Assert.NotNull(result.Error);
     Assert.Equal(code, result.Error.Code);
     Assert.Equal(trigger, result.Error.Trigger);
 }
        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"); });
        }
예제 #12
0
        void command_symbol_of_parsing_result_should_be_null_when_parse_default_command_successfully()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand().EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(Array.Empty <string>());

            Assert.True(result.IsSuccess);
            Assert.NotNull(result.Command);
            Assert.Null(result.Command.Symbol);
        }
예제 #13
0
        public void should_get_null_command_when_parese_failed()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(new [] { "--flat" });

            Assert.False(result.IsSuccess);
            Assert.Null(result.Command);
        }
        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_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"));
        }
예제 #16
0
        public void should_get_empty_string_when_get_default_command_description()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flat", 'f')
                                .EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(new [] { "--flat" });

            Assert.True(result.IsSuccess);
            Assert.Equal(string.Empty, result.Command.Description);
        }
예제 #17
0
        void should_return_empty_description_for_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFlagOption("flag", 'f', "flag description")
                                .EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(new[] { "--flag" });

            Assert.True(result.IsSuccess);
            Assert.Equal(string.Empty, result.Command.Description);
        }
예제 #18
0
        public void should_get_correct_command_when_parese_success()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .EndCommand()
                                .Build();
            ArgsParsingResult result = parser.Parse(Array.Empty <string>());

            Assert.True(result.IsSuccess);
            Assert.NotNull(result.Command);
            Assert.Null(result.Command.Symbol);
        }
        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"));
        }
예제 #20
0
        public void should_throw_if_mandatory_free_value_is_not_present()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFreeValue("name", string.Empty, true, DefaultTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertError(ArgsParsingErrorCode.RequiredFreeValueNotPresent, "<name>");
        }
예제 #21
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_get_default_command_symbol()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand().EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(Array.Empty <string>());

            result.AssertSuccess();
            Assert.True(result.Command.IsDefaultCommand());
            Assert.Null(result.Command.Symbol);
        }
예제 #23
0
        public void should_get_cannot_find_value_if_no_value_is_provided_for_key_value_option_for_specified_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddOptionWithValue("key", 'k', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertError(ArgsParsingErrorCode.CannotFindValueForOption, "-k");
        }
        public void should_get_not_match_any_command_error_for_non_argument_if_default_command_is_not_set()
        {
            // start-(EoA)->no default command ? error

            var parser = new ArgsParserBuilder().Build();

            ArgsParsingResult result = parser.Parse(Array.Empty <string>());

            result.AssertError(
                ArgsParsingErrorCode.DoesNotMatchAnyCommand,
                "Unexpected end of arguments.");
        }
예제 #25
0
        public void should_get_free_value_continuously_for_specified_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .ConfigFreeValue(true)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal(new [] { "free-value1", "free-value2" }, result.GetUndefinedFreeValues());
        }
예제 #26
0
        public void should_capture_single_free_variable()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue("name", "description")
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("free_value", result.GetFreeRawValue("name"));
        }
예제 #27
0
        public void should_support_mandatory_free_values_for_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFreeValue("name", string.Empty, true, DefaultTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("free_value", result.GetFirstFreeValue <string>("name"));
        }
        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"); });
        }
        public void should_get_flag_for_specified_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFlagOption("flag", 'f', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.True(result.GetFlagValue("--flag"));
        }
        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"));
        }