Esempio n. 1
0
        public void CreateInstance()
        {
            _omBuilder = new OptionMapBuilder(3);
            _omBuilder.AppendOption("p", "pretend");
            _omBuilder.AppendOption(null, "newuse");
            _omBuilder.AppendOption("D", null);

            _optionMap = _omBuilder.OptionMap;
        }
Esempio n. 2
0
        public override ParserState Parse(IStringEnumerator argumentEnumerator, OptionMap map, object options)
        {
            string[]   parts  = argumentEnumerator.Current.Substring(2).Split(new char[] { '=' }, 2);
            OptionInfo option = map[parts[0]];

            if (option == null)
            {
                return(ParserState.Failure);
            }
            option.IsDefined = true;
            if (!option.IsBoolean)
            {
                if (parts.Length == 1 && !argumentEnumerator.IsLast && !ArgumentParser.IsInputValue(argumentEnumerator.Next))
                {
                    return(ParserState.Failure);
                }
                if (parts.Length == 2)
                {
                    if (option.SetValue(parts[1], options))
                    {
                        return(ParserState.Success);
                    }
                    else
                    {
                        return(ParserState.Failure);
                    }
                }
                else
                {
                    if (option.SetValue(argumentEnumerator.Next, options))
                    {
                        return(ParserState.Success | ParserState.MoveOnNextElement);
                    }
                    else
                    {
                        return(ParserState.Failure);
                    }
                }
            }
            else
            {
                if (parts.Length == 2)
                {
                    return(ParserState.Failure);
                }
                if (option.SetValue(true, options))
                {
                    return(ParserState.Success);
                }
                else
                {
                    return(ParserState.Failure);
                }
            }
        }
        public static IOptionMap CreateMap(object target)
        {
            IList <Pair <FieldInfo, OptionAttribute> > list = ReflectionUtil.RetrieveFieldList <OptionAttribute>(target);
            IOptionMap map = new OptionMap(list.Count);

            foreach (Pair <FieldInfo, OptionAttribute> pair in list)
            {
                map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
            }
            return(map);
        }
Esempio n. 4
0
        public static OptionMap CreateMap(object target, CommandLineParserSettings settings)
        {
            var list = ReflectionUtil.RetrieveFieldList<OptionAttribute>(target);
            OptionMap map = new OptionMap(list.Count, settings);

            foreach (Pair<FieldInfo, OptionAttribute> pair in list)
            {
                map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
            }

            return map;
        }
Esempio n. 5
0
        public static OptionMap CreateMap(object target, CommandLineParserSettings settings)
        {
            var       list = ReflectionUtil.RetrieveFieldList <OptionAttribute>(target);
            OptionMap map  = new OptionMap(list.Count, settings);

            foreach (Pair <FieldInfo, OptionAttribute> pair in list)
            {
                map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
            }

            return(map);
        }
Esempio n. 6
0
        private bool DoParseArgumentsVerbs(string[] args, object options, ref object verbInstance)
        {
            var verbs    = ReflectionHelper.RetrievePropertyList <VerbOptionAttribute>(options);
            var helpInfo = ReflectionHelper.RetrieveMethod <HelpVerbOptionAttribute>(options);

            if (args.Length == 0)
            {
                if (helpInfo != null || _settings.HelpWriter != null)
                {
                    DisplayHelpVerbText(options, helpInfo, null);
                }

                return(false);
            }

            var optionMap = OptionMap.Create(options, verbs, _settings);

            if (TryParseHelpVerb(args, options, helpInfo, optionMap))
            {
                return(false);
            }

            var verbOption = optionMap[args.First()];

            // User invoked a bad verb name
            if (verbOption == null)
            {
                if (helpInfo != null)
                {
                    DisplayHelpVerbText(options, helpInfo, null);
                }

                return(false);
            }

            verbInstance = verbOption.GetValue(options);
            if (verbInstance == null)
            {
                // Developer has not provided a default value and did not assign an instance
                verbInstance = verbOption.CreateInstance(options);
            }

            var verbResult = DoParseArgumentsCore(args.Skip(1).ToArray(), verbInstance);

            if (!verbResult && helpInfo != null)
            {
                // Particular verb parsing failed, we try to print its help
                DisplayHelpVerbText(options, helpInfo, args.First());
            }

            return(verbResult);
        }
