コード例 #1
0
        public void TestParse()
        {
            // Arrange
            var parser = new ParameterParserBuilder()
                         .AddFlag("Foo", shortName: "f", longName: "foo")
                         .AddFlag("Bar", longName: "bar")
                         .AddValue("OptionalValue", longName: "optional")
                         .AddValue("MandatoryValue", shortNames: new[] { "m", "A" }, optional: false)
                         .AddValue("SecondMandatory", longNames: new[] { "second", "mandatory" }, optional: false)
                         .Build();

            var cliParams = "--optional ValueOne -f --mandatory ValueTwo ArgOne ArgTwo".Split(' ');

            // Act
            var result = parser.Parse(cliParams);

            // Assert
            Assert.NotNull(result, "Result nullability");
            Assert.IsTrue(result.HasParameter("Foo"), "Parameter Foo present");
            Assert.IsFalse(result.HasParameter("Bar"), "Parameter Bar present");
            Assert.IsTrue(result.HasParameter("OptionalValue"), "Parameter OptionalValue present");
            Assert.IsFalse(result.HasParameter("MandatoryValue"), "Parameter MandatoryValue present");
            Assert.IsTrue(result.HasParameter("SecondMandatory"), "Parameter SecondMandatory present");

            Assert.AreEqual("ValueOne", result.GetParameterValue("OptionalValue"), "OptionalValue value");
            Assert.AreEqual("ValueTwo", result.GetParameterValue("SecondMandatory"), "SecondMandatory value");

            Assert.AreEqual(new[] { "ArgOne", "ArgTwo" }, result.GetRemainingArguments(), "Remaining args");
        }
コード例 #2
0
        public void TestParseEmptyOptionalValue()
        {
            // Arrange
            var parser = new ParameterParserBuilder <SampleClass>()
                         .AddFlag(x => x.Foo, shortName: "f", longName: "foo")
                         .AddFlag(x => x.Bar, longName: "bar")
                         .AddValue(x => x.OptionalValue, longName: "optional")
                         .AddValue(x => x.MandatoryValue, shortNames: new[] { "m", "A" }, optional: false)
                         .AddValue(x => x.SecondMandatory, longNames: new[] { "second", "mandatory" }, optional: false)
                         .RemainingArguments(x => x.RemainingArgs)
                         .Build();

            var cliParams = "--optional -f --mandatory ValueTwo ArgOne ArgTwo".Split(' ');

            // Act
            var result = parser.Parse(cliParams);

            // Assert
            Assert.NotNull(result, "Result nullability");
            Assert.IsTrue(result.Foo, "Parameter Foo present");
            Assert.IsFalse(result.Bar, "Parameter Bar present");
            Assert.NotNull(result.OptionalValue, "Parameter OptionalValue present");
            Assert.IsNull(result.MandatoryValue, "Parameter MandatoryValue present");
            Assert.NotNull(result.SecondMandatory, "Parameter SecondMandatory present");

            Assert.AreEqual(String.Empty, result.OptionalValue, "OptionalValue value");
            Assert.AreEqual("ValueTwo", result.SecondMandatory, "SecondMandatory value");

            Assert.AreEqual(new[] { "ArgOne", "ArgTwo" }, result.RemainingArgs, "Remaining args");
        }