Reworked RecrownedGTK tools.
This commit is contained in:
@@ -18,17 +18,9 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
{
|
||||
while (running)
|
||||
{
|
||||
output.WrappedOutput("\nAwaiting command.");
|
||||
output.WriteLine("Awaiting command.");
|
||||
string command = input.GetInput();
|
||||
try
|
||||
{
|
||||
Process(input, output, command);
|
||||
|
||||
}
|
||||
catch (ArgumentException ae)
|
||||
{
|
||||
output.WrappedOutput("Error: " + ae.Message);
|
||||
}
|
||||
Process(input, output, command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,11 +31,13 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
}
|
||||
string command = commandAndArguments;
|
||||
string[] arguments = null;
|
||||
|
||||
if (!ContainsCommand(command)) {
|
||||
userOutput.Write("Unable to find command. Type \"help\" for more information.");
|
||||
return;
|
||||
}
|
||||
if (commandAndArguments.Contains(" "))
|
||||
{
|
||||
command = command.Remove(command.IndexOf(' '));
|
||||
if (!ContainsCommand(command)) throw new ArgumentException("Command not found.");
|
||||
string[] argumentsSplit = commandAndArguments.Substring(command.Length + 1).Split(' ');
|
||||
List<string> argumentsList = new List<string>();
|
||||
|
||||
@@ -59,7 +53,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
i++;
|
||||
if (i >= argumentsSplit.Length)
|
||||
{
|
||||
throw new ArgumentException("Argument is missing terminating \'\"\'");
|
||||
userOutput.Write("Argument is missing terminating \'\"\'");
|
||||
}
|
||||
|
||||
} while (!argumentsSplit[i].Contains("\""));
|
||||
@@ -74,8 +68,9 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
}
|
||||
arguments = argumentsList.ToArray();
|
||||
}
|
||||
if (!ContainsCommand(command)) throw new ArgumentException("Command not found. Type \"help\" for a list of commands.");
|
||||
GetCommand(command).Run(userInput, userOutput, arguments);
|
||||
if (!GetCommand(command).Run(userInput, userOutput, arguments)) {
|
||||
userOutput.Write("Command {0} was not used correctly. Please refer to \"help\" for more information.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -4,14 +4,14 @@ namespace RecrownedGTK.Tools.CommandProcessor.Commands
|
||||
{
|
||||
public class ClearConsoleCommand : EngineCommand
|
||||
{
|
||||
public ClearConsoleCommand() : base("clear")
|
||||
public ClearConsoleCommand() : base("Clears the console.", "clear")
|
||||
{
|
||||
help = "Clears the console.";
|
||||
}
|
||||
|
||||
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
|
||||
public override bool Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
|
||||
{
|
||||
userOutput.ClearOutput();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,49 +6,49 @@ namespace RecrownedGTK.Tools.CommandProcessor.Commands
|
||||
{
|
||||
CommandEngine commandEngine;
|
||||
|
||||
public HelpCommand(CommandEngine commandEngine) : base("help")
|
||||
public HelpCommand(CommandEngine commandEngine) : base("help [command] [arg]", "help")
|
||||
{
|
||||
this.commandEngine = commandEngine;
|
||||
help = "help [command] [arg]";
|
||||
}
|
||||
|
||||
|
||||
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments)
|
||||
public override bool Run(IUserInput userInput, IUserOutput userOutput, string[] arguments)
|
||||
{
|
||||
if (arguments != null)
|
||||
{
|
||||
if (commandEngine.ContainsCommand(arguments[0]))
|
||||
{
|
||||
if (arguments.Length < 2) userOutput.WrappedOutput(commandEngine.GetCommand(arguments[0]).Help(null));
|
||||
if (arguments.Length < 2) userOutput.WriteLine(commandEngine.GetCommand(arguments[0]).Help(null));
|
||||
for (int i = 1; i < arguments.Length; i++)
|
||||
{
|
||||
userOutput.WrappedOutput(commandEngine.GetCommand(arguments[0]).Help(arguments[i]));
|
||||
userOutput.WriteLine(commandEngine.GetCommand(arguments[0]).Help(arguments[i]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(arguments[0] + " not a command. Type \"help\" for a list of commands.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
userOutput.WrappedOutput("Tools for RecrownedGTK library. Possible commands are as follows:\n");
|
||||
userOutput.WriteLine("Tools for RecrownedGTK library. Possible commands are as follows:\n");
|
||||
foreach (EngineCommand engineCommand in commandEngine.commands)
|
||||
{
|
||||
for (int i = 0; i < engineCommand.InvokeStrings.Length; i++)
|
||||
{
|
||||
userOutput.WrappedOutput(engineCommand.InvokeStrings[i]);
|
||||
userOutput.Write(engineCommand.InvokeStrings[i]);
|
||||
if (i + 1 < engineCommand.InvokeStrings.Length)
|
||||
{
|
||||
userOutput.WrappedOutput(", ");
|
||||
userOutput.Write(", ");
|
||||
}
|
||||
}
|
||||
userOutput.WrappedOutput(" : ");
|
||||
userOutput.WrappedOutput(engineCommand.Help().Replace("\n", "\n\t"));
|
||||
userOutput.WrappedOutput("\n");
|
||||
userOutput.Output("--------");
|
||||
userOutput.Write(": ");
|
||||
userOutput.Write(engineCommand.Help());
|
||||
userOutput.Write("\n");
|
||||
userOutput.WriteLine("--------");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,15 +4,15 @@
|
||||
{
|
||||
CommandEngine commandEngine;
|
||||
|
||||
public StopCommand(CommandEngine commandEngine) : base("quit", "stop", "q", "exit")
|
||||
public StopCommand(CommandEngine commandEngine) : base("Exits the tool.", "quit", "stop", "q", "exit")
|
||||
{
|
||||
this.commandEngine = commandEngine;
|
||||
help = "Exits the tool.";
|
||||
}
|
||||
|
||||
public override void Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
|
||||
public override bool Run(IUserInput userInput, IUserOutput userOutput, string[] arguments = null)
|
||||
{
|
||||
commandEngine.running = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
/// <summary>
|
||||
/// Arguments that this command should accept and take into account.
|
||||
/// </summary>
|
||||
protected EngineCommandArgument[] commandArguments;
|
||||
protected string help;
|
||||
protected EngineCommandArgument[] arguments;
|
||||
protected readonly string help;
|
||||
|
||||
public EngineCommand(string help, params string[] invokeStrings)
|
||||
{
|
||||
@@ -56,10 +56,13 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the command.
|
||||
/// Runs the engine command.
|
||||
/// </summary>
|
||||
/// <param name="arguments">arguments to be used. Each string should be the argument input. May be null.</param>
|
||||
public abstract void Run(IUserInput userInput = null, IUserOutput userOutput = null, params string[] arguments);
|
||||
/// <param name="userInput">The user input object.</param>
|
||||
/// <param name="userOutput">The user output object.</param>
|
||||
/// <param name="arguments">The arguments.</param>
|
||||
/// <returns>True if the command executed successfully, otherwise false to display a help message.</returns>
|
||||
public abstract bool Run(IUserInput userInput, IUserOutput userOutput, params string[] arguments);
|
||||
|
||||
/// <summary>
|
||||
/// Check if given argument is viable for command.
|
||||
@@ -78,11 +81,11 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
/// <returns>null if not viable or the <see cref="EngineCommandArgumentOf(argument)"/> if viable.</returns>
|
||||
public EngineCommandArgument EngineCommandArgumentOf(string argument)
|
||||
{
|
||||
for (int i = 0; i < commandArguments.Length; i++)
|
||||
for (int i = 0; i < arguments.Length; i++)
|
||||
{
|
||||
if (commandArguments[i].invokeString == argument)
|
||||
if (arguments[i].invokeString == argument)
|
||||
{
|
||||
return commandArguments[i];
|
||||
return arguments[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -147,7 +150,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
/// <returns>The string for the help.</returns>
|
||||
public string Help(string argument = null)
|
||||
{
|
||||
if (argument != null && commandArguments != null)
|
||||
if (argument != null && arguments != null)
|
||||
{
|
||||
if (Validate(argument))
|
||||
{
|
||||
@@ -165,25 +168,24 @@ namespace RecrownedGTK.Tools.CommandProcessor
|
||||
{
|
||||
StringBuilder helpBuilder = new StringBuilder();
|
||||
helpBuilder.Append(help);
|
||||
if (commandArguments != null)
|
||||
if (arguments != null)
|
||||
{
|
||||
helpBuilder.AppendLine();
|
||||
helpBuilder.Append("Possible arguments: ");
|
||||
for (int i = 0; i < commandArguments.Length; i++)
|
||||
for (int i = 0; i < arguments.Length; i++)
|
||||
{
|
||||
helpBuilder.Append(commandArguments[i].invokeString);
|
||||
if (commandArguments[i].required) helpBuilder.Append('*');
|
||||
if (i == commandArguments.Length - 2)
|
||||
helpBuilder.Append(arguments[i].invokeString);
|
||||
if (arguments[i].required) helpBuilder.Append('*');
|
||||
if (i == arguments.Length - 2)
|
||||
{
|
||||
helpBuilder.Append(", and ");
|
||||
}
|
||||
else if (i < commandArguments.Length - 2)
|
||||
else if (i < arguments.Length - 2)
|
||||
{
|
||||
helpBuilder.Append(", ");
|
||||
}
|
||||
}
|
||||
helpBuilder.Append('.');
|
||||
helpBuilder.AppendLine(" (* are required arguments.)");
|
||||
helpBuilder.Append(". (* are required arguments.)");
|
||||
}
|
||||
return helpBuilder.ToString();
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
namespace RecrownedGTK.Tools.CommandProcessor {
|
||||
public interface IUserOutput
|
||||
{
|
||||
void Output(string output);
|
||||
void WrappedOutput(string output);
|
||||
void WriteLine(string output);
|
||||
void Write(string output);
|
||||
void ClearOutput();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user