public RunResult Run(string[] args) { if (args == null) { args = new string[0]; } Queue <string> arguments = new Queue <string>(args); if (_optionSet != null) { arguments = new Queue <string>(_optionSet.Parse(args)); } try { if (CommandRecords.Count == 0) { throw new NoCommandsDefinedException(); } if (arguments.Count == 0) { _helpGenerator.ShowCommandUsage(_availableCommands, _optionSet); return(new RunResult()); } var commandName = arguments.Dequeue(); var commandSelector = new CommandSelector(); var command = commandSelector.Select(commandName, CommandRecords); EnsureCommandActions(command); var actionName = arguments.Count > 0 ? arguments.Dequeue() : null; var availableActions = command.CommandActions; if (actionName == null) { _helpGenerator.ShowCommandHelp(command.Command, availableActions.ToList()); return(new RunResult()); } var actionSelector = new ActionSelector(); var action = actionSelector.Select(actionName, command.Command, availableActions); var parser = new CommandLineParser(); CommandLineParseResult parseResult = parser.Parse(action, arguments.ToArray()); return(parseResult.CommandAction.Run(_resolver, parseResult)); } catch (CommandParseExceptionBase exception) { exception.Render(); } catch (TargetInvocationException exception) { Exception innerException = exception.InnerException; if (innerException == null) { throw; } if (innerException is CommandParseExceptionBase) { ((CommandParseExceptionBase)exception.InnerException).Render(); return(new RunResult()); } throw new CommandInvocationException("Error executing command", innerException); } return(new RunResult()); }
public async Task <RunResult> RunViaRouteAsync(RouteQuery routeQuery) { try { if (CommandRecords.Count == 0) { throw new NoCommandsDefinedException(); } string route = routeQuery.Route; string method = routeQuery.Method; method = method.ToUpper(); route = route.ToLower(); var query = from item in CommandActionRecords where item.Route == route && item.CommandAction.Method == method select item; var actionRecord = query.FirstOrDefault(); if (actionRecord == null) { return(new RunResult() { ErrorCode = 404 }); } var action = actionRecord.CommandAction; var parser = new CommandLineParser(); var clp = new List <CommandLineParameter> { new CommandLineParameter("body", routeQuery.Body) }; CommandLineParseResult parseResult = new CommandLineParseResult(action, clp, new List <string>().ToArray()); return(await parseResult.CommandAction.RunAsync(_resolver, parseResult)); } catch (CommandParseExceptionBase exception) { exception.Render(); } catch (TargetInvocationException exception) { Exception innerException = exception.InnerException; if (innerException == null) { throw; } if (innerException is CommandParseExceptionBase) { ((CommandParseExceptionBase)exception.InnerException).Render(); return(new RunResult()); } throw new CommandInvocationException("Error executing command", innerException); } return(new RunResult() { ErrorCode = 404 }); }