0

Hi I'm trying implement commands in a c++ console app The app has a command prompt , basically does nothing until you type a specific command , now the problem is I can't find an efficient way to do this, the only solution I can think of is implementing thousands of if else statements which are not exactly efficient, also switch statements don't work on strings, The commands are separate functions with different arguments and all commands are preprocessor definitions,

I tried implementing if else statements but they were silly

one other way i also could think of was using a scripting language and implementing my own functions, for example i could use chaiscript and implement my own function called 'new' which takes two parameters (first what type of file and second the name of the file) and i could give it the command line inputs instead of a script file, this approach could save me from creating my own scripting language or command line interface(whatever it is called)

6
  • 1
    std::string cmd; std::getline(std::cin, cmd); if(cmd == "secret") { /* do stuff */ } or use a std::unordered_map to map from your commands to whatever function you want ot execute for each command Commented Mar 21, 2022 at 20:33
  • You are reading input from user. if chain is hardly going to be noticeable. Don't bother with performance until it is actually not meeting requirements. Commented Mar 21, 2022 at 20:33
  • 3
    Watch the tags. The approaches in C are quite different from C++. Depending on the size of the protocol a std::unordered_map<std::string, std::function> could be what you are looking for. Commented Mar 21, 2022 at 20:35
  • 6
    Side note: It's often a good idea to show even the silly attempts. That attempt A) might not be all that silly and B) could be the only thing separating your question from being dismissed as zero-effort homework crowd sourcing. Commented Mar 21, 2022 at 20:38
  • This question is unanswerable within the guidelines of Stack Overflow, as you have not provided a code example that represents what you're trying to achieve. As a result, potential solutions will be generic and not tailored to your needs. Commented Mar 21, 2022 at 20:38

1 Answer 1

2

Have a std::map of string => std::function

Like this

#include <iostream>
#include <map>
#include <functional>
#include <string>
typedef std::function<void(const std::string &)> CmdFunc;
void cmda(const std::string &line) {
    std::cout << "cmda " << line << "\n";
}
void cmdb(const std::string& line) {
    std::cout << "cmdb " << line << "\n";
}

int main() {

    std::map<std::string, CmdFunc > cmds;
    cmds["a"] = cmda;
    cmds["b"] = cmdb;
    //...
    while (true) {

        std::string cmdLine;
        std::getline(std::cin, cmdLine);
        std::string token = cmdLine.substr(0, cmdLine.find(" "));
        auto find = cmds.find(token);
        if (find != cmds.end())
            (find->second)(cmdLine);
        else
            std::cout << "unknown command\n";

    }
}

output

a froot
cmda a froot
b wang
cmdb b wang
c
unknown command
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I was looking for thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.