Browse Source

Updated comments in Command.cs.

Elabourated on comments already in Command.cs.
Added new comments to Command.cs.
I added these comments because I do not think the code is as readable as it can be here. I think I need to go in a refactor it a bit more, so it is a little easier to read.
merge-requests/1/head
Craig Oates 7 years ago
parent
commit
6bad3f6085
  1. 50
      Console.Waterworks/Console.Waterworks/Models/Command.cs

50
Console.Waterworks/Console.Waterworks/Models/Command.cs

@ -11,57 +11,77 @@ namespace Console.Waterworks.Models
private List<string> _arguments;
public IEnumerable<string> Arguments
{
get
{
return _arguments;
}
get { return _arguments; }
}
public Command(string input, string commandsNamespace, string className)
{
// Ugly regex to split string on spaces, but preserve quoted text intact:
var stringArray =
Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
// 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 stringArray = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
_arguments = new List<string>();
for (int i = 0; i < stringArray.Length; i++)
{
// The first element is always the command:
// The first part of the end-users input. This should be the command part of the input.
if (i == 0)
{
Name = stringArray[i];
// Set the class name
// 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[] s = stringArray[0].Split('.');
if (s.Length == 2)
{
ClassName = s[0];
Name = s[1];
// In short, you will have gone from "Class1.MethodName 12 12" to s[Class1, MethodName].
}
}
else
{
var inputArgument = stringArray[i];
// Assume that most of the time, the input argument is NOT 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.
string argument = inputArgument;
// Is the argument a quoted text 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.
var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline);
var match = regex.Match(inputArgument);
// If it IS quoted, there will be at least one capture:
// If the input is quoted text, "match" will hold the various elements making its Count greater than 1.
// This is when the when the code enters the if-block.
if (match.Captures.Count > 0)
{
// Get the unquoted text from within the qoutes:
// 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(match.Captures[0].Value);
// The argument should include all text from between the quotes
// as a single string:
// 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;
}
/*
* 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);
}
}

Loading…
Cancel
Save