Beispiel #1
0
        public static OptionMap Create(
            object target,
            IList <Pair <PropertyInfo, VerbOptionAttribute> > verbs,
            ParserSettings settings)
        {
            var map = new OptionMap(verbs.Count, settings);

            foreach (var verb in verbs)
            {
                var optionInfo = new OptionInfo(verb.Right, verb.Left, settings.ParsingCulture)
                {
                    HasParameterLessCtor = verb.Left.PropertyType.GetConstructor(Type.EmptyTypes) != null
                };

                if (!optionInfo.HasParameterLessCtor && verb.Left.GetValue(target, null) == null)
                {
                    throw new ParserException("Type {0} must have a parameterless constructor or" +
                                              " be already initialized to be used as a verb command.".FormatInvariant(verb.Left.PropertyType));
                }

                map[verb.Right.UniqueName] = optionInfo;
            }

            map.RawOptions = target;
            return(map);
        }
Beispiel #2
0
        public OptionInfo this[string key]
        {
            get
            {
                OptionInfo option = null;

                if (_map.ContainsKey(key))
                {
                    option = _map[key];
                }
                else
                {
                    if (_names.ContainsKey(key))
                    {
                        var optionKey = _names[key];
                        option = _map[optionKey];
                    }
                }

                return(option);
            }

            set
            {
                _map[key] = value;

                if (value.HasBothNames)
                {
                    // ReSharper disable PossibleInvalidOperationException
                    _names[value.LongName] = new string(value.ShortName.Value, 1);
                    // ReSharper restore PossibleInvalidOperationException
                }
            }
        }
Beispiel #3
0
        public static OptionMap Create(object target, ParserSettings settings)
        {
            var list = ReflectionHelper.RetrievePropertyList <BaseOptionAttribute>(target);

            if (list == null)
            {
                return(null);
            }

            var map = new OptionMap(list.Count, settings);

            foreach (var pair in list)
            {
                if (pair.Left != null && pair.Right != null)
                {
                    string uniqueName;
                    if (pair.Right.AutoLongName)
                    {
                        uniqueName          = pair.Left.Name.ToLowerInvariant();
                        pair.Right.LongName = uniqueName;
                    }
                    else
                    {
                        uniqueName = pair.Right.UniqueName;
                    }

                    map[uniqueName] = new OptionInfo(pair.Right, pair.Left, settings.ParsingCulture);
                }
            }

            map.RawOptions = target;
            return(map);
        }
Beispiel #4
0
 protected static void EnsureOptionArrayAttributeIsNotBoundToScalar(OptionInfo option)
 {
     if (!option.IsArray && option.IsAttributeArrayCompatible)
     {
         throw new ParserException();
     }
 }
Beispiel #5
0
 protected static void EnsureOptionAttributeIsArrayCompatible(OptionInfo option)
 {
     if (!option.IsAttributeArrayCompatible)
     {
         throw new ParserException();
     }
 }
Beispiel #6
0
        private void BuildMutuallyExclusiveMap(OptionInfo option)
        {
            var setName = option.MutuallyExclusiveSet;

            if (!_mutuallyExclusiveSetMap.ContainsKey(setName))
            {
                _mutuallyExclusiveSetMap.Add(setName, new MutuallyExclusiveInfo(option));
            }

            _mutuallyExclusiveSetMap[setName].IncrementOccurrence();
        }
Beispiel #7
0
        private static void SetParserStateIfNeeded(object options, OptionInfo option, bool?required, bool?mutualExclusiveness)
        {
            var list = ReflectionHelper.RetrievePropertyList <ParserStateAttribute>(options);

            if (list.Count == 0)
            {
                return;
            }

            var property = list[0].Left;

            // This method can be called when parser state is still not intialized
            if (property.GetValue(options, null) == null)
            {
                property.SetValue(options, new X3Platform.CommandLine.ParserState(), null);
            }

            var parserState = (IParserState)property.GetValue(options, null);

            if (parserState == null)
            {
                return;
            }

            var error = new ParsingError
            {
                BadOption =
                {
                    ShortName = option.ShortName,
                    LongName  = option.LongName
                }
            };

            if (required != null)
            {
                error.ViolatesRequired = required.Value;
            }

            if (mutualExclusiveness != null)
            {
                error.ViolatesMutualExclusiveness = mutualExclusiveness.Value;
            }

            parserState.Errors.Add(error);
        }
Beispiel #8
0
 public MutuallyExclusiveInfo(OptionInfo option)
 {
     BadOption = option;
 }
Beispiel #9
0
 protected void DefineOptionThatViolatesFormat(OptionInfo option)
 {
     PostParsingState.Add(new ParsingError(option.ShortName, option.LongName, true));
 }