diff --git a/Console.Waterworks/Console.Waterworks/Models/Command.cs b/Console.Waterworks/Console.Waterworks/Models/Command.cs index 3e40c5c..66486ab 100644 --- a/Console.Waterworks/Console.Waterworks/Models/Command.cs +++ b/Console.Waterworks/Console.Waterworks/Models/Command.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Text.RegularExpressions; namespace Console.Waterworks.Models @@ -8,85 +9,68 @@ namespace Console.Waterworks.Models public string Name { get; set; } public string ClassName { get; set; } - private List _arguments; + List _arguments; public IEnumerable Arguments { get { return _arguments; } } + /* + * Let us assume the end-user entered: [> Say "Hello, World!" 2] + * Once everything has been parsed, _arguments should look like the following, + * _arguments["Hello, World!", 2] (This is the "same" for Arguments) + * Once everything has been processed, ClassName will have been set as as well as Arguments. + * Using the example at the top of this comment block, the end result will look somethinglike, + * ClassName = "Say" | Arguments = ["Hello, World!", 2] + * This object will then be used as the guide to locate the command-method this model describes. + */ public Command(string input, string commandsNamespace, string className) { - // This takes a string (the end-user entered into the console) and splits it up at the spaces. - // The string tokens are then put in to an array to keep the sentence intact. var splitInput = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); - _arguments = new List(); for (int i = 0; i < splitInput.Length; i++) { - // This (if) block is for the first part of the end-users input (i = 0). - // This should be the "command" part of the input. if (i == 0) - { - Name = splitInput[i]; - - // This sets the default name of the command-methods (A.K.A. "Console.Commands") - ClassName = className; - - /* - * This is for people referencing command-methods not in the default command-method class. - * At this moment in time, the default name is specified in CW_Constants: "ConsoleCommands". - * It seperates the class name from the command-method. - * Most of the time, this if-bock will never be entered -- this is a fancy feature. - */ - string[] splitCommandInput = splitInput[0].Split('.'); - if (splitCommandInput.Length == 2) - { - ClassName = splitCommandInput[0]; - Name = splitCommandInput[1]; - // In short, you will have gone from "Class1.MethodName" to splitCommandInput[Class1, MethodName]. - } - } - // This (else) block processes the arguments passed in after the command part of the input. + establishCommand(splitInput, i, className); else - { - var inputArgument = splitInput[i]; - - // The assumption here is the input argument is NOT going to be quoted text. - // The aim here is to deal with quoted text only when needed. - string argument = inputArgument; + establishArguments(splitInput, i); + } + } - // This bit is just a check to see if it is a quoted string. - // If it is quoted text, "matches" will hold the various parts of the quote. - var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline); - var matches = regex.Match(inputArgument); + void establishCommand(string[] splitInput, int splitInputLocation, string nameOfClass) + { + Name = splitInput[splitInputLocation]; + ClassName = nameOfClass; // The default is "ConsoleCommands.cs" + string[] splitCommandInput = splitInput[0].Split('.'); - // If the input is quoted text, "matches" will hold the various elements making its Count greater than 1. - // This is when the when the code enters the if-block. - if (matches.Captures.Count > 0) - { - // This if-block then deals with the various parts of the quoted text. - // It adds each part of the quoted text to "argument" one-by-one. - var captureQuotedText = new Regex("[^\"]*[^\"]"); - var quoted = captureQuotedText.Match(matches.Captures[0].Value); + // This next part is a fancy feature and will almost never be used. + if (splitCommandInput.Length == 2) + { + // In short, it takes something like >TestClass1.TestMethod2 "Hello, World". + // And, it isolates the "TestClass1" and TestMethod2" parts. + ClassName = splitCommandInput[0]; // "TestClass1" + Name = splitCommandInput[1]; // TestMethod2" + } + } - // This bit is so the quoted text can be returned as a single line. - // The most important part here is the exterenal elements have been removed. - // This means the command-method part as well as other parameters/arguments accompanying this one. - argument = quoted.Captures[0].Value; - } + void establishArguments(string[] splitInput, int splitInputLocation) + { + var inputArgument = splitInput[splitInputLocation]; + string argument = inputArgument; - /* - * Let us assume the end-user entered: Say "Hello, World!" 2 - * Once everything has been parsed, _arguments should look like the following, - * _arguments["Hello, World!", 2] (This is the "same" for Arguments) - * Once everything has been processed, ClassName will have been set (line 32 above) as as well as Arguments. - * Using the example at the top of this comment block, the end result will look something like, - * ClassName = "Say" | Arguments ["Hello, World!", 2] - * This object will then be used as the guide to locate the command-method this model describes. - */ - _arguments.Add(argument); - } + // This bit is just a check to see if the input is a quoted string. + // If it is quoted text, "matches" will hold the various parts of the quote. + var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline); + var matches = regex.Match(inputArgument); + if (matches.Captures.Count > 0) + { + // Each part of the quoted text is added to "argument" one-by-one. + // The aim is to add the quoted text as a single line. + var captureQuotedText = new Regex("[^\"]*[^\"]"); + var quoted = captureQuotedText.Match(matches.Captures[0].Value); + argument = quoted.Captures[0].Value; } + _arguments.Add(argument); } } }