Project restructure, Added .Net Core support, Fixed some bugs
This commit is contained in:
154
src/Discord.Net.Commands/CommandParser.cs
Normal file
154
src/Discord.Net.Commands/CommandParser.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public static class CommandParser
|
||||
{
|
||||
private enum CommandParserPart
|
||||
{
|
||||
None,
|
||||
CommandName,
|
||||
Parameter,
|
||||
QuotedParameter,
|
||||
DoubleQuotedParameter
|
||||
}
|
||||
|
||||
public static bool Parse(string input, out string command, out string[] args)
|
||||
{
|
||||
return Parse(input, out command, out args, true);
|
||||
}
|
||||
public static bool ParseArgs(string input, out string[] args)
|
||||
{
|
||||
string ignored;
|
||||
return Parse(input, out ignored, out args, false);
|
||||
}
|
||||
|
||||
private static bool Parse(string input, out string command, out string[] args, bool parseCommand)
|
||||
{
|
||||
CommandParserPart currentPart = parseCommand ? CommandParserPart.CommandName : CommandParserPart.None;
|
||||
int startPosition = 0;
|
||||
int endPosition = 0;
|
||||
int inputLength = input.Length;
|
||||
bool isEscaped = false;
|
||||
List<string> argList = new List<string>();
|
||||
|
||||
command = null;
|
||||
args = null;
|
||||
|
||||
if (input == "")
|
||||
return false;
|
||||
|
||||
while (endPosition < inputLength)
|
||||
{
|
||||
char currentChar = input[endPosition++];
|
||||
if (isEscaped)
|
||||
isEscaped = false;
|
||||
else if (currentChar == '\\')
|
||||
isEscaped = true;
|
||||
|
||||
switch (currentPart)
|
||||
{
|
||||
case CommandParserPart.CommandName:
|
||||
if ((!isEscaped && currentChar == ' ') || endPosition >= inputLength)
|
||||
{
|
||||
int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition;
|
||||
string temp = input.Substring(startPosition, length);
|
||||
if (temp == "")
|
||||
startPosition = endPosition;
|
||||
else
|
||||
{
|
||||
currentPart = CommandParserPart.None;
|
||||
command = temp;
|
||||
startPosition = endPosition;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CommandParserPart.None:
|
||||
if ((!isEscaped && currentChar == '\"'))
|
||||
{
|
||||
currentPart = CommandParserPart.DoubleQuotedParameter;
|
||||
startPosition = endPosition;
|
||||
}
|
||||
else if ((!isEscaped && currentChar == '\''))
|
||||
{
|
||||
currentPart = CommandParserPart.QuotedParameter;
|
||||
startPosition = endPosition;
|
||||
}
|
||||
else if ((!isEscaped && currentChar == ' ') || endPosition >= inputLength)
|
||||
{
|
||||
int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition;
|
||||
string temp = input.Substring(startPosition, length);
|
||||
if (temp == "")
|
||||
startPosition = endPosition;
|
||||
else
|
||||
{
|
||||
currentPart = CommandParserPart.None;
|
||||
argList.Add(temp);
|
||||
startPosition = endPosition;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CommandParserPart.QuotedParameter:
|
||||
if ((!isEscaped && currentChar == '\''))
|
||||
{
|
||||
string temp = input.Substring(startPosition, endPosition - startPosition - 1);
|
||||
currentPart = CommandParserPart.None;
|
||||
argList.Add(temp);
|
||||
startPosition = endPosition;
|
||||
}
|
||||
else if (endPosition >= inputLength)
|
||||
return false;
|
||||
break;
|
||||
case CommandParserPart.DoubleQuotedParameter:
|
||||
if ((!isEscaped && currentChar == '\"'))
|
||||
{
|
||||
string temp = input.Substring(startPosition, endPosition - startPosition - 1);
|
||||
currentPart = CommandParserPart.None;
|
||||
argList.Add(temp);
|
||||
startPosition = endPosition;
|
||||
}
|
||||
else if (endPosition >= inputLength)
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (parseCommand && (command == null || command == ""))
|
||||
return false;
|
||||
|
||||
args = argList.ToArray();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ArgsEqual(string[] args, int expected)
|
||||
{
|
||||
return args.Length == expected;
|
||||
}
|
||||
public static bool ArgsAtLeast(string[] args, int expected)
|
||||
{
|
||||
return args.Length >= expected;
|
||||
}
|
||||
public static bool ArgsAtMost(string[] args, int expected)
|
||||
{
|
||||
return args.Length <= expected;
|
||||
}
|
||||
public static bool ArgsIn(string[] args, params int[] expected)
|
||||
{
|
||||
int count = args.Length;
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
if (count == expected[i])
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static bool ArgsBetween(string[] args, int min, int max)
|
||||
{
|
||||
return args.Length >= min && args.Length <= max;
|
||||
}
|
||||
public static bool NoArgs(string[] args, params int[] expected)
|
||||
{
|
||||
return args.Length == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user