Пример #1
0
 public bool CanRunCommand(RunCommandResult result)
 {
     if (result.InvalidOptions.Any())
     {
         return(false);
     }
     if (result.InvalidArguments.Any())
     {
         return(false);
     }
     if (result.NonOptionalUnknownParameters.Any())
     {
         return(false);
     }
     return(true);
 }
Пример #2
0
        public RunCommandResult RunCommand(Command command, Dictionary <string, string> options, Dictionary <string, string> arguments)
        {
            var result = new RunCommandResult();

            _currentCommand         = command;
            _currentOptions         = options ?? new Dictionary <string, string>();
            _currentArguments       = arguments ?? new Dictionary <string, string>();
            _currentParameterValues = new List <object>();

            foreach (var parameterInfo in command.CommandMethodInfo.GetParameters())
            {
                if (HandleSpecialParameters(parameterInfo))
                {
                    continue;
                }

                var evaluateParameterResult = EvaluateParameter(parameterInfo);
                if (evaluateParameterResult.RawValue.HasValue)
                {
                    // Has value:
                    var rawValue  = (KeyValuePair <string, string>)evaluateParameterResult.RawValue;
                    var converter = TypeDescriptor.GetConverter(parameterInfo.ParameterType);
                    var isValid   = converter.IsValid(rawValue.Value);
                    if (isValid)
                    {
                        var typedValue = converter.ConvertTo(rawValue.Value, parameterInfo.ParameterType);
                        _currentParameterValues.Add(typedValue);
                    }
                    else
                    {
                        // Value is not convertable to parameter type:
                        switch (evaluateParameterResult.Kind)
                        {
                        case CommandParameterKind.Option:
                            result.InvalidOptions.Add(rawValue.Key, rawValue.Value);
                            break;

                        case CommandParameterKind.Argument:
                            result.InvalidArguments.Add(rawValue.Key, rawValue.Value);
                            break;

                        case CommandParameterKind.Unknown:
                            // Should not happen: If Kind is Unknown then there should not be a value. Just in case:
                            throw new NotImplementedException($"Internal Error: {nameof(evaluateParameterResult.Kind)} is {evaluateParameterResult.Kind} but {nameof(evaluateParameterResult.RawValue.Value)} is not null!");
                        }
                    }
                }
                else
                {
                    // Does not have value:
                    if (evaluateParameterResult.IsOptional)
                    {
                        // Is Optional so it does not need a value.
                        _currentParameterValues.Add(Type.Missing);
                    }
                    else
                    {
                        if (evaluateParameterResult.Kind == CommandParameterKind.Unknown)
                        {
                            // Non-optional Unknown parameter, not marked as Option or Argument.
                            result.NonOptionalUnknownParameters.Add(evaluateParameterResult.ParameterInfo);
                        }
                        else
                        {
                            // Should not happen: If Kind is NOT Unknown then there has to be a value. Just in case:
                            throw new NotImplementedException($"Internal Error: {nameof(evaluateParameterResult.Kind)} is {evaluateParameterResult.Kind} and {nameof(evaluateParameterResult.RawValue.Value)} is null!");
                        }
                    }
                }
            }

            if (CanRunCommand(result))
            {
                RunCommand();
                result.Success = true;
            }
            else
            {
                result.Success = false;
            }

            return(result);
        }