public static CommandArg GetCommandArg(this ParameterInfo pi)
 {
     var res = new CommandArg();
     res.Name = pi.Name;
     res.Type = pi.ParameterType;
     res.IsOptional = pi.IsOptional || pi.ParameterType.IsNullable();
     return res;
 }
        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 Eval()
        {
            var io = new StringWriter();
            var interp = new CommandInterpreter(io, io);
            var args = new CommandArg() { Type = typeof(int) }.And(new CommandArg() { Type = typeof(int) });
            interp.AddCommand("plus", args, (o, e, c) => o.WriteLine(c["0"].ConvertTo<int>() + c["1"].ConvertTo<int>()));

            interp.Eval("plus 2 2");
            Assert.AreEqual("4\r\n", io.ToString());
        }
 public void ExtraParamsThrowError()
 {
     var args = new CommandArg() { Type = typeof(string) }.List();
     Assert.Throws<ParseException>(() => new CommandParser().Parse("a b", args));
 }
        public void BadTypeThrowsParseError()
        {
            var args = new CommandArg() { Name = "f1", Type = typeof(bool) }.List();

            Assert.Throws<ParseException>(() => new CommandParser().Parse("--f1 abcd", args));
        }
        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 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 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 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 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());
        }