c++ - Parsing commands in a console application -


so console application based around commands, example user enter "connect 127.0.0.1" , stuff. able separate commands arguments, how make call function based on command string, , have match commands string counterparts?

use standard parsing techniques, , read commands std::cin

you might want declare type vector of arguments.

typedef std::vector<std::string> vectarg_t; 

declare type functions processing commands:

typedef std::function<void(vectarg_t)> commandprocess_t; 

and have map associating command names processors:

std::map<std::string,commandprocess_t> commandmap; 

then fill map @ initialization time, e.g. using anonymous functions:

 commandmap["echo"] = [&](vectarg_t args){      int cnt=0;     (auto& curarg: args) {        if (cnt++ > 0)   std::cout << ' ';       std::cout << curarg;     }     std::cout << std::endl;  }; 

if question parsing program arguments (of main), use getopt(3) , related functions. gnu libc provides argp_parse.

consider perhaps instead embedding interpreter in program, e.g. embedding gnu guile or lua. embedding interpreter important architectural design decision , should made early. application becomes turing-complete.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -