static void Main(string[] args) { Console.WriteLine("args received: "); foreach (string arg in args) { Console.WriteLine(arg); } Console.WriteLine("------------"); ArgDef optionA = new ArgDef(); optionA.argLabels.Add("-a"); optionA.argLabels.Add("-optiona"); ArgDef optionB = new ArgDef(); optionB.argLabels.Add("-y"); optionB.argCount = 2; optionB.type = typeof(int); ArgDef orderedA = new ArgDef(); orderedA.name = "orderedA"; ArgDef orderedLast = new ArgDef(); orderedLast.name = "last"; ArgumentParser argParser = new ArgumentParser("testprog"); argParser.addArgDef(optionA); argParser.addArgDef(optionB); argParser.addArgDef(orderedA); argParser.addArgDef(orderedLast); ParsedArgs pArgs = null; bool exceptions = false; try { pArgs = argParser.parseArgs(args); } catch (Exception e) { exceptions = true; object message = e.Message; if (e.Message == null) { message = e; } Console.WriteLine("[Temp debug] Caught Exception '{0}'", message); Console.WriteLine("\n[Temp debug] Stack trace:\n"); Console.WriteLine(e.StackTrace); Console.WriteLine("\n[Temp debug] end stack trace.\n"); } Console.WriteLine("\n[Temp debug] ParsedArgs:\n"); Console.WriteLine(pArgs); Console.WriteLine("\n[Temp debug] end ParsedArgs:\n"); Console.WriteLine("--------------------------------------\nException Thrown?:{0}\nOption Error(s)?:{1}\n--------------------------------------", exceptions, pArgs.errorOccured()); Console.ReadKey(); }
public void consumeTestFalseOnNoLabelMatch() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-test"); testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] {"-nottest"}, 0, 1); ParsedArgs pArgs = new ParsedArgs(); bool result = testDef.consume(vArgs, pArgs); Assert.IsFalse(result, "[ArgDef][consume] consume should return false when no label matches the input"); }
public virtual void addArgDef(ArgDef argDef) { if (argDef.isOrderedArg()) { orderedArgDefs.Add(argDef); } else { labeledArgDefs.Add(argDef); } }
public void consumeTestFlag() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-t" }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); Assert.IsTrue(pArgs.getValue<bool>("t"), "[ArgDef][consume] consume should set the appropriate value for the given arg name when encountered."); }
public void consumeTestLabeledDouble() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.type = typeof(double); testDef.argCount = 1; testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-t", "33.3" }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); Assert.AreEqual(33.3, pArgs.getValue<double>("t"), 0.01, "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); }
public void consumeTestLabeledBoolAsArray() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.type = typeof(bool); testDef.argCount = 1; testDef.createArrayForArgCount1 = true; testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-t", "false" }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); bool[] result = pArgs.getArray<bool>("t"); Assert.AreEqual<int>(1, result.Length, "[ArgDef][consume] consume should create an array of size 1 when argCount is 1 and createArrayForArgCount1 is true"); Assert.IsFalse(result[0], "[ArgDef][consume] consume should set the appropriate value for the given arg name when encountered."); }
public void parseInitOrderedArgCountNeg1Exception() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.argCount = -1; testDef.parseInit(ArgTypeParser.basicParsers); }
public void parseInitOrderedArgCountMinIntException() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.argCount = int.MinValue; testDef.parseInit(ArgTypeParser.basicParsers); }
public void parseInitOrderedArgCount0NonBoolException() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.type = typeof(double); testDef.parseInit(ArgTypeParser.basicParsers); }
public void parseInitNoTypeParserException() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.type = typeof(FakeUnitTestType); testDef.parseInit(ArgTypeParser.basicParsers); }
public void consumeTestListOfString() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.argCount = 5; testDef.parseInit(ArgTypeParser.basicParsers); string[] testVals = new string[] { "ok", "", "what ", "yeyeah", "turn down" }; VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-t", testVals[0].ToString(), testVals[1].ToString(), testVals[2].ToString(), testVals[3].ToString(), testVals[4].ToString() }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); string[] result = pArgs.getArray<string>("t"); int i = 0; foreach (string val in result) { Assert.AreEqual<string>(testVals[i], val, "[ArgDef][consume] consume resulted in an array with unexpected values."); i++; } }
public void parseInitTestType() { ArgDef testDef = new ArgDef(); testDef.argLabels.AddRange(new string[] { "-t", "-test1", "-test22", "-test0" }); testDef.parseInit(ArgTypeParser.basicParsers); Assert.AreEqual<Type>(typeof(bool), testDef.type, "[ArgDef][parseInit] Expecting type bool for a labeled arg of argCount 0."); testDef = new ArgDef(); testDef.argLabels.AddRange(new string[] { "-t", "-test1", "-test22", "-test0" }); testDef.argCount++; testDef.parseInit(ArgTypeParser.basicParsers); Assert.AreEqual<Type>(typeof(string), testDef.type, "[ArgDef][parseInit] Expecting type string as the type for a labeled arg of argCount > 0 with no specified type."); testDef = new ArgDef(); testDef.argLabels.AddRange(new string[] { "-t", "-test1", "-test22", "-test0" }); testDef.argCount++; testDef.type = typeof(double); testDef.parseInit(ArgTypeParser.basicParsers); Assert.AreEqual(typeof(double), testDef.type, "[ArgDef][parseInit] Type was set to double, but the type after parseInit was not double."); }
public void consumeTestRemainingString() { ArgDef testDef = new ArgDef(); testDef.name = "t"; testDef.argCountIsRemainderOfArgs = true; testDef.parseInit(ArgTypeParser.basicParsers); string[] testVals = new string[] { "what", "the", "french toast" }; VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { testVals[0].ToString(), testVals[1].ToString(), testVals[2].ToString() }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); string[] result = pArgs.getArray<string>("t"); int i = 0; foreach (string val in result) { Assert.AreEqual<string>(testVals[i], val, "[ArgDef][consume] consume resulted in an array with unexpected values."); i++; } }
public void consumeTestOrderedInt() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.type = typeof(int); testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "33" }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("test"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); Assert.AreEqual<int>(33, pArgs.getValue<int>("test"), "[ArgDef][consume] consume should set the appropriate value for the given arg name when encountered."); }
public void consumeTestOrderedEncounteredWrongType() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.type = typeof(int); testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "a" }, 0, 1); ParsedArgs pArgs = new ParsedArgs(); bool result = testDef.consume(vArgs, pArgs); bool errors = testDef.errorOccured(); Assert.IsFalse(result, "[ArgDef][consume] consume should return false when the args provided don't match the arg def's type."); Assert.IsTrue(errors, "[ArgDef][consume] consume should generate an error when the args provided don't match the arg def's type."); }
public void consumeTestNoVArgs() { ArgDef testDef = new ArgDef(); testDef.name = "test"; testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[]{}, 0, 0); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); }
public void consumeTestMultipleOptionEncounters() { ArgDef testDef = new ArgDef(); testDef.argLabels.AddRange(new string[]{"-test", "-t"}); testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] {"-test", "-t"}, 0, 2); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); bool result2 = testDef.consume(vArgs, pArgs); bool errors = testDef.errorOccured(); Assert.IsFalse(result2, "[ArgDef][consume] consume should return false when a label is encountered twice."); Assert.IsTrue(errors, "[ArgDef][consume] consume should generate an error message when a label is encountered twice."); }
public void parseInitTestLabelName() { ArgDef testDef = new ArgDef(); testDef.argLabels.AddRange(new string[] {"-t", "-test1", "-test22", "-test0"}); testDef.parseInit(ArgTypeParser.basicParsers); Assert.AreEqual<string>("test22", testDef.name, "[ArgDef][parseInit] Wrong name chose from labels."); }
public void parseInitTestRequired() { ArgDef testDef = new ArgDef(); testDef.name = "atest"; testDef.parseInit(ArgTypeParser.basicParsers); Assert.IsTrue(testDef.required, "[ArgDef][parseInit] Ordered args should be required."); testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.parseInit(ArgTypeParser.basicParsers); Assert.IsFalse(testDef.required, "[ArgDef][parseInit] Labeled args should not be required unless required was explicitly set."); testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.required = true; Assert.IsTrue(testDef.required, "[ArgDef][parseInit] Labeled args should be required when required is explicitly set."); }
public void consumeTestWrongNumberOfFollowingArgs() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-test"); testDef.argCount = 2; testDef.parseInit(ArgTypeParser.basicParsers); VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-test", "a" }, 0, 2); ParsedArgs pArgs = new ParsedArgs(); bool result = testDef.consume(vArgs, pArgs); bool errors = testDef.errorOccured(); Assert.IsFalse(result, "[ArgDef][consume] consume should return false when there are less remaining args than argCount."); Assert.IsTrue(errors, "[ArgDef][consume] consume should generate an error message when there are less remaining args than argCount."); }
public static ArgDef buildOptionalString(string label) { ArgDef ad = new ArgDef(); ad.argLabels.Add(label); return ad; }
public void parseInitNoNameException() { ArgDef testDef = new ArgDef(); testDef.parseInit(ArgTypeParser.basicParsers); }
public void parseInitNegativeArgCountException() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.argCount = -1; testDef.parseInit(ArgTypeParser.basicParsers); }
public void consumeTestListOfDouble() { ArgDef testDef = new ArgDef(); testDef.argLabels.Add("-t"); testDef.type = typeof(double); testDef.argCount = 3; testDef.parseInit(ArgTypeParser.basicParsers); double[] testVals = new double[] { -234.556, 0, 234234.234 }; VirtualArray<string> vArgs = new VirtualArray<string>(new string[] { "-t", testVals[0].ToString(), testVals[1].ToString(), testVals[2].ToString() }); ParsedArgs pArgs = new ParsedArgs(); testDef.consume(vArgs, pArgs); Assert.IsTrue(pArgs.containsKey("t"), "[ArgDef][consume] consume should add a value to the passed in ParsedArgs when the appropriate args are given"); double[] result = pArgs.getArray<double>("t"); int i = 0; foreach (double val in result) { Assert.AreEqual<double>(testVals[i], val, "[ArgDef][consume] consume resulted in an array with unexpected values."); i++; } }