Browse Source

Refactored Command.cs.

Decomposed constructor
Reduced comments
The aim was to increase the readability of the code with less need for comments. I believe the code is now more readable but time will tell I guess.
merge-requests/1/head
Craig Oates 7 years ago
parent
commit
c31668328c
  1. 108
      Console.Waterworks/Console.Waterworks/Models/Command.cs

108
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; using System.Text.RegularExpressions;
namespace Console.Waterworks.Models namespace Console.Waterworks.Models
@ -8,85 +9,68 @@ namespace Console.Waterworks.Models
public string Name { get; set; } public string Name { get; set; }
public string ClassName { get; set; } public string ClassName { get; set; }
private List<string> _arguments; List<string> _arguments;
public IEnumerable<string> Arguments public IEnumerable<string> Arguments
{ {
get { return _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) 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, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); var splitInput = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
_arguments = new List<string>(); _arguments = new List<string>();
for (int i = 0; i < splitInput.Length; i++) 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) if (i == 0)
{ establishCommand(splitInput, i, className);
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.
else else
{ establishArguments(splitInput, i);
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;
// This bit is just a check to see if it is a quoted string. void establishCommand(string[] splitInput, int splitInputLocation, string nameOfClass)
// If it is quoted text, "matches" will hold the various parts of the quote. {
var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline); Name = splitInput[splitInputLocation];
var matches = regex.Match(inputArgument); 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 next part is a fancy feature and will almost never be used.
// This is when the when the code enters the if-block. if (splitCommandInput.Length == 2)
if (matches.Captures.Count > 0) {
{ // In short, it takes something like >TestClass1.TestMethod2 "Hello, World".
// This if-block then deals with the various parts of the quoted text. // And, it isolates the "TestClass1" and TestMethod2" parts.
// It adds each part of the quoted text to "argument" one-by-one. ClassName = splitCommandInput[0]; // "TestClass1"
var captureQuotedText = new Regex("[^\"]*[^\"]"); Name = splitCommandInput[1]; // TestMethod2"
var quoted = captureQuotedText.Match(matches.Captures[0].Value); }
}
// This bit is so the quoted text can be returned as a single line. void establishArguments(string[] splitInput, int splitInputLocation)
// 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. var inputArgument = splitInput[splitInputLocation];
argument = quoted.Captures[0].Value; string argument = inputArgument;
}
/* // This bit is just a check to see if the input is a quoted string.
* Let us assume the end-user entered: Say "Hello, World!" 2 // If it is quoted text, "matches" will hold the various parts of the quote.
* Once everything has been parsed, _arguments should look like the following, var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline);
* _arguments["Hello, World!", 2] (This is the "same" for Arguments) var matches = regex.Match(inputArgument);
* Once everything has been processed, ClassName will have been set (line 32 above) as as well as Arguments. if (matches.Captures.Count > 0)
* Using the example at the top of this comment block, the end result will look something like, {
* ClassName = "Say" | Arguments ["Hello, World!", 2] // Each part of the quoted text is added to "argument" one-by-one.
* This object will then be used as the guide to locate the command-method this model describes. // The aim is to add the quoted text as a single line.
*/ var captureQuotedText = new Regex("[^\"]*[^\"]");
_arguments.Add(argument); var quoted = captureQuotedText.Match(matches.Captures[0].Value);
} argument = quoted.Captures[0].Value;
} }
_arguments.Add(argument);
} }
} }
} }

Loading…
Cancel
Save