public void Interpret(ConsoleCommandEventArgs eventArgs) { // Abort if eventArgs does not contain useful arguments. The first parameter must be a // command name. if (eventArgs == null) { return; } var args = eventArgs.Args; if (args == null || args.Length == 0) { return; } try { string commandName = args[0].ToLowerInvariant(); if (commandName == "help") { // ----- Help command. // If the previous command handlers have handled this command then we write // a newline to create some space between the help texts. if (eventArgs.Handled) { Console.WriteLine(); } if (args.Length == 1) { WriteHelp(); // Write general help. } else if (!eventArgs.Handled) { WriteCommandHelp(args[1]); // help <command> --> Write specific help. } return; } // Abort if command (other than "help") was already handled. if (eventArgs.Handled) { return; } ConsoleCommand command; bool found = Commands.TryGet(commandName, out command); if (!found) { throw new ConsoleCommandException(ConsoleCommandException.ErrorInvalidCommand, args[0]); } if (command.Execute != null) { command.Execute(args); } } catch (Exception exception) { // We catch all exceptions and print them. Console.WriteLine(exception.Message); } }
private void OnCommandEntered(ConsoleCommandEventArgs eventArgs) { var handler = CommandEntered; try { if (handler != null) handler(this, eventArgs); } catch (Exception exception) { // We catch all exceptions and print them. WriteLine(exception.Message); } // At last call default interpreter. Interpreter.Interpret(eventArgs); }
public void Interpret(ConsoleCommandEventArgs eventArgs) { // Abort if eventArgs does not contain useful arguments. The first parameter must be a // command name. if (eventArgs == null) return; var args = eventArgs.Args; if (args == null || args.Length == 0) return; try { string commandName = args[0].ToLowerInvariant(); if (commandName == "help") { // ----- Help command. // If the previous command handlers have handled this command then we write // a newline to create some space between the help texts. if (eventArgs.Handled) Console.WriteLine(); if (args.Length == 1) WriteHelp(); // Write general help. else if (!eventArgs.Handled) WriteCommandHelp(args[1]); // help <command> --> Write specific help. return; } // Abort if command (other than "help") was already handled. if (eventArgs.Handled) return; ConsoleCommand command; bool found = Commands.TryGet(commandName, out command); if (!found) throw new ConsoleCommandException(ConsoleCommandException.ErrorInvalidCommand, args[0]); if (command.Execute != null) command.Execute(args); } catch (Exception exception) { // We catch all exceptions and print them. Console.WriteLine(exception.Message); } }