From 960bf76802296835d97c79fbd807f2625a7dab3c Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Fri, 28 Dec 2018 18:52:52 -0600 Subject: [PATCH] improved command argument system. --- .../Commands/ClearConsoleCommand.cs | 7 +- .../CommandProcessor/Commands/HelpCommand.cs | 12 +- .../CommandProcessor/Commands/StopCommand.cs | 6 +- .../CommandProcessor/Commands/TestCommand.cs | 39 ----- .../CommandProcessor/EngineCommand.cs | 135 ++++++++++++++++-- .../CommandProcessor/EngineCommandArgument.cs | 18 +++ .../TextureAtlasTools/TexturePackerCommand.cs | 125 ++++++---------- RecrownedAthenaeum.ConsoleTools/Tools.cs | 1 - 8 files changed, 191 insertions(+), 152 deletions(-) delete mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/TestCommand.cs create mode 100644 RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommandArgument.cs diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/ClearConsoleCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/ClearConsoleCommand.cs index 7bed555..b72ee2b 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/ClearConsoleCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/ClearConsoleCommand.cs @@ -8,12 +8,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor.Commands { public ClearConsoleCommand() : base("clear") { - - } - - public override string Help(string argument = null) - { - return "Clears the console."; + help = "Clears the console."; } public override void Run(string[] arguments = null) diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/HelpCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/HelpCommand.cs index a4c01d6..e20d6b0 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/HelpCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/HelpCommand.cs @@ -11,11 +11,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor.Commands public HelpCommand(CommandEngine commandEngine) : base("help") { this.commandEngine = commandEngine; - } - - public override string Help(string argument) - { - return "help [command] [arg]"; + help = "help [command] [arg]"; } @@ -41,10 +37,10 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor.Commands ConsoleUtilities.WriteWrappedLine("Tools for RecrownedAthenaeum library. Possible commands are as follows:\n"); foreach (EngineCommand engineCommand in commandEngine.commands) { - for (int i = 0; i < engineCommand.invokeStrings.Length; i++) + for (int i = 0; i < engineCommand.InvokeStrings.Length; i++) { - ConsoleUtilities.WriteWrapped(engineCommand.invokeStrings[i]); - if (i + 1 < engineCommand.invokeStrings.Length) + ConsoleUtilities.WriteWrapped(engineCommand.InvokeStrings[i]); + if (i + 1 < engineCommand.InvokeStrings.Length) { ConsoleUtilities.WriteWrapped(", "); } diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/StopCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/StopCommand.cs index 5e587a0..f2242af 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/StopCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/StopCommand.cs @@ -11,11 +11,7 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor.Commands public StopCommand(CommandEngine commandEngine) : base("quit", "stop", "q", "exit") { this.commandEngine = commandEngine; - } - - public override string Help(string argument = null) - { - return "Exits the tool."; + help = "Exits the tool."; } public override void Run(string[] arguments = null) diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/TestCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/TestCommand.cs deleted file mode 100644 index 9c9fc9e..0000000 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/Commands/TestCommand.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace RecrownedAthenaeum.Tools.CommandProcessor.Commands -{ - class TestCommand : EngineCommand - { - public TestCommand() : base("test") - { - - } - - public override string Help(string argument = null) - { - switch (argument) - { - case null: - return "Test command. Meant for testing random stuff. Current arguments: \"-textWrap\".\n" + - "Its ironic that the help section is also meant to test things."; - case "-textWrap": - return "-textWrap prints a large amount of text to test the text wrap."; - } - return "Test command."; - } - - public override void Run(string[] arguments = null) - { - if (arguments != null && arguments[0] == "-textWrap") - { - ConsoleUtilities.WriteWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit.[here]\n Duis semper lacinia nisl, eget efficitur massa feugiat vel. Ut egestas elit id sollicitudin pellentesque.[here no space after break.]\nFusce ullamcorper nec turpis at aliquam. Donec cursus mi nec porttitor convallis. Nullam condimentum sollicitudin volutpat. Pellentesque tellus ligula, eleifend sit amet ante ac, accumsan volutpat lorem. Suspendisse potenti. Vestibulum at sodales ipsum. Mauris dignissim maximus purus sagittis elementum.", true); - ConsoleUtilities.WriteWrapped("test"); - ConsoleUtilities.WriteWrapped(". "); - ConsoleUtilities.WriteWrapped("test."); - } - base.Run(arguments); - } - } -} diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs index d38259f..b24cf89 100644 --- a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommand.cs @@ -6,12 +6,20 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor { public abstract class EngineCommand { - bool CaseSensitiveName = false; + + bool caseSensitiveName = false; /// /// the words a user can type that will invoke this command. /// - public string[] invokeStrings; + protected string[] invokeStrings; + public string[] InvokeStrings { get { return invokeStrings; } } + + /// + /// Arguments that this command should accept and take into account. + /// + protected EngineCommandArgument[] arguments; + protected string help; public EngineCommand(params string[] invokeStrings) { @@ -25,13 +33,14 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor /// whether or not this command is invoked. public bool IsInvoked(string command) { - if (!CaseSensitiveName) command = command.ToLower(); + if (!caseSensitiveName) command = command.ToLower(); for (int i = 0; i < invokeStrings.Length; i++) { - if (!CaseSensitiveName) + if (!caseSensitiveName) { if (invokeStrings[i].ToLower() == command) return true; - } else + } + else { if (invokeStrings[i] == command) return true; } @@ -49,16 +58,116 @@ namespace RecrownedAthenaeum.Tools.CommandProcessor } /// - /// Returns the help for the given argument for this command. If no argument is defined, general help for the command should be given. - /// If no argument is given (null), then returns overall help statement. - /// Overall help should list possible arguments for user to use "help [command] [arg]" format. - /// Arguments can then be separately returned from help as the help command will pass in the argument the user requests to see in detail in the parameter. + /// Check if given argument is viable for command. /// - /// The argument the help string is for. Can be null for overall command help. - /// The text to help understand the argument or the overall command. - public virtual string Help(string argument = null) + /// Argument to check. + /// True if valid. + public bool Validate(string argument) { - return "This command doesn't define a help string. Shame on it."; + return EngineCommandArgumentOf(argument) == null ? false : true; + } + + /// + /// Returns the of the given argument or null if the string is an invalid argument. + /// + /// The argument string. + /// null if not viable or the if viable. + public EngineCommandArgument EngineCommandArgumentOf(string argument) + { + for (int i = 0; i < arguments.Length; i++) + { + if (arguments[i].invokeString == argument) + { + return arguments[i]; + } + } + return null; + } + + /// + /// Finds the index of the argument given in an array of arguments for the command. + /// + /// The argument to find the index of. + /// The array containing all arguments. + /// The index or throws argument exception if it doesn't exist. + 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); + } + + /// + /// Finds the index of the argument given in an array of arguments for the command. + /// + /// The argument to find the index of. + /// The array containing all arguments. + /// The index or throws argument exception if it doesn't exist. + public int IndexOfArgumentIn(EngineCommandArgument argument, string[] arguments) + { + return IndexOfArgumentIn(argument.invokeString, arguments); + } + + /// + /// 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. + /// + /// Potential argument to request help for. + /// The string for the help. + public string Help(string argument = null) + { + if (arguments != null) + { + 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."; + } + } + + StringBuilder helpBuilder = new StringBuilder(); + helpBuilder.Append(help); + helpBuilder.AppendLine(); + helpBuilder.Append("Possible arguments are: "); + for (int i = 0; i < arguments.Length; i++) + { + helpBuilder.Append(arguments[i].invokeString); + if (i + 2 > arguments.Length) + { + helpBuilder.Append(", and "); + } + else + { + helpBuilder.Append(", "); + } + } + helpBuilder.Append('.'); + return helpBuilder.ToString(); } } } diff --git a/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommandArgument.cs b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommandArgument.cs new file mode 100644 index 0000000..e55c45c --- /dev/null +++ b/RecrownedAthenaeum.ConsoleTools/CommandProcessor/EngineCommandArgument.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace RecrownedAthenaeum.Tools.CommandProcessor +{ + public class EngineCommandArgument + { + public string invokeString; + public string help; + + public EngineCommandArgument(string invokeString, string help) + { + this.invokeString = invokeString; + this.help = help; + } + } +} diff --git a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs index 44d88a8..45313bd 100644 --- a/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs +++ b/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePackerCommand.cs @@ -10,33 +10,23 @@ namespace RecrownedAthenaeum.Tools.TextureAtlasTools class TexturePackerCommand : EngineCommand { - public TexturePackerCommand() : base("texturepacker") { } - - public override string Help(string argument) + public TexturePackerCommand() : base("texturepacker") { - switch (argument) - { - case null: - return "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties. Possible arguments are \"-i\", \"-o\", \"-mp\", \"-dau\", and \"-9p\". Refer to \"help\" for more info."; - case "-i": - return "-i : Path for input directory containing the textures. Required."; - case "-o": - return "-o : Path for output files. Points to non-existent file. Will create texture and definitions file with name. Required."; - case "-9p": - return "-9p : Can be used multiple times for defining a 9patch. This parameter requires a name, left patch, right patch, top patch, and bottom patch in the format name,a,b,c,d. Optional."; - case "-sp": - return "-sp : starting power for one side of the texture. Default is 8."; - case "-mp": - return "-mp : Maximum power for one side of the texture. Default is 8."; - case "-dau": - return "-dau : disables automatically upscaling the texture."; - default: - return argument + " is not a valid argument. Type \"help texturepacker to see general help and list of arguments.\""; - } + arguments = new EngineCommandArgument[6] { + new EngineCommandArgument("-i", "for input directory containing the textures. Required."), + new EngineCommandArgument("-o", "Path for output files. Points to non-existent file. Will create texture and definitions file with name. Required."), + new EngineCommandArgument("-9p", "Can be used multiple times for defining a 9patch. This parameter requires a name, left patch, right patch, top patch, and bottom patch in the format name,a,b,c,d. Optional."), + new EngineCommandArgument("-sp", "Starting power for one side of the texture. Default is 8."), + new EngineCommandArgument("-mp", "Maximum power for one side of the texture. Default is 8."), + new EngineCommandArgument("-dau", "Disables automatically upscaling the texture."), + }; + + help = "Packs a given directory composed of png and jpg files into an atlas. Can also add 9patch properties. Runnig without arguments triggers interactive mode."; } public override void Run(string[] arguments) { + TexturePacker texturePacker = null; string path = null; int mp = 8; @@ -44,72 +34,47 @@ namespace RecrownedAthenaeum.Tools.TextureAtlasTools bool dau = false; string output = null; - if (arguments == null) throw new ArgumentException("Requires arguments. Type \"help texturepacker\" for more info."); + if (arguments == null) + { + + } + + path = arguments[1 + IndexOfArgumentIn("-i", arguments)]; + int.TryParse(arguments[IndexOfArgumentIn("-mp", arguments) + 1], out mp); + int.TryParse(arguments[IndexOfArgumentIn("-sp", arguments) + 1], out sp); + output = arguments[IndexOfArgumentIn("-o", arguments) + 1]; + if (HasArgument("-dau", arguments)) dau = true; + + texturePacker = new TexturePacker(path, mp, sp); + ConsoleUtilities.WriteWrappedLine("Calculated minimum texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + " with a total of " + texturePacker.TexturesFound + " textures."); + + try + { + texturePacker.Build(!dau); + } + catch (InvalidOperationException e) + { + throw new ArgumentException(e.Message); + } for (int i = 0; i < arguments.Length; i++) { - if (arguments[i] == "-i") + if (arguments[i] == "-9p") { - if (i + 1 == arguments.Length) throw new ArgumentException("-i is not followed by path for input directory."); - path = arguments[i + 1]; - } - if (arguments[i] == "-mp") - { - if (i + 1 >= arguments.Length || !int.TryParse(arguments[i + 1], out mp)) throw new ArgumentException("mp is not followed by maximum power."); - } - if (arguments[i] == "-sp") - { - if (i + 1 >= arguments.Length || !int.TryParse(arguments[i + 1], out sp)) throw new ArgumentException("sp is not followed by maximum power."); - } - if (arguments[i] == "-o") - { - if (i + 1 >= arguments.Length) throw new ArgumentException("-o is not followed by path for output files. (eg. path/to/file where file is the name for the atlas.)"); - output = arguments[i + 1]; - } - - if (i == arguments.Length - 1 && output == null) - { - throw new ArgumentException("no -o argument found to specify output."); - } - - if (arguments[i] == "-dau") dau = true; - } - texturePacker = new TexturePacker(path, mp, sp); - ConsoleUtilities.WriteWrappedLine("Calculated minimum texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + " with a total of " + texturePacker.TexturesFound + " textures."); - if (texturePacker != null) - { - try - { - texturePacker.Build(!dau); - } - catch (InvalidOperationException e) - { - throw new ArgumentException(e.Message); - } - - for (int i = 0; i < arguments.Length; i++) - { - if (arguments[i] == "-9p") + if (i + 5 >= arguments.Length) throw new ArgumentException("-9p is not followed by proper specifiers for a 9Patch (format: \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch.)"); + string[] nPatchArgs = arguments[i + 1].Split(','); + try { - if (i + 5 >= arguments.Length) throw new ArgumentException("-9p is not followed by proper specifiers for a 9Patch (format: \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch.)"); - string[] nPatchArgs = arguments[i + 1].Split(','); - try - { - texturePacker.SetNinePatch(nPatchArgs[0], int.Parse(nPatchArgs[1]), int.Parse(nPatchArgs[2]), int.Parse(nPatchArgs[3]), int.Parse(nPatchArgs[4])); - } - catch (FormatException) - { - throw new ArgumentException("-9p argument parameters must be in the format \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch."); - } + texturePacker.SetNinePatch(nPatchArgs[0], int.Parse(nPatchArgs[1]), int.Parse(nPatchArgs[2]), int.Parse(nPatchArgs[3]), int.Parse(nPatchArgs[4])); + } + catch (FormatException) + { + throw new ArgumentException("-9p argument parameters must be in the format \"-9p textureName,a,b,c,d\" where a, b, c, and d are integers definining the border regions for the 9patch."); } } - texturePacker.Save(Path.GetDirectoryName(output), Path.GetFileName(output)); - Console.WriteLine("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + "."); - } - else - { - throw new ArgumentException("-i not specified."); } + texturePacker.Save(Path.GetDirectoryName(output), Path.GetFileName(output)); + Console.WriteLine("Complete. Final texture size: " + texturePacker.TextureLength + "x" + texturePacker.TextureLength + "."); } } } diff --git a/RecrownedAthenaeum.ConsoleTools/Tools.cs b/RecrownedAthenaeum.ConsoleTools/Tools.cs index 9c0700a..08857f6 100644 --- a/RecrownedAthenaeum.ConsoleTools/Tools.cs +++ b/RecrownedAthenaeum.ConsoleTools/Tools.cs @@ -18,7 +18,6 @@ namespace RecrownedAthenaeum.Tools ce.commands.Add(new HelpCommand(ce)); ce.commands.Add(new TexturePackerCommand()); ce.commands.Add(new StopCommand(ce)); - ce.commands.Add(new TestCommand()); ce.commands.Add(new ClearConsoleCommand()); if (args.Length > 0)