Esempio n. 7
0
        private bool DoParseArgumentsCore(string[] args, object options)
        {
            var hadError  = false;
            var optionMap = OptionMap.Create(options, _settings);

            optionMap.SetDefaults();
            var valueMapper = new ValueMapper(options, _settings.ParsingCulture);

            var arguments = new StringArrayEnumerator(args);

            while (arguments.MoveNext())
            {
                var argument = arguments.Current;
                if (string.IsNullOrEmpty(argument))
                {
                    continue;
                }

                var parser = ArgumentParser.Create(argument, _settings.IgnoreUnknownArguments);
                if (parser != null)
                {
                    var result = parser.Parse(arguments, optionMap, options);
                    if ((result & PresentParserState.Failure) == PresentParserState.Failure)
                    {
                        SetParserStateIfNeeded(options, parser.PostParsingState);
                        hadError = true;
                        continue;
                    }

                    if ((result & PresentParserState.MoveOnNextElement) == PresentParserState.MoveOnNextElement)
                    {
                        arguments.MoveNext();
                    }
                }
                else if (valueMapper.CanReceiveValues)
                {
                    if (!valueMapper.MapValueItem(argument))
                    {
                        hadError = true;
                    }
                }
            }

            hadError |= !optionMap.EnforceRules();

            return(!hadError);
        }
Esempio n. 8
0
 public void ShutdownInstance()
 {
     _optionMap = null;
 }
Esempio n. 9
0
 public OptionMapBuilder(int capacity)
 {
     _optionMap = new OptionMap(capacity, new CommandLineParserSettings(true));
     _options = new List<OptionInfo>(capacity);
     _names = new List<string>(capacity);
 }
Esempio n. 10
0
        private static OptionMap CreateMap(ref OptionMap map,  IDictionary<string, OptionInfo> optionCache)
        {
            if (map == null)
                map = new OptionMap(3, new CommandLineParserSettings(true));

            var attribute1 = new OptionAttribute("p", "pretend");
            var attribute2 = new OptionAttribute(null, "newuse");
            var attribute3 = new OptionAttribute("D", null);

            var option1 = attribute1.CreateOptionInfo();
            var option2 = attribute2.CreateOptionInfo();
            var option3 = attribute3.CreateOptionInfo();

            map[attribute1.UniqueName] = option1;
            map[attribute2.UniqueName] = option2;
            map[attribute3.UniqueName] = option3;

            if (optionCache != null)
            {
                optionCache[attribute1.UniqueName] = option1;
                optionCache[attribute1.UniqueName] = option2;
                optionCache[attribute2.UniqueName]= option3;
            }

            return map;
        }
Esempio n. 11
0
 public abstract ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options);
        public override ParserState Parse(IStringEnumerator argumentEnumerator, OptionMap map, object options)
        {
            IStringEnumerator group = new CharEnumeratorEx(argumentEnumerator.Current.Substring(1));

            while (group.MoveNext())
            {
                OptionInfo option = map[group.Current];
                if (option == null)
                {
                    return(ParserState.Failure);
                }

                option.IsDefined = true;

                if (!option.IsBoolean)
                {
                    if (argumentEnumerator.IsLast && group.IsLast)
                    {
                        return(ParserState.Failure);
                    }

                    if (!group.IsLast)
                    {
                        if (option.SetValue(group.GetRemainingFromNext(), options))
                        {
                            return(ParserState.Success);
                        }
                        else
                        {
                            return(ParserState.Failure);
                        }
                    }

                    if (!argumentEnumerator.IsLast && !ArgumentParser.IsInputValue(argumentEnumerator.Next))
                    {
                        return(ParserState.Failure);
                    }
                    else
                    {
                        if (option.SetValue(argumentEnumerator.Next, options))
                        {
                            return(ParserState.Success | ParserState.MoveOnNextElement);
                        }
                        else
                        {
                            return(ParserState.Failure);
                        }
                    }
                }
                else
                {
                    if (!group.IsLast && map[group.Next] == null)
                    {
                        return(ParserState.Failure);
                    }
                    if (!option.SetValue(true, options))
                    {
                        return(ParserState.Failure);
                    }
                }
            }
            return(ParserState.Success);
        }
