Exemplo n.º 1
0
        public static object FetchVerbOptions(object obj, LinkedList <string> args, ParserConfiguration configuration, ref string verbsPath)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (verbsPath == null)
            {
                verbsPath = string.Empty;
            }

            if (args.Count == 0)
            {
                return(obj);
            }

            var firstArg = args.First.Value;

            foreach (var prop in PropertyFetcher.GetPropertiesWithAttributes <CommandVerbAttribute>(obj))
            {
                if (!(prop.Attribute.Name ?? prop.Property.Name).Equals(firstArg, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var verbOptions = prop.Property.GetValue(obj);
                if (verbOptions == null)
                {
                    verbOptions = configuration.InstanceFactory(prop.Property.PropertyType);
                    if (verbOptions == null)
                    {
                        throw new InvalidOperationException("Instance factory returned null instance.");
                    }

                    prop.Property.SetValue(obj, verbOptions);
                }

                if (verbsPath != string.Empty)
                {
                    verbsPath += " ";
                }

                verbsPath += args.First.Value;
                args.RemoveFirst();
                return(FetchVerbOptions(verbOptions, args, configuration, ref verbsPath));
            }

            return(obj);
        }
Exemplo n.º 2
0
        public void Write(Type optionsType, ParserConfiguration configuration)
        {
            if (optionsType == null)
            {
                throw new ArgumentNullException(nameof(optionsType));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            this.Write(PropertyFetcher.GetPropertiesWithAttributes <CommandVerbAttribute>(optionsType),
                       PropertyFetcher.GetPropertiesWithAttributes <CommandArgumentAttribute>(optionsType), configuration);
        }
Exemplo n.º 3
0
        private void ParseNoVerbInternal(object options, LinkedList <string> args)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var switches = PropertyFetcher.GetPropertiesWithAttributes <CommandArgumentAttribute>(options).ToList();

            var usedProperties = new HashSet <PropertyInfo>();

            var argsPos = 0;

            while (args.Count > 0)
            {
                argsPos += 1;

                var arg = args.First.Value;
                args.RemoveFirst();
                var matchedSwitch =
                    switches.SingleOrDefault(sw => sw.Attribute.IsNameMatch(arg, sw.Property.Name, this.Configuration));

                if (matchedSwitch == null)
                {
                    //try match by pos
                    matchedSwitch = switches.Select(sw => new
                    {
                        Attr   = sw.GetOtherAttribute <CommandInlineArgument>(),
                        Switch = sw
                    })
                                    .Where(wrapper => wrapper.Attr != null)
                                    .Where(wrap => wrap.Attr.Position == argsPos - 1)
                                    .Select(wr => wr.Switch)
                                    .SingleOrDefault();

                    if (matchedSwitch == null)
                    {
                        throw new UnknownSwitchException(arg);
                    }

                    args.AddFirst(arg);
                }

                if (!usedProperties.Add(matchedSwitch.Property))
                {
                    throw new DoubleArgumentException(matchedSwitch.Property.Name);
                }

                var typeParser = this.Configuration.TypeHandler.For(matchedSwitch.Property.PropertyType);
                if (typeParser == null)
                {
                    throw new NotSupportedTypeException(matchedSwitch.Property.PropertyType.FullName);
                }

                var result =
                    typeParser.Parse(new ParserContext(args, matchedSwitch,
                                                       localArg =>
                                                       switches.Any(
                                                           sw => sw.Attribute.IsNameMatch(localArg, sw.Property.Name, this.Configuration)),
                                                       this.Configuration));

                matchedSwitch.Property.SetValue(options, result);
            }


            var missedProperties = switches.Where(cm => !usedProperties.Contains(cm.Property)).ToList();

            foreach (var prop in missedProperties.Where(prop => prop.Attribute.Required))
            {
                throw new MissingArgumentException(prop.Property.Name.ToLower());
            }

            foreach (var prop in missedProperties)
            {
                var defaultValue = prop.Attribute.DefaultValue;
                if (defaultValue != null)
                {
                    prop.Property.SetValue(options, defaultValue);
                }
            }
        }