Simple command parser?
How would I implement a simple command parser? I was planning on starting out simple by just implementing commands and then later adding things like parameters. How would I do this? Would I have a list of commands that execute when the parser matches the command to something user types? Does that mean I'd have to treat parameters as separate commands?
Well, if you were using Python, you could just take strings from the user with something like raw_input() and dumping them in eval(). If not using Python, then please specify what language you are using, and possibly other pertinent information like how you want user input to occur with your application.
Sorry, I always forget to mention which language I'm using. I'm going to be using C++. For the time being the user will be using a console window, like a DOS prompt.
Boost Spirit, or older, battle-tested Lex/Yacc and descendants (Flex/Bison, etc.).
Another option more aimed at actually solving the task rather than exploring techniques is exposing your functions via Lua, possibly by using a choice of wrapper/interface libraries.
There's also Boost program options, which is specifically aimed at GNU-like command-line parsing.
For trivial parsing, simple string tokenization might work as well.
Another option more aimed at actually solving the task rather than exploring techniques is exposing your functions via Lua, possibly by using a choice of wrapper/interface libraries.
There's also Boost program options, which is specifically aimed at GNU-like command-line parsing.
For trivial parsing, simple string tokenization might work as well.
It really depends on how simple of commands you're want to use. I have one application with a main loop that looks like:
Hardly award winning code, but it works fine for the very simple commands the program was supposed to work with.
for (;;) { std::cout << ">>> " << std::flush; std::string command_line; getline(std::cin, command_line); std::stringstream sstr(command_line); std::string command; sstr >> command; if (command == "scan") { std::string name; int value; sstr >> name >> value; // do stuff for scan } else if (command == "filter") { std::string name; int value; sstr >> name >> value; // do stuff for filter } else if (command == "display") { std::string name; sstr >> name; // do stuff for display } // and more commands
Hardly award winning code, but it works fine for the very simple commands the program was supposed to work with.
If I may, what exactly is all of this?
Pardon the very novice question, but why not just use cin >> command? (I'm sure there's a good reason, I'm just curious.)
std::cout << ">>> " << std::flush;std::string command_line;getline(std::cin, command_line); std::stringstream sstr(command_line);std::string command;sstr >> command;
Pardon the very novice question, but why not just use cin >> command? (I'm sure there's a good reason, I'm just curious.)
He/you could, but then you're constraining your quake-style console to only process stuff from standard in. You often don't want that (input via GUI, input via config file, via network...)
What if I'm just messing around with a console-based text game? I won't be doing anything with GUIs, networking, or reading commands from files.
cin>>stuff will only work for a single word, or up to the first space. His solution takes in the entire line, and then stores it for convenient access.
Im not to sure if this is what you want, but in my systems parser I have a base class declared like this (A little modified to better suit C++ library) :
You can store a list of all of the commands in an std::vector<> or std::list<> and create them dynamically (i.e., internal_cmd* cmd = new specific_cmd). Create a class for a new command and execute the command like this (Not tested) :
This allows your command parser to treat each command as the same base class and execute the command by calling the commands callback method. This also provides a separation between your command parser and the actual commands themselves.
//! internal commandclass internal_cmd {public: std::string m_name; int m_flags; virtual void desc () = 0; virtual char* desc_short () = 0; virtual int callback (char*) = 0;};
You can store a list of all of the commands in an std::vector<> or std::list<> and create them dynamically (i.e., internal_cmd* cmd = new specific_cmd). Create a class for a new command and execute the command like this (Not tested) :
//! this is command to teststd::string inputbuf;std::vector <internal_cmd*>::iterator it = cmds.begin();for (; it != cmds.end(); it++) if (*it) if ((*it)->m_name == inputbuf) { (*it)->callback (command_parms); break; }
This allows your command parser to treat each command as the same base class and execute the command by calling the commands callback method. This also provides a separation between your command parser and the actual commands themselves.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement