Exemple #1
0
        public void TestPostParseCallbacks()
        {
            int a1_v = 0;
            int a2_v = 0;
            int b_v  = 0;

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            a.OnPostParse += (parser, arg) => { a1_v = 1; };

            a.OnPostParse += (parser, arg) => { a2_v = 2; };

            b.OnPostParse += (parser, arg) => { b_v = 3; };

            string[] tokens = { "1", "x1", "-c", "3", "x2", "-b", "2", "x3", "x4" };
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
            Assert.AreEqual("3", c.Text);
            Assert.AreEqual(1, a1_v);
            Assert.AreEqual(2, a2_v);
            Assert.AreEqual(3, b_v);
        }
Exemple #2
0
        public void TestNamedOptionalWithValue()
        {
            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            string[] tokens = { "1", "x1", "-c", "3", "x2", "-b", "2", "x3", "x4" };
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
            Assert.AreEqual("3", c.Text);
        }
Exemple #3
0
        public void TestCallbacksCalledOnlyOnSuccessfulParsing()
        {
            int a1_v = 0;

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            a.OnPostParse += (parser, arg) => { a1_v = 1; };

            string[] tokens = { "1", "x1" }; // b is missing
            try
            {
                p.Parse(tokens);
            }
            catch (Isotope.CommandLine.MissingRequiredParameterException)
            {
                // this is ok
            }
            catch (System.Exception)
            {
                // this is not
                throw;
            }
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual(0, a1_v);
        }
Exemple #4
0
        public void TestDuplicateNamedArgs()
        {
            bool caught = false;
            var p = new Isotope.CommandLine.CommandLineParser();
            p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required);
            try{p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required);}
            catch (Isotope.CommandLine.GrammarException )
            {
                caught = true;
            }

            if (caught == false)
            {
                Assert.Fail("Did not catch expected exception");
            }
        }
Exemple #5
0
        public void TestPositionalArgAndNamedArgHaveSameName()
        {
            bool caught = false;
            var  p      = new Isotope.CommandLine.CommandLineParser();

            p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required);
            try { p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.NotRequired); }
            catch (Isotope.CommandLine.GrammarException)
            {
                caught = true;
            }

            if (caught == false)
            {
                Assert.Fail("Did not catch expected exception");
            }
        }
Exemple #6
0
        public void TestExcess3()
        {
            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = { "1", "x1", "-b", "2", "x2" };
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
        }
Exemple #7
0
        public void TestValuesForEmptyStringParameterValues()
        {
            // positional args always take precedence in order over named args, even if they are named on the command line

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = { "", "-b", "" };
            p.Parse(tokens);
            Assert.AreEqual("", a.Text);
            Assert.AreEqual("", b.Text);
        }
Exemple #8
0
        public void TestPositionArgsTakePrecedence()
        {
            // positional args always take precedence in order over named args, even if they are named on the command line

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            string[] tokens = { "-c", "t1", "t2", "-b", "bv", "t5", "t6" };
            p.Parse(tokens);
            Assert.AreEqual("-c", a.Text);
            Assert.AreEqual("bv", b.Text);
            Assert.IsFalse(c.HasValue);
            var unnassigned = p.GetUnassignedTokens();

            Assert.AreEqual(4, unnassigned.Count);
            Assert.AreEqual("t1", unnassigned[0]);
            Assert.AreEqual("t2", unnassigned[1]);
            Assert.AreEqual("t5", unnassigned[2]);
            Assert.AreEqual("t6", unnassigned[3]);
        }
Exemple #9
0
        public void TestMissingRequiredNamedArgThrowsException()
        {
            bool caught = false;
            var  p      = new Isotope.CommandLine.CommandLineParser();
            var  a      = p.AddPositionalParameter("a");
            var  b      = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = { "1", "2" };
            try { p.Parse(tokens); }
            catch (Isotope.CommandLine.MissingRequiredParameterException)
            {
                caught = true;
            }

            if (caught == false)
            {
                Assert.Fail("Did not catch expected exception");
            }
        }
