Beispiel #1
0
        static void Main(string[] args)
        {
            var parser = new OptionParser();

            parser.AddStringOption("I include");
            parser.AddStringOption("x xtract");
            parser.AddStringOption("t type");

            parser.AddBoolOption("h help");
            parser.AddBoolOption("V version");

            var arg_test  = "aa -I/lib1 bb -include=/lib2 -I /lib3 cc -help -xtract/dev/null --type=txt -type cpp dd".Split(" ");
            var rest_test = parser.Parse(arg_test);

            for (int i = 0; i < rest_test.Length; i++)
            {
                System.Console.WriteLine("Testing[{0}] = [{1}]", i, rest_test[i]);
            }


            var testing = parser.GetAll("include");

            for (int i = 0; i < testing.Length; i++)
            {
                System.Console.WriteLine("I Testing[{0}] = [{1}]", i, testing[i]);
            }

            testing = parser.GetAll("t");
            for (int i = 0; i < testing.Length; i++)
            {
                System.Console.WriteLine("f Testing[{0}] = [{1}]", i, testing[i]);
            }

            testing = parser.GetAll("xtract");
            for (int i = 0; i < testing.Length; i++)
            {
                System.Console.WriteLine("t Testing[{0}] = [{1}]", i, testing[i]);
            }

            if (parser.IsSet("h"))
            {
                Console.WriteLine("Help!");
            }

            if (parser.IsSet("version"))
            {
                Console.WriteLine("1.0");
            }

            if (parser.IsSet("file"))
            {
                Console.WriteLine($"file: {parser.Get("file")}");
            }


            parser.Reset();
            Console.WriteLine("Parser resatt ");

            rest_test = parser.Parse("a b c -hV d".Split(" "));
            for (int i = 0; i < rest_test.Length; i++)
            {
                System.Console.WriteLine("Testing[{0}] = [{1}]", i, rest_test[i]);
            }

            if (parser.IsSet("help"))
            {
                Console.WriteLine("Help!");
            }

            if (parser.IsSet("V"))
            {
                Console.WriteLine("1.0");
            }

            Console.WriteLine("Ferdig");
            Console.ReadLine();
        }
        public void TestParser2()
        {
            OptionParser p = new OptionParser();

            p.AddStringOption("I include");
            p.AddStringOption("x xtract");
            p.AddStringOption("t type");

            p.AddBoolOption("h help");
            p.AddBoolOption("V version");

            var argv = "aa -I/lib1 bb -include=/lib2 -I /lib3 cc -help -xtract/dev/null --type=txt -type cpp dd".Split(" ");

            var rest = p.Parse(argv);

            Assert.True(rest.Length == 4);  // aa bb cc dd
            Assert.True(Array.IndexOf(rest, "aa") != -1);
            Assert.True(Array.IndexOf(rest, "bb") != -1);
            Assert.True(Array.IndexOf(rest, "cc") != -1);
            Assert.True(Array.IndexOf(rest, "dd") != -1);

            // Any flag that represents the same option, should return the same value
            Assert.True(p.IsSet("xtract"));
            Assert.True(p.IsSet("x"));

            Assert.True(p.IsSet("help"));
            Assert.True(p.IsSet("type"));

            Assert.True(p.IsSet("I"));
            Assert.False(p.IsSet("V"));

            var iOptions = p.GetAll("I");

            Assert.True(iOptions.Length == 3);

            // They all have to be here, but can come in any order
            Assert.True(Array.IndexOf(iOptions, "/lib1") != -1);
            Assert.True(Array.IndexOf(iOptions, "/lib2") != -1);
            Assert.True(Array.IndexOf(iOptions, "/lib3") != -1);

            Assert.Equal("/dev/null", p.Get("xtract"));

            var typeOptions = p.GetAll("type");

            Assert.True(typeOptions.Length == 2);
            Assert.True(Array.IndexOf(typeOptions, "txt") != -1);
            Assert.True(Array.IndexOf(typeOptions, "cpp") != -1);

            // Unset all flags, ready to parse new input
            p.Reset();

            Assert.False(p.IsSet("xtract"));
            Assert.False(p.IsSet("x"));

            Assert.False(p.IsSet("help"));
            Assert.False(p.IsSet("type"));

            Assert.False(p.IsSet("I"));
            Assert.False(p.IsSet("version"));

            // Allow juxtaposition of boolean flags
            rest = p.Parse("a b c -hV d".Split(" "));

            Assert.True(rest.Length == 4);
            Assert.True(p.IsSet("help"));
            Assert.True(p.IsSet("version"));
        }