-
Notifications
You must be signed in to change notification settings - Fork 2
/
kvraft_tool_main.cpp
executable file
·88 lines (57 loc) · 1.83 KB
/
kvraft_tool_main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* kvraft_tool_main.cpp
Generated by phxrpc_pb2tool from kvraft.proto
*/
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <unistd.h>
#include "phxrpc_kvraft_tool.h"
#include "kvraft_tool_impl.h"
#include "kvraft_client.h"
#include "phxrpc/file.h"
using namespace phxrpc;
void ShowUsage(const char *program) {
printf("\nUsage: %s [-c <config>] [-f <func>] [-v]\n", program);
KVRaftTool::Name2Func_t *name2func = KVRaftTool::GetName2Func();
for (int i{0}; ; ++i) {
KVRaftTool::Name2Func_t *iter = &(name2func[i]);
if (nullptr == iter->name) break;
printf(" -f %s %s\n", iter->name, iter->usage);
}
printf("\n");
exit(0);
}
int main(int argc, char **argv) {
const char *func{nullptr};
const char *config{nullptr};
for (int i{1}; argc - 1 > i; ++i) {
if (0 == strcmp(argv[i], "-c")) {
config = argv[++i];
}
if (0 == strcmp(argv[i], "-f")) {
func = argv[++i];
}
if (0 == strcmp(argv[i], "-v")) {
ShowUsage(argv[0]);
}
}
if (nullptr == func) ShowUsage(argv[0]);
if (nullptr != config) KVRaftClient::Init(config);
KVRaftTool::Name2Func_t *target{nullptr};
KVRaftTool::Name2Func_t *name2func{KVRaftTool::GetName2Func()};
for (int i{0}; 100 > i; ++i) {
KVRaftTool::Name2Func_t *iter = &(name2func[i]);
if (nullptr == iter->name) break;
if (0 == strcasecmp(func, iter->name)) {
target = iter;
break;
}
}
if (nullptr == target) ShowUsage(argv[0]);
OptMap opt_map(target->opt_string);
if (!opt_map.Parse(argc, argv)) ShowUsage(argv[0]);
KVRaftTool::ToolFunc_t targefunc = target->func;
KVRaftToolImpl tool;
if (0 != (tool.*targefunc)(opt_map)) ShowUsage(argv[0]);
return 0;
}