private void ValidateCanBeParsed(ICollection <string> errors, OptionPropertyMetadata property)
 {
     if (!_valueParserSelector.HasParserForProperty(property))
     {
         errors.Add($"There is no parser for the type {property.Type}, please create your own implementation and use the ValueParserAttribute on the property {property.Name}.");
     }
 }
Пример #2
0
        public IValueParser GetParserForProperty(OptionPropertyMetadata optionProperty)
        {
            if (optionProperty.HasCustomParser)
            {
                return(CreateExternalParser(optionProperty.CustomParserType));
            }

            return(DefaultParsers.GetValueParser(optionProperty.ParsedType));
        }
 private void ValidateSwitchValues(ICollection <string> errors, OptionPropertyMetadata property)
 {
     foreach (var switchValue in property.Switches.Values)
     {
         if (!property.Type.IsInstanceOfType(switchValue.Value))
         {
             errors.Add($"The switch value of type {switchValue.Value.GetType()} cannot be assigned to property of type {property.Type} (property name {property.Name})");
         }
     }
 }
        private void ValidateDefaultValue(ICollection <string> errors, OptionPropertyMetadata property)
        {
            if (property.HasDefaultValue)
            {
                var defaultValueType = property.DefaultValue?.GetType();

                if (!property.Type.IsAssignableFrom(defaultValueType))
                {
                    errors.Add(
                        $"The default value of type {defaultValueType} cannot be assigned to property of type {property.Type} (property name {property.Name})");
                }
            }
        }
        private void ValidateCustomValueParser(ICollection <string> errors, OptionPropertyMetadata property)
        {
            if (!property.HasCustomParser)
            {
                return;
            }

            var baseType = typeof(ValueParserBase <>);

            if (!property.CustomParserType.IsSubclassOfGeneric(baseType))
            {
                errors.Add($"The value parser type {property.CustomParserType} is not derived from {baseType}");
            }
        }
Пример #6
0
        public object CreateCollection(IReadOnlyList <object> items, OptionPropertyMetadata propertyMetadata)
        {
            if (propertyMetadata.CollectionType == typeof(Array))
            {
                var arrayType = propertyMetadata.ParsedType.MakeArrayType();
                var array     = (Array)Activator.CreateInstance(arrayType, items.Count);

                for (int i = 0; i < items.Count; i++)
                {
                    array.SetValue(items[i], i);
                }

                return(array);
            }

            if (propertyMetadata.CollectionType == typeof(Collection <>))
            {
                var collection = (IList)Activator.CreateInstance(propertyMetadata.Type);

                foreach (var item in items)
                {
                    collection.Add(item);
                }

                return(collection);
            }

            if (propertyMetadata.CollectionType == typeof(List <>))
            {
                var list = (IList)Activator.CreateInstance(propertyMetadata.Type);

                foreach (var item in items)
                {
                    list.Add(item);
                }

                return(list);
            }

            throw new NotSupportedException($"The collection type {propertyMetadata.CollectionType} is not supported.");
        }
Пример #7
0
        private void ParseAndSetCollectionValue(OptionsParsingContext context, OptionPropertyMetadata propertyMetadata, string[] args, ref int index)
        {
            var startIndex = index;
            var valueCount = 0;

            while (index < args.Length)
            {
                if (_optionsMetadata.IsKeyword(args[index]))
                {
                    index--;
                    break;
                }

                valueCount++;
                index++;
            }

            var parser    = _valueParserSelector.GetParserForProperty(propertyMetadata);
            var rawValues = args.GetArraySubset(startIndex, valueCount);

            context.SetCollectionValue(propertyMetadata, rawValues, parser);
        }
Пример #8
0
 public OptionValue(OptionPropertyMetadata propertyMetadata)
 {
     _propertyMetadata = propertyMetadata;
 }
Пример #9
0
        private void ParseAndSetValue(OptionsParsingContext context, OptionPropertyMetadata propertyMetadata, string rawValue)
        {
            var parser = _valueParserSelector.GetParserForProperty(propertyMetadata);

            context.SetValue(propertyMetadata, rawValue, parser);
        }
Пример #10
0
 public bool HasParserForProperty(OptionPropertyMetadata propertyMetadata)
 {
     return(propertyMetadata.HasCustomParser ||
            propertyMetadata.IsCollection ||
            DefaultParsers.HasParser(propertyMetadata.ParsedType));
 }
Пример #11
0
 public PropertyValue(OptionPropertyMetadata propertyMetadataMetadata, object value)
 {
     PropertyMetadata = propertyMetadataMetadata;
     Value            = value;
 }