A NuGet package. It aim is to help you write extendable and command-based console programs in C# and .Net. https://www.craigoates.net/Software/Project/7
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.

90 lines
4.5 KiB

using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Console.Waterworks.Models
{
class Command
{
public string Name { get; set; }
public string ClassName { get; set; }
private List<string> _arguments;
public IEnumerable<string> Arguments
{
get { return _arguments; }
}
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 stringArray = Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
_arguments = new List<string>();
for (int i = 0; i < stringArray.Length; i++)
{
// The first part of the end-users input. This should be the command part of the input.
if (i == 0)
{
Name = stringArray[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[] 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];
// 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.
// 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 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)
{
// 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);
// 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);
}
}
}
}
}