recrownedathenaeum/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs

180 lines
6.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
namespace RecrownedAthenaeum.Tools.CommandProcessor
{
public abstract class EngineCommand
{
2018-12-29 00:52:52 +00:00
bool caseSensitiveName = false;
/// <summary>
/// the words a user can type that will invoke this command.
/// </summary>
2018-12-29 00:52:52 +00:00
protected string[] invokeStrings;
public string[] InvokeStrings { get { return invokeStrings; } }
/// <summary>
/// Arguments that this command should accept and take into account.
/// </summary>
protected EngineCommandArgument[] arguments;
protected string help;
public EngineCommand(params string[] invokeStrings)
{
this.invokeStrings = invokeStrings ?? throw new ArgumentNullException();
}
/// <summary>
/// Whether or not this command should be invoked given the string.
/// </summary>
/// <param name="command">The string that acts as the command.</param>
/// <returns>whether or not this command is invoked.</returns>
public bool IsInvoked(string command)
{
2018-12-29 00:52:52 +00:00
if (!caseSensitiveName) command = command.ToLower();
for (int i = 0; i < invokeStrings.Length; i++)
{
2018-12-29 00:52:52 +00:00
if (!caseSensitiveName)
{
if (invokeStrings[i].ToLower() == command) return true;
2018-12-29 00:52:52 +00:00
}
else
{
if (invokeStrings[i] == command) return true;
}
}
return false;
}
/// <summary>
/// Runs the command.
/// </summary>
/// <param name="arguments">arguments to be used. May be null.</param>
public virtual void Run(string[] arguments = null)
{
}
/// <summary>
2018-12-29 00:52:52 +00:00
/// Check if given argument is viable for command.
/// </summary>
/// <param name="argument">Argument to check.</param>
/// <returns>True if valid.</returns>
public bool Validate(string argument)
{
return EngineCommandArgumentOf(argument) == null ? false : true;
}
/// <summary>
/// Returns the <see cref="EngineCommandArgument"/> of the given argument or null if the string is an invalid argument.
/// </summary>
/// <param name="argument">The argument string.</param>
/// <returns>null if not viable or the <see cref="EngineCommandArgumentOf(argument)"/> if viable.</returns>
public EngineCommandArgument EngineCommandArgumentOf(string argument)
{
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i].invokeString == argument)
{
return arguments[i];
}
}
return null;
}
/// <summary>
/// Finds the index of the argument given in an array of arguments for the command.
/// </summary>
/// <param name="argument">The argument to find the index of.</param>
/// <param name="arguments">The array containing all arguments.</param>
/// <returns>The index or throws argument exception if it doesn't exist.</returns>
public int IndexOfArgumentIn(string argument, string[] arguments)
{
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i] == argument)
{
return i;
}
}
throw new ArgumentException("Argument " + argument + " is missing. Type \"help\" for more information.");
}
public bool HasArgument(string argument, string[] arguments)
{
try
{
IndexOfArgumentIn(argument, arguments);
}
catch (ArgumentException)
{
return false;
}
return true;
}
public bool HasArgument(EngineCommandArgument argument, string[] arguments)
{
return HasArgument(argument.invokeString, arguments);
}
/// <summary>
/// Finds the index of the argument given in an array of arguments for the command.
/// </summary>
/// <param name="argument">The argument to find the index of.</param>
/// <param name="arguments">The array containing all arguments.</param>
/// <returns>The index or throws argument exception if it doesn't exist.</returns>
public int IndexOfArgumentIn(EngineCommandArgument argument, string[] arguments)
{
return IndexOfArgumentIn(argument.invokeString, arguments);
}
/// <summary>
/// Called when help "command trigger" [argument] is invoked. Argument is optional and therefore, may be null. If the valid arguments are properly registered, this command will take care of it. If no arguments are provided, it will return the general help defined for this command.
/// </summary>
2018-12-29 00:52:52 +00:00
/// <param name="argument">Potential argument to request help for.</param>
/// <returns>The string for the help.</returns>
public string Help(string argument = null)
{
2018-12-29 02:33:34 +00:00
if (argument != null && arguments != null)
2018-12-29 00:52:52 +00:00
{
if (Validate(argument))
{
return EngineCommandArgumentOf(argument).help;
}
else
{
return "The argument " + argument + " does not exist. Type \"help " + invokeStrings[0] + "\" (or any of its aliases) for a list of arguments.";
}
}
2018-12-29 02:33:34 +00:00
else
2018-12-29 00:52:52 +00:00
{
2018-12-29 02:33:34 +00:00
StringBuilder helpBuilder = new StringBuilder();
helpBuilder.Append(help);
if (arguments != null)
2018-12-29 00:52:52 +00:00
{
2018-12-29 02:33:34 +00:00
helpBuilder.AppendLine();
helpBuilder.Append("Possible arguments are: ");
for (int i = 0; i < arguments.Length; i++)
{
helpBuilder.Append(arguments[i].invokeString);
if (i == arguments.Length - 2)
{
helpBuilder.Append(", and ");
}
else if (i < arguments.Length - 2)
{
helpBuilder.Append(", ");
}
}
helpBuilder.Append('.');
2018-12-29 00:52:52 +00:00
}
2018-12-29 02:33:34 +00:00
return helpBuilder.ToString();
2018-12-29 00:52:52 +00:00
}
2018-12-29 02:33:34 +00:00
}
}
}