Browse Source

(Minor) Refactored Command.cs.

Renamed several variables.
Edited several comments.
I am still not entirely happy with the readability of this code. But, I am hopefull it is something which can be read and understood with some degree of ease.
merge-requests/1/head
Craig Oates 7 years ago
parent
commit
8b0d377844
  1. 32
      Console.Waterworks/Console.Waterworks/Models/Command.cs

32
Console.Waterworks/Console.Waterworks/Models/Command.cs

@ -18,15 +18,16 @@ namespace Console.Waterworks.Models
{ {
// This takes a string (the end-user entered into the console) and splits it up at the spaces. // 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. // The string tokens are then put in to an array to keep the sentence intact.
var stringArray = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); var splitInput = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
_arguments = new List<string>(); _arguments = new List<string>();
for (int i = 0; i < stringArray.Length; i++) for (int i = 0; i < splitInput.Length; i++)
{ {
// The first part of the end-users input. This should be the command part of the input. // 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) if (i == 0)
{ {
Name = stringArray[i]; Name = splitInput[i];
// This sets the default name of the command-methods (A.K.A. "Console.Commands") // This sets the default name of the command-methods (A.K.A. "Console.Commands")
ClassName = className; ClassName = className;
@ -37,35 +38,36 @@ namespace Console.Waterworks.Models
* It seperates the class name from the command-method. * 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. * Most of the time, this if-bock will never be entered -- this is a fancy feature.
*/ */
string[] s = stringArray[0].Split('.'); string[] splitCommandInput = splitInput[0].Split('.');
if (s.Length == 2) if (splitCommandInput.Length == 2)
{ {
ClassName = s[0]; ClassName = splitCommandInput[0];
Name = s[1]; Name = splitCommandInput[1];
// In short, you will have gone from "Class1.MethodName 12 12" to s[Class1, MethodName]. // 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.
else else
{ {
var inputArgument = stringArray[i]; var inputArgument = splitInput[i];
// The assumption here is the input argument is NOT going to be quoted text. // 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. // The aim here is to deal with quoted text only when needed.
string argument = inputArgument; string argument = inputArgument;
// This bit is just a check to see if it is a quoted string. // This bit is just a check to see if it is a quoted string.
// If it is quoted text, "match" will hold the various parts of the quote. // If it is quoted text, "matches" will hold the various parts of the quote.
var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline); var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline);
var match = regex.Match(inputArgument); var matches = regex.Match(inputArgument);
// If the input is quoted text, "match" will hold the various elements making its Count greater than 1. // 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. // This is when the when the code enters the if-block.
if (match.Captures.Count > 0) if (matches.Captures.Count > 0)
{ {
// This if-block then deals with the various parts of the quoted text. // 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. // It adds each part of the quoted text to "argument" one-by-one.
var captureQuotedText = new Regex("[^\"]*[^\"]"); var captureQuotedText = new Regex("[^\"]*[^\"]");
var quoted = captureQuotedText.Match(match.Captures[0].Value); var quoted = captureQuotedText.Match(matches.Captures[0].Value);
// This bit is so the quoted text can be returned as a single line. // 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. // The most important part here is the exterenal elements have been removed.

Loading…
Cancel
Save