DeukBot4/DeukBot4/MessageHandlers/CommandHandler/RequestStructure/ParameterMatcher.cs

59 lines
1.8 KiB
C#

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
{
public static class ParameterMatcher
{
public enum ParameterType
{
Word,
Number,
Remainder,
}
public static string[] GenerateRegex(Command command)
{
var arr = new string[command.ParameterTypes.Length];
for (var index = 0; index < command.ParameterTypes.Length; index++)
{
var commandParameterType = command.ParameterTypes[index];
var builder = new StringBuilder();
builder.Append(GetParameterRegex(commandParameterType[index]));
arr[index] = builder.ToString();
}
return arr;
}
private static string GetParameterRegex(ParameterType type)
{
switch (type)
{
case ParameterType.Word:
return " *(\\w+)";
case ParameterType.Number:
return " *(\\d+)";
case ParameterType.Remainder:
return " *(.*)";
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
public static RequestParameter[] GetParameterValues(Command command, string parameterString)
{
foreach (var pattern in command.ParametersMatchers)
{
var matches = Regex.Match(parameterString, pattern);
if (matches.Success)
{
return matches.Groups.Skip(1).Select(x => new RequestParameter(x.Value)).ToArray();
}
}
return new RequestParameter[0];
}
}
}