Esempio n. 13
0
        public static OptionMap CreateMap(object target, CommandLineParserSettings settings)
        {
            var list = ReflectionUtil.RetrievePropertyList<OptionAttribute>(target);
            if (list != null)
            {
                var map = new OptionMap(list.Count, settings);

                foreach (var pair in list)
                {
                    if (pair != null && pair.Right != null)
                        map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
                }

                map.RawOptions = target;

                return map;
            }

            return null;
        }
        private static bool ParseArgumentList(string[] args, object options)
        {
            bool               hadError  = false;
            OptionMap          optionMap = OptionInfo.CreateMap(options);
            IList <string>     valueList = ValueListAttribute.GetReference(options);
            ValueListAttribute vlAttr    = ValueListAttribute.GetAttribute(options);

            IStringEnumerator arguments = new StringEnumeratorEx(args);

            while (arguments.MoveNext())
            {
                string argument = arguments.Current;
                if (argument != null && argument.Length > 0)
                {
                    ArgumentParser parser = ArgumentParser.Create(argument);
                    if (parser != null)
                    {
                        ParserState result = parser.Parse(arguments, optionMap, options);
                        if ((result & ParserState.Failure) == ParserState.Failure)
                        {
                            hadError = true;
                            break;
                        }
                        if ((result & ParserState.MoveOnNextElement) == ParserState.MoveOnNextElement)
                        {
                            arguments.MoveNext();
                        }
                    }
                    else if (valueList != null)
                    {
                        if (vlAttr.MaximumElements < 0)
                        {
                            lock (valueListLock)
                            {
                                valueList.Add(argument);
                            }
                        }
                        else if (vlAttr.MaximumElements == 0)
                        {
                            hadError = true;
                            break;
                        }
                        else
                        {
                            if (vlAttr.MaximumElements > valueList.Count)
                            {
                                lock (valueListLock)
                                {
                                    valueList.Add(argument);
                                }
                            }
                            else
                            {
                                hadError = true;
                                break;
                            }
                        }
                    }
                }
            }

            hadError |= !optionMap.EnforceRules();
            return(!hadError);
        }
Esempio n. 15
0
 public static IOptionMap CreateMap(object target)
 {
     IList<Pair<FieldInfo, OptionAttribute>> list = ReflectionUtil.RetrieveFieldList<OptionAttribute>(target);
     IOptionMap map = new OptionMap(list.Count);
     foreach (Pair<FieldInfo, OptionAttribute> pair in list)
     {
         map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
     }
     return map;
 }
Esempio n. 16
0
 public abstract ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options);
Esempio n. 17
0
        private bool TryParseHelpVerb(string[] args, object options, Pair <MethodInfo, HelpVerbOptionAttribute> helpInfo, OptionMap optionMap)
        {
            var helpWriter = _settings.HelpWriter;

            if (helpInfo != null && helpWriter != null)
            {
                if (string.Compare(args[0], helpInfo.Right.LongName, GetStringComparison(_settings)) == 0)
                {
                    // User explicitly requested help
                    var verb = args.FirstOrDefault();
                    if (verb != null)
                    {
                        var verbOption = optionMap[verb];
                        if (verbOption != null)
                        {
                            if (verbOption.GetValue(options) == null)
                            {
                                // We need to create an instance also to render help
                                verbOption.CreateInstance(options);
                            }
                        }
                    }

                    DisplayHelpVerbText(options, helpInfo, verb);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 18
0
        public override ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options)
        {
            innerParser = new OptionGroupParser(switchLength);

            var parts = argumentEnumerator.Current.Substring(switchLength).Split(new char[] { '=' }, 2);
            if(parts[0].Length > 1)
            {
                var option = map[parts[0]];
                if (option != null) innerParser = new LongOptionParser(switchLength);
            }

            return innerParser.Parse(argumentEnumerator, map, options);
        }