Пример #1
0
        public void should_be_no_conflict(string s1, char?a1, string s2, char?a2, string argument1, string argument2)
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue(s1, a1, string.Empty)
                                .AddOptionWithValue(s2, a2, string.Empty)
                                .EndCommand()
                                .Build();

            ArgsParsingResult result = parser.Parse(new [] { argument1, "value1", argument2, "value2" });

            Assert.Equal("value1", result.GetFirstOptionValue <string>(argument1));
            Assert.Equal("value2", result.GetFirstOptionValue <string>(argument2));
        }
Пример #2
0
        static int RenderPredefinedMaze(ArgsParsingResult argsParsingResult, string imagePath)
        {
            string mazeKind        = argsParsingResult.GetFirstOptionValue <string>("--kind");
            int    numberOfRows    = argsParsingResult.GetFirstOptionValue <int>("--row");
            int    numberOfColumns = argsParsingResult.GetFirstOptionValue <int>("--column");

            if (numberOfRows <= 0 || numberOfColumns <= 0)
            {
                PrintUsage("InvalidArgument", $"--row {numberOfRows} --column {numberOfColumns}");
                return(InvalidArgumentCode);
            }

            using (FileStream stream = File.Create(imagePath))
            {
                return(RenderPredefinedMaze(stream, mazeKind, new MazeGridSettings(numberOfRows, numberOfColumns)));
            }
        }
        public void should_transform_integer_values()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginDefaultCommand()
                                .AddOptionWithValue("integer", 'i', string.Empty, true, IntegerTransformer.Instance)
                                .EndCommand()
                                .Build();

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

            result.AssertSuccess();
            Assert.Equal(12, result.GetOptionValue("-i").Single());
            Assert.Equal(12, result.GetFirstOptionValue <int>("--integer"));
        }
        public void should_throw_if_option_is_not_defined()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .EndCommand()
                                .Build();

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

            Assert.Throws <ArgumentException>(() => result.GetOptionValue <string>("--not_existed_option"));
            Assert.Throws <ArgumentException>(() => result.GetFirstOptionValue <string>("--not_existed_option"));
            Assert.Throws <ArgumentException>(() => result.GetFreeValue <string>("not_existed_free_value"));
            Assert.Throws <ArgumentException>(() => result.GetFirstFreeValue <string>("not_existed_free_value"));
        }
        public void should_throw_if_option_is_null()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .EndCommand()
                                .Build();

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

            Assert.Throws <ArgumentNullException>(() => result.GetOptionValue <object>(null));
            Assert.Throws <ArgumentNullException>(() => result.GetFirstOptionValue <object>(null));
            Assert.Throws <ArgumentNullException>(() => result.GetFreeValue <object>(null));
            Assert.Throws <ArgumentNullException>(() => result.GetFirstFreeValue <object>(null));
        }
Пример #6
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"));
        }
        public void should_throw_if_option_has_no_value()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddOptionWithValue("option", 'o', string.Empty)
                                .EndCommand()
                                .Build();

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

            IList <string> values = result.GetOptionValue <string>("--option");

            Assert.Empty(values);
            Assert.Throws <InvalidOperationException>(() => result.GetFirstOptionValue <string>("--option"));
        }
        public void should_throw_if_result_is_not_a_successful_one()
        {
            ArgsParser parser = new ArgsParserBuilder()
                                .BeginCommand("command", string.Empty)
                                .AddFreeValue("name", string.Empty)
                                .AddOptionWithValue("option", 'o', string.Empty, true)
                                .EndCommand()
                                .Build();

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

            Assert.False(result.IsSuccess);
            Assert.Throws <InvalidOperationException>(() => result.GetOptionValue <bool>("-o"));
            Assert.Throws <InvalidOperationException>(() => result.GetFirstOptionValue <bool>("-o"));
            Assert.Throws <InvalidOperationException>(() => result.GetFreeValue <bool>("name"));
            Assert.Throws <InvalidOperationException>(() => result.GetFirstFreeValue <bool>("name"));
        }