Exemple #10
0
 public void TestCreateNamedRequiredArg()
 {
     var p = new Isotope.CommandLine.CommandLineParser();
     p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required);
 }
Exemple #11
0
        public void TestMissingRequiredNamedArgThrowsException()
        {
            bool caught = false;
            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = {"1", "2"};
            try {p.Parse(tokens);}
            catch (Isotope.CommandLine.MissingRequiredParameterException )
            {
                caught = true;
            }

            if (caught == false)
            {
                Assert.Fail("Did not catch expected exception");
            }
        }
Exemple #12
0
        public void TestValuesForEmptyStringParameterValues()
        {
            // positional args always take precedence in order over named args, even if they are named on the command line

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = {"", "-b", ""};
            p.Parse(tokens);
            Assert.AreEqual("", a.Text);
            Assert.AreEqual("", b.Text);
        }
Exemple #13
0
        public void TestCallbacksCalledOnlyOnSuccessfulParsing()
        {
            int a1_v = 0;

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            a.OnPostParse += (parser, arg) => { a1_v = 1; };

            string[] tokens = {"1", "x1"}; // b is missing
            try
            {
                p.Parse(tokens);
            }
            catch (Isotope.CommandLine.MissingRequiredParameterException)
            {
                // this is ok
            }
            catch (System.Exception)
            {
                // this is not
                throw;
            }
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual(0, a1_v);
        }
Exemple #14
0
        public void TestPositionArgsTakePrecedence()
        {
            // positional args always take precedence in order over named args, even if they are named on the command line

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            string[] tokens = {"-c", "t1", "t2", "-b", "bv", "t5", "t6"};
            p.Parse(tokens);
            Assert.AreEqual("-c", a.Text);
            Assert.AreEqual("bv", b.Text);
            Assert.IsFalse(c.HasValue);
            var unnassigned = p.GetUnassignedTokens();
            Assert.AreEqual(4, unnassigned.Count);
            Assert.AreEqual("t1", unnassigned[0]);
            Assert.AreEqual("t2", unnassigned[1]);
            Assert.AreEqual("t5", unnassigned[2]);
            Assert.AreEqual("t6", unnassigned[3]);
        }
Exemple #15
0
        public void TestPostParseCallbacks()
        {
            int a1_v = 0;
            int a2_v = 0;
            int b_v = 0;

            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            a.OnPostParse += (parser, arg) => { a1_v = 1; };

            a.OnPostParse += (parser, arg) => { a2_v = 2; };

            b.OnPostParse += (parser, arg) => { b_v = 3; };

            string[] tokens = {"1", "x1", "-c", "3", "x2", "-b", "2", "x3", "x4"};
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
            Assert.AreEqual("3", c.Text);
            Assert.AreEqual(1, a1_v);
            Assert.AreEqual(2, a2_v);
            Assert.AreEqual(3, b_v);
        }
Exemple #16
0
        public void TestNamedOptionalWithValue()
        {
            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);
            var c = p.AddNamedParameter("c", Isotope.CommandLine.ParameterRequirement.NotRequired);

            string[] tokens = {"1", "x1", "-c", "3", "x2", "-b", "2", "x3", "x4"};
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
            Assert.AreEqual("3", c.Text);
        }
Exemple #17
0
        public void TestExcess3()
        {
            var p = new Isotope.CommandLine.CommandLineParser();
            var a = p.AddPositionalParameter("a");
            var b = p.AddNamedParameter("b", Isotope.CommandLine.ParameterRequirement.Required);

            string[] tokens = {"1", "x1", "-b", "2", "x2"};
            p.Parse(tokens);
            Assert.AreEqual("1", a.Text);
            Assert.AreEqual("2", b.Text);
        }
Exemple #18
0
        public void TestCreateNamedRequiredArg()
        {
            var p = new Isotope.CommandLine.CommandLineParser();

            p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required);
        }