Ejemplo n.º 1
0
        public void Valid_Complex_Required_Option_Should_be_Parsed()
        {
            SimpleRequiredConfig     oConfig = new SimpleRequiredConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string expected = "http://192.168.1.2/root-path/";

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"-required1 ""{expected}"" -required2 123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"-required1:""{expected}"" -required2:123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"/required1 ""{expected}"" /required2 123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);

            oConfig.RequiredOption1 = null;
            oParser.ParseArguments(oConfig, $@"/required1:""{expected}"" /required2:123");
            Assert.AreEqual(expected, oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);
        }
Ejemplo n.º 2
0
        public void Valid_Custom_DataType_Option_Shoud_Be_Parsed()
        {
            CustomDataTypeConfig oConfig = new CustomDataTypeConfig();

            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.RegisterCustomDataTypeHandler(typeof(Color), (name, value) =>
            {
                switch (value)
                {
                case "Red":
                    return(Color.Red);

                case "Yellow":
                    return(Color.Yellow);

                case "Green":
                    return(Color.Green);

                default:
                    return(Color.None);
                }
            },

                                                  (name, value, required) =>
            {
                return(new string[] { "None", "Red", "Yellow", "Green" }.Contains(value));
            });

            oParser.ParseArguments(oConfig, @"-color ""Green""");

            Assert.AreEqual(Color.Green, oConfig.Color);
        }
Ejemplo n.º 3
0
        public void Valid_String_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-s:""Example Name""");

            Assert.AreEqual("Example Name", oConfig.StringValueOption);
        }
Ejemplo n.º 4
0
        public void Valid_Char_Option_With_Single_Numeric_Value_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "-c 1");

            Assert.AreEqual('1', oConfig.CharValueOption);
        }
Ejemplo n.º 5
0
        public void Valid_Char_Option_With_Uppercase_Value_Should_be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "-c A");

            Assert.AreEqual('A', oConfig.CharValueOption);
        }
Ejemplo n.º 6
0
        public void Valid_UInt64_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui64 131072");

            Assert.AreEqual((ulong)131072, oConfig.UInt64ValueOption);
        }
Ejemplo n.º 7
0
        public void Valid_Int16_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-i16 32767");

            Assert.AreEqual(32767, oConfig.Int16ValueOption);
        }
Ejemplo n.º 8
0
        public void Parse_Valid_UInt16_Long_Option()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui16 65535");

            Assert.AreEqual((ushort)65535, oConfig.UInt16ValueOption);
        }
Ejemplo n.º 9
0
        public void Valid_FileInfo_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-file ""C:\Windows\explorer.exe""");

            Assert.IsNotNull(oConfig.FileInfoValueOption);
        }
Ejemplo n.º 10
0
        public void Valid_Directory_Info_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-dir ""C:\Windows""");

            Assert.IsNotNull(oConfig.DirInfoValueOption);
        }
Ejemplo n.º 11
0
        public void Valid_UInt32_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-ui32 65536");

            Assert.AreEqual((uint)65536, oConfig.UInt32ValueOption);
        }
Ejemplo n.º 12
0
        public void Valid_Bool_Option_With_AlternativeName_And_Alternative_Value_Should_be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "/? false");

            Assert.AreEqual(false, oConfig.ShowHelpOption);
        }
Ejemplo n.º 13
0
        public void Valid_DateTime_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"--date ""1970-04-01 10:43:28""");

            Assert.AreEqual(DateTime.Parse("1970-04-01 10:43:28"), oConfig.DateValueOption);
        }
Ejemplo n.º 14
0
        public void Valid_Int64_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"-l 1234567890");

            Assert.AreEqual(1234567890, oConfig.Int64ValueOption);
        }
Ejemplo n.º 15
0
        public void Valid_Bool_Options_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, "--help -v:yes");

            Assert.AreEqual(true, oConfig.ShowHelpOption);
            Assert.AreEqual(true, oConfig.VerboseMessagesOption);
        }
Ejemplo n.º 16
0
        public void Valid_Required_Option_Should_Be_Parsed()
        {
            SimpleRequiredConfig     oConfig = new SimpleRequiredConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"/required1:""abc"" -required2:123");

            Assert.AreEqual("abc", oConfig.RequiredOption1);
            Assert.AreEqual(123, oConfig.RequiredOption2);
        }
Ejemplo n.º 17
0
        public void Invalid_Char_Option_With_Long_Numeric_Value_Should_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, "-c 123");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
Ejemplo n.º 18
0
        public void Valid_Parameters_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"""First Parameter Value"" ""Second Parameter Value""");

            Assert.AreEqual("First Parameter Value", oConfig.TextValueParameter1);
            Assert.AreEqual("Second Parameter Value", oConfig.TextValueParameter2);
        }
Ejemplo n.º 19
0
        public void Invalid_UInt64_Option_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, @"-ui64 -1");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
Ejemplo n.º 20
0
        public void Invalid_Uri_Option_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult oResult = oParser.ParseArguments(oConfig, $@"/uri ""C:\\Root\\Folder""");

            Assert.AreEqual(ResultStatus.Failure, oResult.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, oResult.Errors.First().ErrorType);
        }
