public ICommandExecuteResult Execute(IConsoleCommandParameterConverter argConverter, params string[] args)
        {
            var result = from convertedArgs in CommandParameterInfo.ConvertArgs(this, args, argConverter)
                         from returned in Execute(convertedArgs)
                         select returned;

            return(result.Try().Match <ICommandExecuteResult>(
                       returned => Result.HasValue
                    ? new ConsoleCommandExecuteSuccess(returned)
                    : ConsoleCommandExecuteSuccess.Void,
                       e => new ConsoleCommandExecuteFailure(e)));
        }
Beispiel #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());
        }
Beispiel #3
0
 public void OneTimeSetUp()
 {
     _commandProvider = new TestCommandProvider();
     _commands        = ConsoleCommands.GatherFromInstance(_commandProvider);
     _converter       = ConsoleCommandParameterConverter.Default;
 }
Beispiel #4
0
 public ConsoleCommands(IConsoleCommandParser commandParser,
                        IConsoleCommandParameterConverter parameterConverter)
 {
     _parser             = commandParser ?? throw new ArgumentNullException(nameof(commandParser));
     _parameterConverter = parameterConverter ?? throw new ArgumentNullException(nameof(parameterConverter));
 }