public void TestUnnamedParmeters()
        {
            var args = new string[] { "input", "output" };

            var attributeParser = new ByAttributeParser <TwoUnnamedParameters>();
            var parser          = attributeParser.PrepareParser(args);

            Assert.That(parser.UnnamedArgumentInfos.Count(), Is.EqualTo(2));
            Assert.That(parser.UnnamedArgumentInfos.ElementAt(0).Index, Is.EqualTo(0));
            Assert.That(parser.UnnamedArgumentInfos.ElementAt(1).Index, Is.EqualTo(1));

            Assert.That(parser.NamedArgumentInfos.Count(), Is.EqualTo(0));

            var result = attributeParser.FillObject();

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Input, Is.EqualTo("input"));
            Assert.That(result.Output, Is.EqualTo("output"));

            // Try to provoke an error
            args            = new string[] { };
            attributeParser = new ByAttributeParser <TwoUnnamedParameters>();
            parser          = attributeParser.PrepareParser(args);
            var parseResult = parser.ParseOrShowUsage();

            result = attributeParser.FillObject();
            Assert.That(parseResult, Is.False);
            Assert.That(parser.Errors.Count, Is.EqualTo(1));
        }
        public void TestConversionOfAttributes()
        {
            var args            = new string[] { };
            var attributeParser = new ByAttributeParser <Parameters>();
            var parser          = attributeParser.PrepareParser(args);

            Assert.That(parser, Is.Not.Null);

            var infos = parser.ArgumentInfos;

            Assert.That(infos.Count(), Is.EqualTo(2));

            var namedArgumentInfos   = infos.Where(x => x is NamedArgumentInfo);
            var unnamedArgumentInfos = infos.Where(x => x is UnnamedArgumentInfo);

            Assert.That(unnamedArgumentInfos.Count(), Is.EqualTo(1));
            Assert.That(namedArgumentInfos.Count(), Is.EqualTo(1));

            var namedArgument   = namedArgumentInfos.First() as NamedArgumentInfo;
            var unnamedArgument = unnamedArgumentInfos.First() as UnnamedArgumentInfo;

            Assert.That(namedArgument.ShortName, Is.EqualTo('v'));
            Assert.That(namedArgument.DefaultValue, Is.EqualTo("1"));
            Assert.That(namedArgument.HelpText, Is.EqualTo("Input text"));
            Assert.That(unnamedArgument.Index, Is.EqualTo(1));
            Assert.That(unnamedArgument.DefaultValue, Is.EqualTo("Default Value"));
            Assert.That(unnamedArgument.HelpText, Is.EqualTo("Input text"));
        }
Beispiel #3
0
        /// <summary>
        /// Parses the value into the a new instance of the given object type
        /// </summary>
        /// <typeparam name="T">Type to be created </typeparam>
        /// <returns>The created type, which received the argument</returns>
        /// <remarks>This class somehow violates the layering between Parser and the
        /// attribute-driven parser. To ease the use of the attribute-driven parser,
        /// the static method is included here and not in the class ByAttributeParser</remarks>
        public static T ParseIntoOrShowUsage <T>(string[] args) where T : class, new()
        {
            var byAttributeParser = new ByAttributeParser <T>();
            var parser            = byAttributeParser.PrepareParser(args);

            parser.Parse();

            if (parser.ShowUsageIfNecessary())
            {
                return(null);
            }

            var result = byAttributeParser.FillObject();

            if (parser.ShowUsageIfNecessary())
            {
                return(null);
            }

            return(result);
        }