public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { string result; if (!arguments.Any()) { if (returnTypes.Contains(CommandResultType.Command)) { return(new CommandCommandResult(this)); } result = string.Empty; } else { var comResult = arguments.First().Execute(info, Enumerable.Empty <ICommand>(), new CommandResultType[] { CommandResultType.String }); result = ((StringCommandResult)comResult).Content; } var commandResults = XCommandSystem.FilterList(commands, result); if (commandResults.Skip(1).Any()) { throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall); } var argSubList = arguments.Skip(1).ToArray(); return(commandResults.First().Value.Execute(info, argSubList, returnTypes)); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { string result; if (arguments.Count == 0) { if (returnTypes.Contains(CommandResultType.Command)) { return(new CommandCommandResult(this)); } result = string.Empty; } else { var comResult = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String }); result = ((StringCommandResult)comResult).Content; } var commandResults = XCommandSystem.FilterList(commands, result).ToArray(); if (commandResults.Length > 1) { throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall); } if (commandResults.Length == 0) { throw new CommandException("No matching command", CommandExceptionReason.AmbiguousCall); } var argSubList = arguments.TrySegment(1); return(commandResults[0].Value.Execute(info, argSubList, returnTypes)); }
private static object ConvertParam(string value, Type targetType) { if (targetType == typeof(string)) { return(value); } if (targetType.IsEnum) { var enumVals = Enum.GetValues(targetType).Cast <Enum>(); var result = XCommandSystem.FilterList(enumVals.Select(x => new KeyValuePair <string, Enum>(x.ToString(), x)), value).Select(x => x.Value).FirstOrDefault(); if (result == null) { throw new CommandException($"Invalid parameter \"{value}\"", CommandExceptionReason.MissingParameter); } return(result); } if (targetType.IsConstructedGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable <>)) { targetType = targetType.GenericTypeArguments[0]; } return(Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture)); }