public T GetValue <T>(string argumentString) { argumentString = argumentString.Trim().ToLower(); ArgumentSchema s = this.marshalers.Keys .Where(a => a.Argument.Contains(argumentString)) .FirstOrDefault() ?? throw new ArgumentException(ErrorCode.INVALID_PARAMETER, argumentString); Marshaler m = this.marshalers .Where(a => a.Key.Argument.Contains(argumentString)) .FirstOrDefault().Value; try { if ((T)(m.Value) == null) { throw new NullReferenceException(); } return((T)(m.Value)); } catch (NullReferenceException) { if (s.Required == true) { throw new ArgumentException(ErrorCode.MISSING, string.Join(ArgumentResource.Separator, s.Argument), null); } return(default);
private static void PrintValidationResults(ICliContext context, ValidationException ex) { context.Console.Error.WithForegroundColor(ConsoleColor.Red, (error) => error.WriteLine("Validation failed:")); foreach (IGrouping <string, ValidationFailure> group in ex.Errors.GroupBy(x => x.PropertyName)) { ArgumentSchema property = context.CommandSchema.GetArguments().Where(x => x.Property?.Name == group.Key).First(); string name = group.Key; if (property is CommandOptionSchema option) { name = "--" + option.Name; } else if (property is CommandParameterSchema parameter) { name = $"Parameter {parameter.Order}"; } context.Console.Error.Write(" "); context.Console.Error.WithForegroundColor(ConsoleColor.Cyan, (error) => error.Write(name)); context.Console.Error.Write(" "); context.Console.Error.WithForegroundColor(ConsoleColor.Green, (error) => error.Write($"[{group.First().AttemptedValue}]")); context.Console.Error.WriteLine(" "); foreach (var validationError in group) { context.Console.Error.Write(" -- "); context.Console.Error.WithForegroundColor(ConsoleColor.White, (error) => error.WriteLine(validationError.ErrorMessage)); } context.Console.Error.WriteLine(); } }
public static TypinException CannotConvertMultipleValuesToNonScalar(ArgumentSchema argument, IReadOnlyList <string> values) { return(argument switch { CommandParameterSchema parameter => CannotConvertMultipleValuesToNonScalar(parameter, values), CommandOptionSchema option => CannotConvertMultipleValuesToNonScalar(option, values), _ => throw new ArgumentOutOfRangeException(nameof(argument)) });
private static object?ConvertScalar(this ArgumentSchema argumentSchema, string?value, Type targetType) { try { // No conversion necessary if (targetType == typeof(object) || targetType == typeof(string)) { return(value); } // Bool conversion (special case) if (targetType == typeof(bool)) { return(string.IsNullOrWhiteSpace(value) || bool.Parse(value)); } // Char conversion (special case) if (targetType == typeof(char)) { return(TextUtils.UnescapeChar(value)); } // Primitive conversion Func <string, object?>?primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType); if (primitiveConverter is not null && !string.IsNullOrWhiteSpace(value)) { return(primitiveConverter(value)); } // Enum conversion conversion if (targetType.IsEnum && !string.IsNullOrWhiteSpace(value)) { return(Enum.Parse(targetType, value ?? string.Empty, true)); } // Nullable<T> conversion Type?nullableUnderlyingType = targetType.TryGetNullableUnderlyingType(); if (nullableUnderlyingType is not null) { return(!string.IsNullOrWhiteSpace(value) ? ConvertScalar(argumentSchema, value, nullableUnderlyingType) : null); } // String-constructible conversion ConstructorInfo?stringConstructor = targetType.GetConstructor(new[] { typeof(string) }); if (stringConstructor is not null) { return(stringConstructor.Invoke(new object?[] { value })); } // String-parseable (with format provider) conversion MethodInfo?parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true); if (parseMethodWithFormatProvider is not null) { return(parseMethodWithFormatProvider.Invoke(null, new object[] { value !, FormatProvider })); }
private void ParseSchemaArgument(ArgumentSchema argument) { try { Marshaler marshaler = reflection.GetInstanceByProperty <Marshaler>(nameof(marshaler.Schema), argument.Marshaler); this.marshalers.Add(argument, marshaler); } catch (ReflectionException ex) { throw new ArgumentException(ErrorCode.REFLECTION, ex.ErrorMessage()); } }
private static object?ConvertScalar(this ArgumentSchema argumentSchema, string?value, Type targetType, IBindingConverter?converterInstance) { try { if (converterInstance is not null && targetType.IsAssignableFrom(converterInstance.TargetType)) { // Structs do not support inheritance, so we want to execute our Nullable<T> handling logic if converter is for T, not Nullable<T>. // If value is null or whitespace, we must check if this is a nullable struct and converter is for non-nullable struct else normal conversion with converter. if (string.IsNullOrWhiteSpace(value) && converterInstance.TargetType != targetType && targetType.TryGetNullableUnderlyingType() is not null) { return(null); } return(converterInstance.Convert(value)); } // No conversion necessary if (targetType == typeof(object) || targetType == typeof(string)) { return(value); } // Bool conversion (special case) if (targetType == typeof(bool)) { return(string.IsNullOrWhiteSpace(value) || bool.Parse(value)); } // Char conversion (special case) if (targetType == typeof(char)) { return(TextUtils.UnescapeChar(value)); } // Primitive conversion if (!string.IsNullOrWhiteSpace(value) && PrimitiveConverter(targetType, value) is object v) { return(v); } // Enum conversion conversion if (targetType.IsEnum && !string.IsNullOrWhiteSpace(value)) { return(Enum.Parse(targetType, value ?? string.Empty, true)); } // Nullable<T> conversion Type?nullableUnderlyingType = targetType.TryGetNullableUnderlyingType(); if (nullableUnderlyingType is not null) { return(!string.IsNullOrWhiteSpace(value) ? ConvertScalar(argumentSchema, value, nullableUnderlyingType, converterInstance: null) // we can pass null because we have an extra check in the beginning of the method : null); } // String-constructible conversion ConstructorInfo?stringConstructor = targetType.GetConstructor(new[] { typeof(string) }); if (stringConstructor is not null) { return(stringConstructor.Invoke(new object?[] { value })); } // String-parsable (with format provider) conversion MethodInfo?parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true); if (parseMethodWithFormatProvider is not null) { return(parseMethodWithFormatProvider.Invoke(null, new object[] { value !, FormatProvider })); }