I wanted to get some "basic" design ideas on parsing and processing commands that the user has typed in. I already have a basic system implemented that can get the command and arguments from the user. It can already split up which one was the command and argument and store it. Though the proper way to deal with commands is what I really am asking for.
I have done this before and they were just really simple ones. I would just "hard code" a if else if statement against some simple commands. While they may work for a simple application if used more and more, a more sophisticated designed basically mandatory.
I would like a way to "easily" add a command if needed, change how many arguments are required for that command. I am trying to get a "sophisticated" design though this isn't by any means a complicated parser, scripting language or anything like that.
Example of how simple:
>add 10 10
"add" would be the command and the arguments are everything afterwards. I have a basic system that gives the command and arguments the user typed in already so I guess parsing the commands and arguments isn't the problem. I also can "split" the arguments up and do something with them. From the previous example I can split 10 ad 10 up and give them to add so it can do it's job. Though that is the part that feels very "hackish." Giving the arguments to the command and processing the command. It feels very hardcoded.
Quick Example of what my code may look like right now:
// ..more stuff here
std::string input = GetUserInput();
// the command the user typed in
std::string command = ExtractCommand(input);
// arguments for the command
std::string arguments = ExtractArguments(input);
// "Mode" or Command we need to use
// CommandMode would just be an enum that takes in the command and returns what "mode" we need to be in
CommandMode userMode = GetMode(command);
switch(userMode)
{
// ... all the different cases/commands, would run the function needed for that mode/command
}
// ... more stuff here
Sure it works but seems hackish and requires work if anything changes like more commands need to be added also checking that the correct arguments are entered and even if that command needed any arguments.
At this time also I do just assume that the arguments passed in are the correct "type" though later on I am looking to expand it to check that the argument list fits the command by what it expected and the number of arguments it needs or wants.
Thanks for any thoughts and ideas!