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); }
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); }
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); }
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"); } }
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"); } }
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); }
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); }
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]); }
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"); } }
public void TestCreateNamedRequiredArg() { var p = new Isotope.CommandLine.CommandLineParser(); p.AddNamedParameter("a", Isotope.CommandLine.ParameterRequirement.Required); }
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"); } }
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); }
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); }
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]); }
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); }
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); }
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); }