public void TestHypensInArguments() { string[] args = "-somefile \"this-is-a-terrible-filename.txt\" -another param ".Split(' '); var dest = new CommandTestObj(); CmdLineArg[] Arguments = new CmdLineArg[] { new CmdLineArg() { Flag = "somefile", DataType=typeof(string), PropertyName="somefile"}, new CmdLineArg() { Flag = "another", DataType=typeof(string), PropertyName="another"} }; CmdLineJobBase cmds = new CmdLineJobBase(); Assert.IsTrue(cmds.Load(args, Arguments, dest), "Load failed!"); Assert.AreEqual("param", dest.another, "parameter after filename with hypens was clobbered"); Assert.AreEqual("this-is-a-terrible-filename.txt", dest.somefile, "failed on filename with hyphens"); }
public void TestInvalidArguments() { string[] args = "-realarg abcde -realagr defghi ".Split(' '); var dest = new CommandTestObj(); CmdLineArg[] Arguments = new CmdLineArg[] { new CmdLineArg() { Flag = "realarg", DataType=typeof(string), PropertyName="somefile"} }; CmdLineJobBase cmds = new CmdLineJobBase(); bool loadDidFail = cmds.Load(args, Arguments, dest); //switched this, nunit was being weird Assert.IsTrue(loadDidFail == false, "Load Succeeded???"); //assert should pass, load should fail }
public void TestParamDisambiguation() { string[] args = "-f 1 -fo 2 -foo 3".Split(' '); var dest = new CommandTestObj(); CmdLineArg[] Arguments = new CmdLineArg[] { new CmdLineArg() { Flag = "f", DataType=typeof(int), PropertyName="f"}, new CmdLineArg() { Flag = "fo", DataType=typeof(int), PropertyName="fo"}, new CmdLineArg() { Flag = "foo", DataType=typeof(int), PropertyName="foo"}, }; CmdLineJobBase cmds = new CmdLineJobBase(); Assert.IsTrue(cmds.Load(args, Arguments, dest), "Load failed!"); Assert.AreEqual(1, dest.f, "failed on 'f'"); Assert.AreEqual(2, dest.fo, "failed on 'fo'"); Assert.AreEqual(3, dest.foo, "failed on 'foo'"); }
public void TestTypeConversion() { string[] args = "-string abcdef ghijklmno pqrstuvwxyz -double 3.14159265359 -int 8".Split(' '); var dest = new CommandTestObj(); CmdLineArg[] Arguments = new CmdLineArg[] { new CmdLineArg() { Flag = "string", DataType=typeof(string), PropertyName="str"}, new CmdLineArg() { Flag = "double", DataType=typeof(double), PropertyName="dbl"}, new CmdLineArg() { Flag = "int", DataType=typeof(int), PropertyName="it"}, }; CmdLineJobBase cmds = new CmdLineJobBase(); Assert.IsTrue(cmds.Load(args, Arguments, dest), "Load failed!"); Assert.AreEqual("abcdef ghijklmno pqrstuvwxyz", dest.str, "failed on string with spaces"); Assert.AreEqual(3.14159265359, dest.dbl, "failed on double parsing"); Assert.AreEqual(8, dest.it, "failed on integer parsing"); }
public bool Load(string[] args, CmdLineArg[] availFlags, object dest) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < args.Length; i++) { sb.Append(args[i]).Append(' '); } string line = sb.ToString(); char delim = '-'; if (line.IndexOf(delim) == -1) { //if we have arguments, but they didn't include flags, it's probably a file, //look for the file. if (File.Exists(args[0])) { var lines = File.ReadAllLines(args[0]); sb = new StringBuilder(); foreach (string chunk in lines) { if (chunk.StartsWith("#") || chunk.StartsWith("/")) continue; sb.Append(chunk).Append(" "); } line = sb.ToString(); } else { _log.ErrorFormat("Provided config file didn't exist {0}", args[0]); } } HashSet<string> knownFlags = new HashSet<string>(); foreach (var arg in availFlags) { var flag = arg.Flag.ToLower(); if (!knownFlags.Contains(flag)) { knownFlags.Add(flag); } else { _log.ErrorFormat("Programmer error, the flag {0} appears more than once ", flag); return false; } } var thisType = dest.GetType(); int idx = line.IndexOf(delim); //grab the first flag while (idx >= 0) { int nextSpace = line.IndexOf(' ', idx + 1); string flag = line.Substring(idx + 1, nextSpace - (idx + 1)).ToLower(); string contents = string.Empty; idx += 1 + flag.Length; if (!knownFlags.Contains(flag)) { _log.ErrorFormat("Invalid parameter: {0}", flag.Substring(0, Math.Min(512, flag.Length))); return false; } int end = 0; int nextQuote = line.IndexOf('\"', idx); if (nextQuote >= 0) { int quoteEnd = line.IndexOf('\"', nextQuote + 1); if (quoteEnd >= 0) { end = quoteEnd + 1; } else { _log.Error("Unterminated Quote found in arguments."); return false; } } else { end = line.IndexOf(delim, idx); } if (end == -1) { end = line.Length; } if (end > idx) { contents = line.Substring(idx, end - idx).Trim(); if (string.IsNullOrEmpty(contents)) { contents = true.ToString(); } else if (contents.StartsWith("\"")) { contents = contents.Trim('\"'); } idx = end; } //find the next flag. idx = line.IndexOf(delim, idx); for (int p = 0; p < availFlags.Length; p++) { var arg = availFlags[p]; if (arg.Flag.Equals(flag, StringComparison.CurrentCultureIgnoreCase)) { var prop = thisType.GetProperty(arg.PropertyName); prop.SetValue(dest, Utilities.GetAsType(prop.PropertyType, contents, null), null); break; } } } return true; }