Beispiel #1
0
        public void GoodArgs_NormalResult()
        {
            IParseResult ipr = parser.Parse(new string[] { "OptOneOfEach", "-o", "1", "--switch", "value" });

            Assert.True(ipr.Ok);
            SuccessfulParse <OptOneOfEach> pr = Assert.IsType <SuccessfulParse <OptOneOfEach> >(ipr);

            Assert.NotNull(pr);
            Assert.NotNull(pr.Verb);
            Assert.NotNull(pr.Verb);
            Assert.NotNull(pr.Object);
            OptOneOfEach obj = pr.Object;

            Assert.Equal(1, obj.Option);
            Assert.True(obj.Switch);
            Assert.Equal("value", obj.Value);
        }
        internal static void ManyValueParsing(string[] args, bool shouldBeSuccessful, OptOneOfEach expectedResult)
        {
            CliParser fp = new CliParserBuilder()
                           .AddVerb <OptOneOfEach>("default", verb =>
            {
                verb.AddMultiValue(x => x.ManyValues, x => x.HelpText = "h");
                verb.AddOption(x => x.Option, x => { x.ShortName = "o"; x.LongName = "oo"; x.DefaultValue = 0; x.HelpText = "h"; });
                verb.AddSwitch(x => x.Switch, x => { x.ShortName = "s"; x.LongName = "ss"; x.HelpText = "h"; });
            }).Build();

            IParseResult result = fp.Parse(args);

            Assert.Equal(shouldBeSuccessful, result.Ok);
            if (shouldBeSuccessful)
            {
                SuccessfulParse <OptOneOfEach> tResult = Assert.IsType <SuccessfulParse <OptOneOfEach> >(result);
                OptOneOfEach x = tResult.Object !;
                Assert.NotNull(x);
                Assert.True(expectedResult.Option == x.Option);
                Assert.True(expectedResult.Switch == x.Switch);
                Assert.True(expectedResult.ManyValues.SequenceEqual(x.ManyValues));
            }
        }
        internal static void SimpleParsing(bool outcome, bool isValidVerb, Components components, string[] args)
        {
            CliParser fp = new CliParserBuilder()
                           .AddVerb <OptOneOfEach>("default", verb =>
            {
                if ((components & Components.Value) == Components.Value)
                {
                    verb.AddValue(x => x.Value, x => x.HelpText = "h");
                }
                if ((components & Components.Switch) == Components.Switch)
                {
                    verb.AddSwitch(x => x.Switch, x => { x.ShortName = "-s"; x.LongName = "--switch"; x.HelpText = "h"; });
                }
                if ((components & Components.Option) == Components.Option)
                {
                    verb.AddOption(x => x.Option, x => { x.ShortName = "-o"; x.LongName = "--option"; x.HelpText = "h"; });
                }
            }).Build();

            IParseResult parsed = fp.Parse(args);

            if (outcome)
            {
                Assert.True(parsed.Ok);
                SuccessfulParse <OptOneOfEach> parseResult = Assert.IsType <SuccessfulParse <OptOneOfEach> >(parsed);
                Assert.NotNull(parseResult.Object);
                OptOneOfEach parsedObject = parseResult.Object !;

                Assert.NotNull(parsedObject);
                if ((components & Components.Value) == Components.Value)
                {
                    Assert.Equal("Value", parsedObject.Value);
                }
                if ((components & Components.Switch) == Components.Switch)
                {
                    Assert.True(parsedObject.Switch, $"Switch was specified but didn't set property to true");
                }
                else
                {
                    Assert.False(parsedObject.Switch, $"Switch was specified but didn't set property to false");
                }
                if ((components & Components.Option) == Components.Option)
                {
                    Assert.Equal(30, parsedObject.Option);
                }
            }
            else
            {
                Assert.False(parsed.Ok);
                // Verb still shouldn't be null, since for our test cases we DO pass the verb
                if (isValidVerb)
                {
                    Assert.NotNull(parsed.Verb);
                }
                else
                {
                    Assert.Null(parsed.Verb);
                }
                Assert.NotEmpty(parsed.Errors);
            }
        }