protected static void EnsureOptionArrayAttributeIsNotBoundToScalar(OptionInfo option) { if (!option.IsArray && option.IsAttributeArrayCompatible) { throw new ParserException(); } }
public static OptionMap Create(object target, ParserSettings settings) { IList <Pair <PropertyInfo, BaseOptionAttribute> > list = ReflectionHelper.RetrievePropertyList <BaseOptionAttribute>(target); if (list is null) { return(null); } var map = new OptionMap(list.Count, settings); foreach (Pair <PropertyInfo, BaseOptionAttribute> 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); }
protected static void EnsureOptionAttributeIsArrayCompatible(OptionInfo option) { if (!option.IsAttributeArrayCompatible) { throw new ParserException(); } }
public OptionInfo this[string key] { get { OptionInfo option = null; if (_map.ContainsKey(key)) { option = _map[key]; } else { if (_names.ContainsKey(key)) { string 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 } } }
public static OptionMap Create( object target, IList <Pair <PropertyInfo, VerbOptionAttribute> > verbs, ParserSettings settings) { var map = new OptionMap(verbs.Count, settings); foreach (Pair <PropertyInfo, VerbOptionAttribute> 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) is 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); }
private void BuildMutuallyExclusiveMap(OptionInfo option) { string setName = option.MutuallyExclusiveSet; if (!_mutuallyExclusiveSetMap.ContainsKey(setName)) { _mutuallyExclusiveSetMap.Add(setName, new MutuallyExclusiveInfo(option)); } _mutuallyExclusiveSetMap[setName].IncrementOccurrence(); }
private static void SetParserStateIfNeeded(object options, OptionInfo option, bool?required, bool?mutualExclusiveness) { IList <Pair <PropertyInfo, ParserStateAttribute> > list = ReflectionHelper.RetrievePropertyList <ParserStateAttribute>(options); if (list.Count == 0) { return; } PropertyInfo property = list[0].Left; // This method can be called when parser state is still not intialized if (property.GetValue(options, null) is null) { property.SetValue(options, new ParserState(), null); } var parserState = (IParserState)property.GetValue(options, null); if (parserState is 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); }
protected void DefineOptionThatViolatesFormat(OptionInfo option) { PostParsingState.Add(new ParsingError(option.ShortName, option.LongName, true)); }
public MutuallyExclusiveInfo(OptionInfo option) { BadOption = option; }
public override PresentParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options) { var optionGroup = new OneCharStringEnumerator(argumentEnumerator.Current.Substring(1)); while (optionGroup.MoveNext()) { OptionInfo option = map[optionGroup.Current]; if (option is null) { return(_ignoreUnkwnownArguments ? PresentParserState.MoveOnNextElement : PresentParserState.Failure); } option.IsDefined = true; EnsureOptionArrayAttributeIsNotBoundToScalar(option); if (!option.IsBoolean) { if (argumentEnumerator.IsLast && optionGroup.IsLast) { return(PresentParserState.Failure); } bool valueSetting; if (!optionGroup.IsLast) { if (!option.IsArray) { valueSetting = option.SetValue(optionGroup.GetRemainingFromNext(), options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); } EnsureOptionAttributeIsArrayCompatible(option); IList <string> items = GetNextInputValues(argumentEnumerator); items.Insert(0, optionGroup.GetRemainingFromNext()); valueSetting = option.SetValue(items, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting, true)); } if (!argumentEnumerator.IsLast && !IsInputValue(argumentEnumerator.Next)) { return(PresentParserState.Failure); } if (!option.IsArray) { valueSetting = option.SetValue(argumentEnumerator.Next, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting, true)); } EnsureOptionAttributeIsArrayCompatible(option); IList <string> moreItems = GetNextInputValues(argumentEnumerator); valueSetting = option.SetValue(moreItems, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); } if (!optionGroup.IsLast && map[optionGroup.Next] is null) { return(PresentParserState.Failure); } if (!option.SetValue(true, options)) { return(PresentParserState.Failure); } } return(PresentParserState.Success); }
public override PresentParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options) { string[] parts = argumentEnumerator.Current.Substring(2).Split(new[] { '=' }, 2); OptionInfo option = map[parts[0]]; if (option is null) { return(_ignoreUnkwnownArguments ? PresentParserState.MoveOnNextElement : PresentParserState.Failure); } option.IsDefined = true; EnsureOptionArrayAttributeIsNotBoundToScalar(option); bool valueSetting; if (!option.IsBoolean) { if (parts.Length == 1 && (argumentEnumerator.IsLast || !IsInputValue(argumentEnumerator.Next))) { return(PresentParserState.Failure); } if (parts.Length == 2) { if (!option.IsArray) { valueSetting = option.SetValue(parts[1], options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); } EnsureOptionAttributeIsArrayCompatible(option); IList <string> items = GetNextInputValues(argumentEnumerator); items.Insert(0, parts[1]); valueSetting = option.SetValue(items, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); } else { if (!option.IsArray) { valueSetting = option.SetValue(argumentEnumerator.Next, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting, true)); } EnsureOptionAttributeIsArrayCompatible(option); IList <string> items = GetNextInputValues(argumentEnumerator); valueSetting = option.SetValue(items, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); } } if (parts.Length == 2) { return(PresentParserState.Failure); } valueSetting = option.SetValue(true, options); if (!valueSetting) { DefineOptionThatViolatesFormat(option); } return(BooleanToParserState(valueSetting)); }