private Try <object?> Execute(object[] convertedArgs)
        {
            return(() => {
                var result = ExecutedMethodInfo.Invoke(ExecutedMethodInstance, convertedArgs);
                ArrayPools <object?> .Return(convertedArgs); // convertedArgs was previously requested in ConvertArgs()

                return result;
            });
        }
示例#2
0
        /// <summary>
        ///     Converts given string arguments to their typed version using passed parameter infos as a 'scheme'.
        /// </summary>
        /// <param name="command">Command, whose parameter infos serve as a 'scheme' for args.</param>
        /// <param name="args">Args to map onto passed parameter infos.</param>
        /// <param name="parameterConverter">
        ///     Parameter converter used to convert string representation
        ///     of a parameter into actual parameter.
        /// </param>
        /// <returns>
        ///     Array with converted arguments. It is requested from <see cref="ArrayPools{T}" />,
        ///     so do not forget to return it there when you're done with it.
        /// </returns>
        /// <exception cref="ArgumentException">Not enough arguments for passed parameter infos.</exception>
        internal static Try <object?[]> ConvertArgs(IConsoleCommand command,
                                                    string[] args,
                                                    IConsoleCommandParameterConverter parameterConverter)
        {
            object?[] Convert()
            {
                var parameterInfos = command.Parameters;

                if (args.Length > parameterInfos.Count)
                {
                    throw new ArgumentException($"Passed {args.Length} argument(s), but command '{command.Name}' " +
                                                $"has {parameterInfos.Count} command parameter(s).");
                }

                // Request array will be returned in ConsoleCommand.Execute()
                var convertedArgs = ArrayPools <object?> .Request(parameterInfos.Count);

                for (int index = 0; index < parameterInfos.Count; index++)
                {
                    var currentParamInfo = parameterInfos[index];
                    if (args.Length > index)
                    {
                        int currentIndex     = index;
                        var conversionResult = parameterConverter.Convert(currentParamInfo, args[currentIndex]);
                        conversionResult.Match(convertedArg => convertedArgs[currentIndex] = convertedArg,
                                               e => throw e);
                    }
                    else
                    {
                        if (currentParamInfo.Default.HasDefault)
                        {
                            convertedArgs[index] = currentParamInfo.Default.Value;
                        }
                        else
                        {
                            throw new ArgumentException($"Passed {args.Length} argument(s), but it was not enough for " +
                                                        $"a '{command.Name}' command with {parameterInfos.Count} parameters.");
                        }
                    }
                }

                return(convertedArgs);
            }

            return(() => Convert());
        }