# Simple command parser?

This topic is 3538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
It really depends on how simple of commands you're want to use. I have one application with a main loop that looks like:
  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.

##### Share on other sites
If I may, what exactly is all of this?

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.)

##### Share on other sites
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...)

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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) :

//! 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.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632139
• Total Posts
3004369

×