/// <inheritdoc /> public bool Execute(string command) { command = command.Trim(); PromptCommandAttribute[] cmds = null; try { // Parse arguments var cmdArgs = new List <CommandToken>(command.SplitCommandLine()); cmds = _commandCache.SearchCommands(cmdArgs).ToArray(); var cmd = cmds.SearchRightCommand(cmdArgs, null, out var args); if (cmd == null) { if (cmds.Length > 0) { throw new InvalidParameterException($"Wrong parameters for <{cmds.FirstOrDefault().Command}>"); } throw new InvalidPromptCommandException($"Command not found <{command}>"); } // Get command lock (_consoleHandler) { // Raise event OnCommandRequested?.Invoke(this, cmd, command); // Invoke var ret = cmd.Method.Invoke(cmd.Instance, args); if (ret is Task task) { task.Wait(); } } return(true); } catch (Exception e) { var msg = e.InnerException != null ? e.InnerException.Message : e.Message; _consoleHandler.WriteLine(msg, ConsoleOutputStyle.Error); #if DEBUG _consoleHandler.WriteLine(e.ToString(), ConsoleOutputStyle.Error); #endif PrintHelp(cmds); return(false); } }
/// <summary> /// Execute command /// </summary> /// <param name="command">Command</param> /// <returns>Return false if fail</returns> public bool Execute(string command) { command = command.Trim(); PromptCommandAttribute[] cmds = null; try { // Parse arguments var cmdArgs = new List <CommandToken>(); cmds = SearchCommands(command, cmdArgs).ToArray(); var cmd = SearchRightCommand(cmds, cmdArgs); if (cmd == null) { if (cmds.Length > 0) { throw (new Exception($"Wrong parameters for <{cmds.FirstOrDefault().Command}>")); } throw (new Exception($"Command not found <{command}>")); } // Get command lock (_consoleReader) lock (_consoleWriter) { // Raise event OnCommandRequested?.Invoke(this, cmd, command); // Invoke cmd.Method.Invoke(this, cmd.ConvertToArguments(cmdArgs.Skip(cmd.CommandLength).ToArray())); } return(true); } catch (Exception e) { string msg = e.InnerException != null ? e.InnerException.Message : e.Message; _consoleWriter.WriteLine(msg, ConsoleOutputStyle.Error); PrintHelp(cmds); return(false); } }