namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};
// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}
public static string TestMethod()
{
return "Yay it worked";
}
}
}
namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};
// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}
public static string TestMethod()
{
return "Yay it worked";
}
}
}