/// <summary>Parse a command string and invoke it if valid.</summary> /// <param name="input">The command to run, including the command name and any arguments.</param> /// <param name="monitor">Encapsulates monitoring and logging.</param> public static void CallCommand(string input, IMonitor monitor) { // normalise input input = input?.Trim(); if (string.IsNullOrWhiteSpace(input)) { return; } // tokenise input string[] args = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string commandName = args[0]; args = args.Skip(1).ToArray(); // get command Command command = Command.FindCommand(commandName); if (command == null) { monitor.Log("Unknown command", LogLevel.Error); return; } // fire command command.CalledArgs = args; command.Fire(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void help_CommandFired(Command cmd) { if (cmd.CalledArgs.Length > 0) { Command fnd = Command.FindCommand(cmd.CalledArgs[0]); if (fnd == null) { LogError("The command specified could not be found"); } else { if (fnd.CommandArgs.Length > 0) { LogInfo("{0}: {1} - {2}", fnd.CommandName, fnd.CommandDesc, fnd.CommandArgs.ToSingular()); } else { LogInfo("{0}: {1}", fnd.CommandName, fnd.CommandDesc); } } } else { LogInfo("Commands: " + Command.RegisteredCommands.Select(x => x.CommandName).ToSingular()); } }
private static void help_CommandFired(object o, EventArgsCommand e) { if (e.Command.CalledArgs.Length > 0) { var fnd = Command.FindCommand(e.Command.CalledArgs[0]); if (fnd == null) { Log.AsyncR("The command specified could not be found"); } else { if (fnd.CommandArgs.Length > 0) { Log.AsyncY($"{fnd.CommandName}: {fnd.CommandDesc} - {fnd.CommandArgs.ToSingular()}"); } else { Log.AsyncY($"{fnd.CommandName}: {fnd.CommandDesc}"); } } } else { Log.AsyncY("Commands: " + Command.RegisteredCommands.Select(x => x.CommandName).ToSingular()); } }