public ICollectionParser?GetParser(Type type, ValueParserProvider valueParsers) { if (type.IsArray) { var elementType = type.GetElementType(); var elementParser = valueParsers.GetParser(elementType); return(new ArrayParser(elementType, elementParser, valueParsers.ParseCulture)); } if (type.IsGenericType) { var typeDef = type.GetGenericTypeDefinition(); var elementType = type.GetGenericArguments().First(); var elementParser = valueParsers.GetParser(elementType); if (typeof(IList <>) == typeDef || typeof(IEnumerable <>) == typeDef || typeof(ICollection <>) == typeDef || typeof(IReadOnlyCollection <>) == typeDef || typeof(IReadOnlyList <>) == typeDef || typeof(List <>) == typeDef) { return(new ListParser(elementType, elementParser, valueParsers.ParseCulture)); } if (typeof(ISet <>) == typeDef || typeof(HashSet <>) == typeDef) { return(new HashSetParser(elementType, elementParser, valueParsers.ParseCulture)); } } return(null); }
internal CommandLineApplication(CommandLineApplication parent, IHelpTextGenerator helpTextGenerator, CommandLineContext context, bool throwOnUnexpectedArg) { _context = context ?? throw new ArgumentNullException(nameof(context)); Parent = parent; ThrowOnUnexpectedArgument = throwOnUnexpectedArg; Options = new List <CommandOption>(); Arguments = new List <CommandArgument>(); Commands = new List <CommandLineApplication>(); RemainingArguments = new List <string>(); HelpTextGenerator = helpTextGenerator; Invoke = () => 0; ValidationErrorHandler = DefaultValidationErrorHandler; SetContext(context); _services = new Lazy <IServiceProvider>(() => new ServiceProvider(this)); ValueParsers = new ValueParserProvider(); _conventionContext = CreateConventionContext(); if (Parent != null) { foreach (var convention in Parent._conventions) { Conventions.AddConvention(convention); } } }
public ITupleValueParser GetParser(Type type, ValueParserProvider valueParsers) { var typeInfo = type.GetTypeInfo(); if (!typeInfo.IsGenericType) { return(null); } var typeDef = typeInfo.GetGenericTypeDefinition(); if (typeDef == typeof(Tuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool)) { var innerParser = valueParsers.GetParser(typeInfo.GenericTypeArguments[1]); if (innerParser == null) { return(null); } var parserType = typeof(TupleValueParser <>).MakeGenericType(typeInfo.GenericTypeArguments[1]); return((ITupleValueParser)Activator.CreateInstance(parserType, new object[] { innerParser })); } if (typeDef == typeof(ValueTuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool)) { var innerParser = valueParsers.GetParser(typeInfo.GenericTypeArguments[1]); if (innerParser == null) { return(null); } var parserType = typeof(ValueTupleValueParser <>).MakeGenericType(typeInfo.GenericTypeArguments[1]); return((ITupleValueParser)Activator.CreateInstance(parserType, new object[] { innerParser })); } return(null); }
private CommandOptionType GetOptionType(PropertyInfo prop, ValueParserProvider valueParsers) { CommandOptionType optionType; if (OptionType.HasValue) { optionType = OptionType.Value; } else if (!CommandOptionTypeMapper.Default.TryGetOptionType(prop.PropertyType, valueParsers, out optionType)) { throw new InvalidOperationException(Strings.CannotDetermineOptionType(prop)); } return(optionType); }
public bool TryGetOptionType( Type clrType, ValueParserProvider valueParsers, out CommandOptionType optionType) { try { optionType = GetOptionType(clrType, valueParsers); return(true); } catch { optionType = default; return(false); } }
public CommandOptionType GetOptionType(Type clrType, ValueParserProvider valueParsers = null) { if (clrType == typeof(bool) || clrType == typeof(bool[])) { return(CommandOptionType.NoValue); } if (clrType == typeof(string)) { return(CommandOptionType.SingleValue); } if (clrType.IsArray || typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(clrType)) { return(CommandOptionType.MultipleValue); } var typeInfo = clrType.GetTypeInfo(); if (typeInfo.IsEnum) { return(CommandOptionType.SingleValue); } if (typeInfo.IsGenericType) { var typeDef = typeInfo.GetGenericTypeDefinition(); if (typeDef == typeof(Nullable <>)) { return(GetOptionType(typeInfo.GetGenericArguments().First(), valueParsers)); } if (typeDef == typeof(Tuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool)) { if (GetOptionType(typeInfo.GenericTypeArguments[1], valueParsers) == CommandOptionType.SingleValue) { return(CommandOptionType.SingleOrNoValue); } } if (typeDef == typeof(ValueTuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool)) { if (GetOptionType(typeInfo.GenericTypeArguments[1], valueParsers) == CommandOptionType.SingleValue) { return(CommandOptionType.SingleOrNoValue); } } } if (typeof(byte) == clrType || typeof(short) == clrType || typeof(int) == clrType || typeof(long) == clrType || typeof(ushort) == clrType || typeof(uint) == clrType || typeof(ulong) == clrType || typeof(float) == clrType || typeof(double) == clrType) { return(CommandOptionType.SingleValue); } if (valueParsers?.GetParser(clrType) != null) { return(CommandOptionType.SingleValue); } throw new ArgumentException("Could not determine CommandOptionType", clrType.Name); }