using System.CommandLine.Parsing;
namespace Brutal.ImGuiApi.Abstractions;
///
/// A parsing utility class for the terminal interface
///
internal static class Parser
{
private const char CommandDelimiter = ';';
///
/// Splits an input string into individual command strings
///
/// The raw string from terminal input
/// A list of individual command strings
public static List SplitToCommands(string inInput)
{
var commands = new List();
var withinQuotes = false;
var startIndex = 0;
void TryAddCommand(string command)
{
var trimmedCommand = command.Trim();
if (trimmedCommand.Length > 0)
commands.Add(trimmedCommand);
}
// Ignore our delimiter if it's part of a quoted string
for (var i = 0; i < inInput.Length; i++)
if (inInput[i] == '\"')
{
withinQuotes = !withinQuotes;
}
else if (inInput[i] == CommandDelimiter && !withinQuotes)
{
// Split input into individual commands by delimiter
TryAddCommand(inInput.Substring(startIndex, i - startIndex));
startIndex = i + 1;
}
// Add the last command if there is one
if (startIndex < inInput.Length)
TryAddCommand(inInput.Substring(startIndex).Trim());
return commands;
}
///
/// Splits a command string into a command name and args string
///
/// The command string to split
/// The command name part of the input string
/// The args part of the input string
public static void SplitCommand(string inCommandString, out string inCommandName, out string inArgsString)
{
const StringSplitOptions flags = StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries;
// Split at first whitespace
var split = inCommandString.Split(Array.Empty(), 2, flags);
if (split.Length == 0)
throw new TerminalInterfaceException("Command parsing failed, command string was empty.");
inCommandName = split[0].ToLower();
inArgsString = split.Length > 1 ? split[1] : "";
}
///
/// Splits an arg string into individual args according to the parsing mode
///
/// The string to split
/// The parsing mode to use
/// An array of arg strings, or null if the args string was empty
public static string[]? SplitToArgs(string inArgsString, ArgParseMode inArgParseMode)
{
// If we're using single string parsing mode, return the args as a single string
if (inArgParseMode == ArgParseMode.Unparsed)
// Remove outer whitespace and quotes if they exist
return new[] { inArgsString };
// If there are no args, return null
if (string.IsNullOrEmpty(inArgsString))
return null;
// If we're using default parsing mode, split the args into individual strings
return CommandLineParser.SplitCommandLine(inArgsString).ToArray();
}
}