コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }
コード例 #3
0
 public void TrimQuotesFromQuotedStrings()
 {
     var parsed = new CommandParser().Lex("a \"arg with space\" c");
     Assert.AreEqual("(a,arg with space,c)", parsed.Print());
 }
コード例 #4
0
 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());
 }
コード例 #5
0
 public void ParseArgs()
 {
     var res = new CommandParser().ParseArgs("a".And("b").And("c"), new string[] { });
     Assert.AreEqual(3, res.Count());
 }
コード例 #6
0
        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());
        }
コード例 #7
0
        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());
        }
コード例 #8
0
 public void ParseEmptyString()
 {
     var res = new CommandParser().Parse("");
     Assert.AreEqual("()", res.Print());
 }
コード例 #9
0
 public void ParseSingleWord()
 {
     var res = new CommandParser().Parse("say");
     Assert.AreEqual("([0,say])", res.Print());
 }
コード例 #10
0
        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());
        }
コード例 #11
0
 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());
 }
コード例 #12
0
        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());
        }
コード例 #13
0
 public void ParseNull()
 {
     var res = new CommandParser().Parse(null);
     Assert.AreEqual("()", res.Print());
 }
コード例 #14
0
        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());
        }
コード例 #15
0
        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());
        }
コード例 #16
0
 public void Parse()
 {
     var res = new CommandParser().Parse("a b c");
     Assert.AreEqual("([0,a],[1,b],[2,c])", res.Print());
 }
コード例 #17
0
        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());
        }