/// <summary> /// Class constructor. /// </summary> /// <param name="userCmd">User entered command string.</param> public SMCommandHelper(string UserID, string userCmd) { this.UserID = UserID; // trims whitespace or leading / char[] trimFromStart = { '/' }; this.userCmd = Utils.CleanString(userCmd, trimFromStart); this.cmdName = this.GetCommandName(this.userCmd); this.cmd = this.GetCommandByName(this.cmdName); }
/// <summary> /// Shows help for a given command. /// </summary> /// <param name="command"></param> private void ShowCommandHelp(string command) { string output = ""; SMCommand cmd = this.GetCommand(command); if (cmd == null) { this.CommandNotFound(); return; } output += this.GetHelpStringForCommand(cmd, command); this.OutputHelp(output); }
/// <summary> /// Gets the SMCommand object for a given comand name (checking command aliases). /// </summary> /// <param name="cmdName">The command name to search for.</param> /// <returns>SMCommand or null.</returns> private SMCommand GetCommandByName(string cmdName) { List <SMCommand> commands = (List <SMCommand>)HttpContext.Current.Application["SMCommands"]; SMCommand command = commands.FirstOrDefault(cmd => { List <string> names = cmd.CommandName.Split(',').ToList(); if (names.FirstOrDefault(name => name.Trim().ToLower() == cmdName.ToLower()) != null) { return(true); } return(false); }); return(command); }
/// <summary> /// Builds the help string for a given command. /// </summary> /// <param name="command">SMCOmmand to get help string for.</param> /// <returns>The help string to display to the user.</returns> private string GetHelpStringForCommand(SMCommand command, string commandName) { string helpString = ""; List <string> aliases = this.GetCmdAliasList(command); foreach (string alias in aliases) { if (alias.ToLower() == commandName.ToLower()) { Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("name", alias); helpString += this.outputFormatter.Bold(alias, 0); helpString += this.outputFormatter.General($": {new TagReplacer(command.CommandDescription).Replace(data)}"); helpString += this.outputFormatter.General(""); helpString += this.outputFormatter.ListItem( this.outputFormatter.General("Command Syntax: ", 0) + this.outputFormatter.Italic(new TagReplacer(command.CommandSyntax).Replace(data), 0) ); helpString += this.outputFormatter.ListItem( this.outputFormatter.General("Example: ", 0) + this.outputFormatter.Italic(new TagReplacer(command.ExampleUsage).Replace(data), 0) ); if (command.RequiredSkill != null) { helpString += this.outputFormatter.ListItem( this.outputFormatter.General("Required Skill: ", 0) + this.outputFormatter.Italic(command.RequiredSkill.Replace("Skill.", ""), 0) ); } } } return(helpString); }
/// <summary> /// Gets a list of aliases for a given SMCommand. /// </summary> /// <param name="cmd">The SMCommand.</param> /// <returns>The list of alias names.</returns> private List <string> GetCmdAliasList(SMCommand cmd) { return(cmd.CommandName.Split(',').Select(name => name.Trim()).ToList()); }