Console.Waterworks.Core is the .Net Core version of Console.Waterworks.
https://www.craigoates.net/Software/project/8
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
3.2 KiB
78 lines
3.2 KiB
7 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text.RegularExpressions;
|
||
|
|
||
|
namespace Console.Waterworks.Core.Models
|
||
|
{
|
||
|
class Command
|
||
|
{
|
||
|
public string Name { get; set; }
|
||
|
public string ClassName { get; set; }
|
||
|
|
||
|
List<string> _arguments;
|
||
|
public IEnumerable<string> Arguments
|
||
|
{
|
||
|
get { return _arguments; }
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Let us assume the end-user entered: [> Say "Hello, World!" 2].
|
||
|
* The input entered needs to be parse and seperated.
|
||
|
* This is so ClassName and Arguments can be set.
|
||
|
* Once everything has been parsed, _arguments should look like the following,
|
||
|
* _arguments["Hello, World!", "2"] (This will be "mirrored" in 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.
|
||
|
*/
|
||
|
public Command(string input, string commandsNamespace, string className)
|
||
|
{
|
||
|
var splitInput = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
|
||
|
_arguments = new List<string>();
|
||
|
for (int i = 0; i < splitInput.Length; i++)
|
||
|
{
|
||
|
if (i == 0)
|
||
|
establishCommand(splitInput, i, className);
|
||
|
else
|
||
|
establishArguments(splitInput, i);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void establishCommand(string[] splitInput, int splitInputLocation, string nameOfClass)
|
||
|
{
|
||
|
Name = splitInput[splitInputLocation];
|
||
|
ClassName = nameOfClass; // The default is "ConsoleCommands.cs"
|
||
|
string[] splitCommandInput = splitInput[0].Split('.');
|
||
|
|
||
|
// 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"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void establishArguments(string[] splitInput, int splitInputLocation)
|
||
|
{
|
||
|
var inputArgument = splitInput[splitInputLocation];
|
||
|
string argument = inputArgument;
|
||
|
|
||
|
// This bit is just a check to see if the current part of 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);
|
||
|
}
|
||
|
}
|
||
|
}
|