Beispiel #1
0
        private static bool TryBindParameter(ParameterInfo parameter, CommandLineArguments arguments, out object value)
        {
            var expectedType = parameter.ParameterType;

            if (expectedType == typeof(CommandLineArguments))
            {
                value = new CommandLineArguments(arguments);
            }
            else if (arguments.TryGetValue(parameter.Name, out value) || arguments.TryGetValue((parameter.Position).ToString(), out value))
            {
                if (expectedType.IsArray)
                {
                    var elemType = expectedType.GetElementType();
                    Debug.Assert(elemType != null, "elemType != null");
                    if (value is string[])
                    {
                        var oldArr = value as string[];
                        var newArr = Array.CreateInstance(elemType, oldArr.Length);
                        for (var v = 0; v < oldArr.Length; v++)
                        {
                            newArr.SetValue(TypeConvert.Convert(typeof(string), elemType, oldArr[v]), v);
                        }

                        value = newArr;
                    }
                    else if (value != null)
                    {
                        var newArr = Array.CreateInstance(elemType, 1);
                        newArr.SetValue(TypeConvert.Convert(value.GetType(), elemType, value), 0);
                        value = newArr;
                    }
                    else
                    {
                        var newArr = Array.CreateInstance(elemType, 0);
                        value = newArr;
                    }
                }
                else if (expectedType == typeof(bool) && value == null)
                {
                    value = true;
                }
                else if (value == null)
                {
                    value = parameter.IsOptional ? parameter.DefaultValue : expectedType.IsValueType ? TypeActivator.CreateInstance(expectedType) : null;
                }
                else if (expectedType.IsEnum && value is string[])
                {
                    var valuesStr = (string[])value;
                    var values    = Array.ConvertAll(valuesStr, v => TypeConvert.Convert(typeof(string), expectedType, v));
                    if (IsSigned(Enum.GetUnderlyingType(expectedType)))
                    {
                        var combinedValue = 0L;
                        foreach (var v in values)
                        {
                            combinedValue |= (long)TypeConvert.Convert(expectedType, typeof(long), v);
                        }

                        value = Enum.ToObject(expectedType, combinedValue);
                    }
                    else
                    {
                        var combinedValue = 0UL;
                        foreach (var v in values)
                        {
                            combinedValue |= (ulong)TypeConvert.Convert(expectedType, typeof(ulong), v);
                        }

                        value = Enum.ToObject(expectedType, combinedValue);
                    }
                }
                else
                {
                    value = TypeConvert.Convert(value.GetType(), expectedType, value);
                }
            }
            else if (parameter.IsOptional)
            {
                value = parameter.DefaultValue;
            }
            else if (expectedType == typeof(bool))
            {
                value = false;
            }
            else
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
 public static object GetDefault(this Type t)
 {
     return(TypeConvert.GetDefault(t));
 }
        internal static CommandLineException InvalidCommandParameters(MethodInfo commandMethod, ParameterBindResult[] parameterBindResults)
        {
            if (commandMethod == null)
            {
                throw new ArgumentNullException("commandMethod");
            }
            if (parameterBindResults == null)
            {
                throw new ArgumentNullException("parameterBindResults");
            }

            var bindingErrors = new Dictionary <string, Exception>();
            var builder       = new StringBuilder();

            builder.AppendFormat("Invalid parameters for command {0}(", commandMethod.Name);
            foreach (var parameterBindResult in parameterBindResults)
            {
                if (parameterBindResult.Parameter.IsOptional)
                {
                    builder.Append("[");
                }
                builder.Append(parameterBindResult.Parameter.Name);
                if (parameterBindResult.Parameter.IsOptional)
                {
                    builder.Append("]");
                }
                var isLast = parameterBindResult == parameterBindResults[parameterBindResults.Length - 1];
                if (isLast)
                {
                    builder.Append(")");
                }
                else
                {
                    builder.Append(", ");
                }
            }
            builder.AppendLine(":");
            foreach (var parameterBindResult in parameterBindResults)
            {
                builder.Append("\t").Append(parameterBindResult.Parameter.Name).Append(": ");
                if (parameterBindResult.IsSuccess)
                {
                    var value = TypeConvert.ToString(parameterBindResult.Value) ?? "<null>";
                    if (value.Length > 32)
                    {
                        builder.Append(value.Substring(0, 32)).AppendLine("...");
                    }
                    else
                    {
                        builder.AppendLine(value);
                    }
                }
                else
                {
                    bindingErrors[parameterBindResult.Parameter.Name] = parameterBindResult.Error;

                    var errorMessage  = parameterBindResult.Error.Message;
                    var parameterType = parameterBindResult.Parameter.ParameterType;
                    parameterType = Nullable.GetUnderlyingType(parameterType) ?? parameterType;
                    builder.Append("(").Append(parameterType.Name).Append(") ");
                    if (parameterBindResult.Error is FormatException && parameterBindResult.Value != null)
                    {
                        var value = TypeConvert.ToString(parameterBindResult.Value) ?? "<null>";
                        builder.Append(errorMessage)
                        .Append(" Value: '");
                        if (value.Length > 32)
                        {
                            builder.Append(value.Substring(0, 32)).Append("...");
                        }
                        else
                        {
                            builder.Append(value);
                        }
                        builder.AppendLine("'.");
                    }
                    else
                    {
                        builder.AppendLine(errorMessage);
                    }
                }
            }

            var error = new CommandLineException(builder.ToString());

            error.Data["method"] = commandMethod.Name;
#if !NETSTANDARD1_3
            error.Data["methodToken"] = commandMethod.MetadataToken;
#endif
            error.Data["bindingErrors"] = bindingErrors;
            return(error);
        }