Added basic help and quit functions to tools.

This commit is contained in:
2020-05-26 21:06:31 -05:00
parent d149e12433
commit 2d9f7617aa
8 changed files with 181 additions and 25 deletions

View File

@@ -0,0 +1,56 @@
using System;
using System.Text;
using SlatedGameToolkit.Tools.System;
using SlatedGameToolkit.Tools.System.Interaction;
namespace SlatedGameToolkit.Tools.Commands
{
public class HelpCommand : IInvocable
{
private readonly string[] invokers = new string[] {"help"};
private CommandMap commandMap;
public HelpCommand(CommandMap commandMap) {
this.commandMap = commandMap;
}
public bool Execute(IInteractable interactable, string[] args)
{
if (args.Length > 0) {
IInvocable invocable = commandMap[args[0]];
if (invocable == null) interactable.Tell("Unable to find command {0}. Please type \"help\" for more a list of commands.");
StringBuilder builder = new StringBuilder();
builder.AppendJoin(", ", invocable.GetInvokers());
interactable.Tell("Possible aliases: " + builder.ToString() + ": ");
interactable.Tell("Description: " + invocable.getDescription());
interactable.Tell("Usage: " + invocable.getUsage(args.Length > 0 ? args[0] : null));
} else {
interactable.Tell("--- Help ---");
foreach (IInvocable invocable in commandMap)
{
interactable.Separate();
StringBuilder builder = new StringBuilder();
builder.AppendJoin(", ", invocable.GetInvokers());
interactable.Tell(builder.ToString() + ": ");
interactable.Tell(invocable.getDescription());
}
}
return true;
}
public string getDescription()
{
return "Displays the help message. You're looking at it.";
}
public string getUsage(string arg)
{
return "Usage: \"help [command]\" where [command] is a specific command you want to see usage for.";
}
public string[] GetInvokers()
{
return invokers;
}
}
}