-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_terminal_client.cpp
63 lines (52 loc) · 1.7 KB
/
mock_terminal_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include <cstdlib>
#include <cstdint>
#define PROMPT "> "
class CommandHandler {
public:
CommandHandler() {
counter = 0;
help_string = "Available commands:\n";
addFunction("help",
"print this help message",
[this]() { std::cout << help_string; counter++; });
addFunction("hello",
"say Hello!",
[this]() { std::cout << "Hello!\n"; counter++; });
addFunction("print",
"print how many times this session got a command",
[this]() { std::cout << "Function calls: " << counter << std::endl; counter++; });
addFunction("exit",
"exit the program",
[]() { std::cout << "Exiting program.\n"; exit(0); });
}
void addFunction(const std::string& name, const std::string& hint, std::function<void()> callback) {
commands[name] = callback;
help_string += name + " - " + hint + "\n";
}
void run() {
std::cout << "Enter a command, or help for more info.\n";
while (true) {
std::cout << PROMPT;
std::string input;
std::cin >> input;
if (commands.find(input) != commands.end()) {
commands[input]();
} else {
std::cout << "Invalid command. Type 'help' for a list of commands.\n";
}
}
}
private:
std::map<std::string, std::function<void()>> commands;
std::string help_string;
uint32_t counter;
};
int main() {
CommandHandler handler;
handler.run();
return 0;
}