using System.Linq;
namespace StardewModdingAPI.Framework.Commands
{
/// The 'help' SMAPI console command.
internal class HelpCommand : IInternalCommand
{
/*********
** Fields
*********/
/// Manages console commands.
private readonly CommandManager CommandManager;
/*********
** Accessors
*********/
/// The command name, which the user must type to trigger it.
public string Name { get; } = "help";
/// The human-readable documentation shown when the player runs the built-in 'help' command.
public string Description { get; } = "Lists command documentation.\n\nUsage: help\nLists all available commands.\n\nUsage: help \n- cmd: The name of a command whose documentation to display.";
/*********
** Public methods
*********/
/// Construct an instance.
/// Manages console commands.
public HelpCommand(CommandManager commandManager)
{
this.CommandManager = commandManager;
}
/// Handle the console command when it's entered by the user.
/// The command arguments.
/// Writes messages to the console.
public void HandleCommand(string[] args, IMonitor monitor)
{
if (args.Any())
{
Command result = this.CommandManager.Get(args[0]);
if (result == null)
monitor.Log("There's no command with that name.", LogLevel.Error);
else
monitor.Log($"{result.Name}: {result.Documentation}{(result.Mod != null ? $"\n(Added by {result.Mod.DisplayName}.)" : "")}", LogLevel.Info);
}
else
{
string message = "The following commands are registered:\n";
IGrouping[] groups = (from command in this.CommandManager.GetAll() orderby command.Mod?.DisplayName, command.Name group command.Name by command.Mod?.DisplayName).ToArray();
foreach (var group in groups)
{
string modName = group.Key ?? "SMAPI";
string[] commandNames = group.ToArray();
message += $"{modName}:\n {string.Join("\n ", commandNames)}\n\n";
}
message += "For more information about a command, type 'help command_name'.";
monitor.Log(message, LogLevel.Info);
}
}
}
}