Ejemplo n.º 21
0
        public void Invalid_Bool_Option_With_AlternativeName_Should_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            ParseResult actual = oParser.ParseArguments(oConfig, "/? Nein");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
            Assert.AreEqual($@"Value ""Nein"" is invalid for option ""?""", actual.Errors.First().Message);
        }
Ejemplo n.º 22
0
        public void Valid_Uri_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string      expected = "http://example.com/root/";
            ParseResult oResult  = oParser.ParseArguments(oConfig, $@"/uri ""{expected}""");

            Assert.AreEqual(ResultStatus.Success, oResult.Status);
            Assert.AreEqual(expected, oConfig.UriOptionValue.ToString());
        }
Ejemplo n.º 23
0
        public void Valid_SecureString_Option_Should_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string value = "this is my secret";

            oParser.ParseArguments(oConfig, $@"-secure-string ""{value}""");

            Assert.AreEqual(value, oConfig.SecureStringOption.GetString());
        }
Ejemplo n.º 24
0
        public void Invalid_Single_Option_With_Exceeding_Value_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
            ParseResult actual = oParser.ParseArguments(oConfig, $@"--single {3.402823E+39}");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, actual.Errors.First().ErrorType);
        }
Ejemplo n.º 25
0
        public void Valid_Mixed_Arguments_With_Correct_Sequence_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            oParser.ParseArguments(oConfig, @"""First Parameter Value"" ""Second Parameter Value"" --verbose:yes -i:123");

            Assert.AreEqual("First Parameter Value", oConfig.TextValueParameter1);
            Assert.AreEqual("Second Parameter Value", oConfig.TextValueParameter2);
            Assert.AreEqual(true, oConfig.VerboseMessagesOption);
            Assert.AreEqual(123, oConfig.Int32ValueOption);
        }
Ejemplo n.º 26
0
        public void Complex_String_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            string expected = "http://192.168.1.2/root-path/";

            oConfig.StringValueOption = null;
            oParser.ParseArguments(oConfig, $@"-s ""{expected}""");
            Assert.AreEqual(expected, oConfig.StringValueOption);

            oConfig.StringValueOption = null;
            oParser.ParseArguments(oConfig, $@"-s:""{expected}""");
            Assert.AreEqual(expected, oConfig.StringValueOption);

            oConfig.StringValueOption = null;
            oParser.ParseArguments(oConfig, $@"/s ""{expected}""");
            Assert.AreEqual(expected, oConfig.StringValueOption);

            oConfig.StringValueOption = null;
            oParser.ParseArguments(oConfig, $@"/s:""{expected}""");
            Assert.AreEqual(expected, oConfig.StringValueOption);
        }
Ejemplo n.º 27
0
        public void Missing_Required_Option2_Shoud_be_Parsed_With_Error()
        {
            SimpleRequiredConfig     oConfig = new SimpleRequiredConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();
            ParseResult actual;

            actual = oParser.ParseArguments(oConfig, @"-required1 ""abc""");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(1, actual.Errors.Count());

            ParseError oActualError = actual.Errors.First();

            Assert.AreEqual(ParseErrorType.RequiredOptionValue, oActualError.ErrorType);
            Assert.AreEqual("required2", oActualError.ItemName);
        }
Ejemplo n.º 28
0
        public void Invalid_Mixed_Arguments_With_Alternative_Incorrect_Sequence_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();
            ParseResult actual;
            ParseError  oActualError;

            actual = oParser.ParseArguments(oConfig, @"""First Parameter Value"" /i:123 --verbose ""Second Parameter Value""");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(1, actual.Errors.Count());

            oActualError = actual.Errors.First();
            Assert.AreEqual(ParseErrorType.InvalidOptionValue, oActualError.ErrorType);
            Assert.AreEqual("verbose", oActualError.ItemName);
            Assert.AreEqual("Second Parameter Value", oActualError.ItemValue);
        }
Ejemplo n.º 29
0
        public void Invalid_FileInfo_Option_Shoud_Be_Parsed_With_Error()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();
            ParseResult actual;

            actual = oParser.ParseArguments(oConfig, @"-file ""?;""");

            Assert.AreEqual(ResultStatus.Failure, actual.Status);
            Assert.AreEqual(1, actual.Errors.Count());

            ParseError oActualError = actual.Errors.First();

            Assert.AreEqual(ParseErrorType.InvalidOptionValue, oActualError.ErrorType);
            Assert.AreEqual("file", oActualError.ItemName);
            Assert.AreEqual("?;", oActualError.ItemValue);
        }
Ejemplo n.º 30
0
        public void Valid_Double_Option_Shoud_Be_Parsed()
        {
            SimpleValidConfig        oConfig = new SimpleValidConfig();
            ConsoleCommandLineParser oParser = new ConsoleCommandLineParser();

            // set / restore current thread's culture due to culture-depending notation
            CultureInfo oCurrentCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                oParser.ParseArguments(oConfig, $@"--double {3.14159265358979}");

                Assert.AreEqual((double)3.14159265358979, oConfig.DoubleValueOption);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = oCurrentCulture;
            }
        }