public void AssignArgs() { var parsed = new KeyValuePair<string, string>(null, "a") .And(new KeyValuePair<string, string>(null, "b")); var res = new CommandParser().AssignArgs(parsed, new CommandArg[] { }); Assert.AreEqual("([0, a],[1, b])", res.Print()); }
public void InterspersedFlagsDontClobberArgs() { var args = new CommandArg() { Type = typeof(string), IsOptional = false } .And(new CommandArg() { Type = typeof(string), IsOptional = false }) .And(new CommandArg() { Name = "f1", Type = typeof(bool), IsOptional = true }) .And(new CommandArg() { Name = "f2", Type = typeof(bool), IsOptional = true }); var res = new CommandParser().Parse("a --f1 b --f2", args); Assert.AreEqual("([0,a],[1,b],[f1,True],[f2,True])", res.Print()); }
public void Parse() { var res = new CommandParser().Parse("a b c"); Assert.AreEqual("([0,a],[1,b],[2,c])", res.Print()); }
public void TrimQuotesFromQuotedStrings() { var parsed = new CommandParser().Lex("a \"arg with space\" c"); Assert.AreEqual("(a,arg with space,c)", parsed.Print()); }
public void ParseFlags() { var lexed = Read.List("(a,--f1,b,--f2)"); var parsed = new CommandParser().ParseArgs(lexed, "f1".And("f2")); Assert.AreEqual("([, a],[f1, ],[, b],[f2, ])", parsed.Print()); }
public void ParseTypedArgs() { var args = new CommandArg() { Name = "1", Type = typeof(bool), IsOptional = false } .And(new CommandArg() { Name = "2", Type = typeof(int), IsOptional = false }) .And(new CommandArg() { Name = "3", Type = typeof(string), IsOptional = false }); var res = new CommandParser().Parse("false 42 abc", args); Assert.AreEqual("([1,False],[2,42],[3,abc])", res.Print()); Assert.AreEqual(typeof(bool), res["1"].GetType()); Assert.AreEqual(typeof(int), res["2"].GetType()); Assert.AreEqual(typeof(string), res["3"].GetType()); }
public void ParseTwoInts() { var args = new CommandArg() { Name = "a", Type = typeof(int), IsOptional = false } .And(new CommandArg() { Name = "b", Type = typeof(int), IsOptional = false }); var res = new CommandParser().Parse("2 2", args); Assert.AreEqual("([a,2],[b,2])", res.Print()); }
public void ParseSingleWord() { var res = new CommandParser().Parse("say"); Assert.AreEqual("([0,say])", res.Print()); }
public void ParseSwitches() { var args = new CommandArg() { Name = "s1", Type = typeof(string), IsOptional = true } .And(new CommandArg() { Name = "s2", Type = typeof(bool), IsOptional = true }); var res = new CommandParser().Parse("-s1 val1 --s2 true", args); Assert.AreEqual("([s1,val1],[s2,True])", res.Print()); }
public void ParseQuotedArgs() { var res = new CommandParser().Parse("a \"arg with space\" c"); Assert.AreEqual("([0,a],[1,arg with space],[2,c])", res.Print()); }
public void ParseQuotedSwitches() { var args = new CommandArg() { Name = "s1", Type = typeof(string), IsOptional = true } .And(new CommandArg() { Name = "s2", Type = typeof(string), IsOptional = true }); var res = new CommandParser().Parse("-s1 abc -s2 \"switch with space\"", args); Assert.AreEqual("([s1,abc],[s2,switch with space])", res.Print()); }
public void ParseOptionalArgsPositionally() { var args = new CommandArg() { Type = typeof(string), Name = "dbname", IsOptional = true }.List(); var res = new CommandParser().Parse("foosums", args); Assert.AreEqual("([dbname,foosums])", res.Print()); }
public void ParseNull() { var res = new CommandParser().Parse(null); Assert.AreEqual("()", res.Print()); }
public void ParseFlags() { var args = new CommandArg() { Name = "flag1", Type = typeof(bool), IsOptional = true } .And(new CommandArg() { Name = "flag2", Type = typeof(bool), IsOptional = true }); var res = new CommandParser().Parse("-flag1 --flag2", args); Assert.AreEqual("([flag1,True],[flag2,True])", res.Print()); }
public void ParseEmptyString() { var res = new CommandParser().Parse(""); Assert.AreEqual("()", res.Print()); }
public void ParseArgsSwitchesFlags() { var args = new CommandArg() { Type = typeof(string)} .And(new CommandArg() { Type = typeof(int)}) .And(new CommandArg() { Name = "f1", Type = typeof(bool), IsOptional = true }) .And(new CommandArg() { Name = "f2", Type = typeof(bool), IsOptional = true }) .And(new CommandArg() { Name = "s1", Type = typeof(string), IsOptional = true}) .And(new CommandArg() { Name = "s2", Type = typeof(bool), IsOptional = true}); var res = new CommandParser().Parse("abc --f1 --s2 false 42 -s1 \"some text\" -f2", args); Assert.AreEqual("([0,abc],[1,42],[f1,True],[f2,True],[s1,some text],[s2,False])", res.Print()); }