public void Example_Documentation()
        {
            var swA = new CommandSwitch("AAA", 'a');

            swA.AddParameter(Arity.One, "a-param");
            swA.SetHelp("Help for A.");

            var swB = new CommandSwitch("be");

            swB.AddParameter(Arity.NoneOrOne, "b-param");
            swB.SetHelp("Help for B.");

            var parser = new Parser();

            parser.AddSwitch(swA);
            parser.AddSwitch(swB);

            var result = parser.Parse(new string[] { "-a", "111", "--be=222", "path1" });

            Assert.AreEqual(1, result.pathsCount);
            Assert.AreEqual("path1", result.paths[0]);
            Assert.AreEqual(2, result.switchesCount);
            Assert.AreEqual("AAA", result.switches[0].primaryName);
            Assert.AreEqual("111", result.switches[0].args[0]);
            Assert.AreEqual("be", result.switches[1].primaryName);
            Assert.AreEqual("222", result.switches[1].args[0]);
        }
        public void RepeatedSwitches_OneOrNoneArgIncorrect_Throws()
        {
            var parser = new Parser();
            var swA    = new CommandSwitch("AA", 'a');

            swA.AddParameter(Arity.NoneOrOne, "ones");
            parser.AddSwitch(swA);

            Assert.Throws <InvalidInput>(() => parser.Parse(new string[] { "-a=111", "--AA=222" }));
        }
        public void SwitchesWithArguments_OneOrNone_NoValue()
        {
            var parser = new Parser();
            var swA    = new CommandSwitch("a");

            swA.AddParameter(Arity.NoneOrOne, "ones");
            parser.AddSwitch(swA);

            var result = parser.Parse(new string[] { "-a" });

            Assert.AreEqual(0, result.pathsCount);
            Assert.AreEqual(1, result.switchesCount);
            Assert.AreEqual("a", result.switches[0].primaryName);
            Assert.AreEqual(0, result.switches[0].argsCount);
        }
Esempio n. 4
0
        public void GetHelp_MaybeParam()
        {
            var sw1 = new CommandSwitch("switch");

            sw1.SetHelp("Help here");
            sw1.AddParameter(Arity.NoneOrOne, "p");

            StringBuilder sb = new StringBuilder();

            sw1.GetHelp(new Setup(), sb);
            var actual   = sb.ToString();
            var expected = "    --switch[=p]          Help here\n";

            Assert.AreEqual(expected, actual);
        }
Esempio n. 5
0
        public void GetHelp_WhenNoLongNameAndNoAltNames()
        {
            var sw1 = new CommandSwitch("s");

            sw1.SetHelp("Help here");
            sw1.AddParameter(Arity.NoneOrOne, "p");

            StringBuilder sb = new StringBuilder();

            sw1.GetHelp(new Setup(), sb);
            var actual   = sb.ToString();
            var expected = " -s[=p]                   Help here\n";

            Assert.AreEqual(expected, actual);
        }
        public void RepeatedSwitches_OneArg()
        {
            var parser = new Parser();
            var swA    = new CommandSwitch("AA", 'a');

            swA.AddParameter(Arity.One, "ones");
            parser.AddSwitch(swA);

            var result = parser.Parse(new string[] { "-a", "111", "--AA" });

            Assert.AreEqual(0, result.pathsCount);
            Assert.AreEqual(1, result.switchesCount);
            Assert.AreEqual("AA", result.switches[0].primaryName);
            Assert.AreEqual(1, result.switches[0].argsCount);
            Assert.AreEqual("111", result.switches[0].args[0]);
        }
Esempio n. 7
0
        public void GetHelp_OneParam()
        {
            var sw1 = new CommandSwitch("switch", 's');

            sw1.AddAlternativeNames("alt");
            sw1.SetHelp("Help here");
            sw1.AddParameter(Arity.One, "p");

            StringBuilder sb = new StringBuilder();

            sw1.GetHelp(new Setup(), sb);
            var actual   = sb.ToString();
            var expected = " -s --switch, --alt <p>   Help here\n";

            Assert.AreEqual(expected, actual);
        }
        public void SwitchesWithArguments_Many()
        {
            var parser = new Parser();
            var swA    = new CommandSwitch("a");

            swA.AddParameter(Arity.Any, "ones");
            parser.AddSwitch(swA);

            var result = parser.Parse(new string[] { "-a", "111", "222" });

            Assert.AreEqual(0, result.pathsCount);
            Assert.AreEqual(1, result.switchesCount);
            Assert.AreEqual("a", result.switches[0].primaryName);
            Assert.AreEqual(2, result.switches[0].argsCount);
            Assert.AreEqual("111", result.switches[0].args[0]);
            Assert.AreEqual("222", result.switches[0].args[1]);
        }
        public void SwitchesWithArguments_Many_InterruptedBySwitch()
        {
            var parser = new Parser();
            var swA    = new CommandSwitch("a");
            var swBE   = new CommandSwitch("be");

            swA.AddParameter(Arity.Any, "ones");
            swBE.AddParameter(Arity.NoneOrOne, "threes");
            parser.AddSwitch(swA);
            parser.AddSwitch(swBE);

            var result = parser.Parse(new string[] { "-a", "111", "222", "--be=333" });

            Assert.AreEqual(0, result.pathsCount);
            Assert.AreEqual(2, result.switchesCount);
            Assert.AreEqual("a", result.switches[0].primaryName);
            Assert.AreEqual(2, result.switches[0].argsCount);
            Assert.AreEqual("111", result.switches[0].args[0]);
            Assert.AreEqual("222", result.switches[0].args[1]);
            Assert.AreEqual("be", result.switches[1].primaryName);
            Assert.AreEqual("333", result.switches[1].args[0]);
        }