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);
        }
        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);
        }
Exemple #3
0
        public void should_get_option_raw_value_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", "value" });

            result.AssertSuccess();
            Assert.Equal("value", result.GetOptionRawValue("--key").Single());
        }
Exemple #4
0
        public void should_get_non_required_optional_raw_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("key", 'k', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Empty(result.GetOptionRawValue("--key"));
        }
Exemple #5
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_be_ok_if_transforming_empty_option_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("integer", 'i', string.Empty, false, IntegerTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Empty(result.GetOptionValue <int>("--integer"));
        }
Exemple #7
0
        public void should_get_free_value_for_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .ConfigFreeValue(true)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal(new [] { "free-value" }, result.GetUndefinedFreeValues());
        }
        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"));
        }
Exemple #9
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"));
        }
Exemple #10
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());
        }
        public void should_transform_multiple_integer_values()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("integer", 'i', string.Empty, true, IntegerTransformer.Instance)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { "-i", "12", "--integer", "13" });

            result.AssertSuccess();
            Assert.Equal(new [] { 12, 13 }, result.GetOptionValue <int>("-i"));
        }
Exemple #12
0
        public void should_support_transformer_on_free_values()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue("name", string.Empty, false, IntegerTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal(123, result.GetFreeValue <int>("name").Single());
            Assert.Equal(123, result.GetFirstFreeValue <int>("name"));
        }
Exemple #13
0
        public void should_throw_if_get_a_undefined_free_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .ConfigFreeValue(true)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Throws <ArgumentException>(() => result.GetFreeValue("undefined_name"));
            Assert.Throws <ArgumentException>(() => result.GetFreeRawValue("undefined_name"));
        }
        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_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));
        }
Exemple #16
0
        public void should_throw_if_name_is_not_defined_when_getting_free_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddFreeValue("free_value_name", string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();

            Assert.Throws <ArgumentException>(() => result.GetFreeValue("undefined"));
            Assert.Throws <ArgumentException>(() => result.GetFreeRawValue("undefined"));
        }
Exemple #17
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"));
        }
Exemple #18
0
        public void should_recognize_free_value_for_a_flag_like_number()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddOptionWithValue("option", 'o', string.Empty, true)
                                .AddFreeValue("number", string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("option_value", result.GetFirstOptionValue <string>("-o"));
            Assert.Equal("-1", result.GetFreeRawValue("number"));
        }
Exemple #19
0
        public void should_get_option_raw_value_for_multiple_def_in_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("key-a", 'a', string.Empty)
                                .AddOptionWithValue("key-b", 'b', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("value-a", result.GetOptionRawValue("-a").Single());
            Assert.Equal("value-b", result.GetOptionRawValue("--key-b").Single());
        }
Exemple #20
0
        public void should_throw_if_option_is_not_defined_when_getting_option_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("option", 'o', string.Empty, true)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();

            Assert.Throws <ArgumentException>(() => result.GetOptionRawValue("--not-defined"));
            Assert.Throws <ArgumentException>(() => result.GetOptionValue("--not-defined"));
        }
Exemple #21
0
        public void should_ignore_uncaptured_definitions()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue("name", string.Empty)
                                .AddFreeValue("uncaptured_definition", string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("name_value", result.GetFreeRawValue("name"));
            Assert.Empty(result.GetFreeValue("uncaptured_definition"));
            Assert.Equal(string.Empty, result.GetFreeRawValue("uncaptured_definition"));
        }
        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));
        }
Exemple #23
0
        public void should_get_option_raw_value_with_default_command()
        {
            // waiting-value -(any)-> ok

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("key", 'k', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal("value", result.GetOptionRawValue("--key").First());
            Assert.Equal("value", result.GetOptionRawValue("-k").First());
        }
Exemple #24
0
        public void should_capture_multiple_free_values_with_undefined_ones()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue("name", "description")
                                .AddFreeValue("age", "description")
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(
                new[] { "command", "name_value", "age_value", "undefined_value1", "undefined_value2" });

            result.AssertSuccess();
            Assert.Equal("name_value", result.GetFreeRawValue("name"));
            Assert.Equal("age_value", result.GetFreeRawValue("age"));
            Assert.Equal(new object[] { "undefined_value1", "undefined_value2" }, result.GetUndefinedFreeValues());
        }
        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_multiple_flag_for_default_command()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .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 [] { "-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_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"));
        }
        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"));
        }
Exemple #29
0
        public void should_get_option_raw_value_even_if_value_looks_like_options_keys(string optionLikeValue)
        {
            // waiting-value -(any)-> ok

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("key", 'k', string.Empty)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal(optionLikeValue, result.GetOptionRawValue("--key").First());
            Assert.Equal(optionLikeValue, result.GetOptionRawValue("-k").First());

            Assert.Throws <ArgumentException>(() => result.GetOptionRawValue("--value"));
            Assert.Throws <ArgumentException>(() => result.GetOptionRawValue("-v"));
        }
        public void should_get_the_named_definition_basic_information()
        {
            const string name        = "free_value_name";
            const string description = "description";

            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue(name, description, false, IntegerTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();

            Assert.True(result.Command.AllowFreeValue);
            IFreeValueDefinitionMetadata definition = result.Command.GetRegisteredFreeValuesMetadata().Single();

            Assert.Equal(name, definition.Name);
            Assert.Equal(description, definition.Description);
            Assert.Same(IntegerTransformer.Instance, definition.Transformer);
        }