-
Notifications
You must be signed in to change notification settings - Fork 318
/
callback.cpp
154 lines (131 loc) · 3.28 KB
/
callback.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <util.h>
#include <common.h>
#include <errlog.h>
#include <callback.h>
static std::vector<Callback*> *callbacks;
typedef std::map<uintptr_t, Callback*> CBMap;
Callback::Callback() {
if(0==callbacks) {
callbacks = new std::vector<Callback*>;
}
callbacks->push_back(this);
}
Callback *Callback::find(
const char *name,
bool printList
) {
CBMap found;
auto sz = strlen(name);
for(auto c:*callbacks) {
std::vector<const char*> names;
names.push_back(c->name());
c->aliases(names);
for(const auto nm:names) {
auto x = std::min(sz, strlen(nm));
if(0==strncasecmp(nm, name, x)) {
found[(uintptr_t)c] = c;
}
}
}
if(1==found.size() && !printList) {
return found.begin()->second;
} else if(0==found.size()) {
printf("\n");
warning("\"%s\": unknown command name\n", name);
} else {
printf("\n");
if(!printList) {
warning(
"\"%s\": ambiguous command name, could be any of:\n",
name
);
}
int count = 0;
for(auto pair:found) {
std::vector<const char*> names;
auto c = pair.second;
c->aliases(names);
printf(
" %3d. %s%s",
++count,
c->name(),
0<names.size() ? " (aliases: " : ""
);
auto first = true;
for(const auto nm:names) {
printf(
"%s%s",
first ? "" : ", ",
nm
);
first = false;
}
printf(
"%s\n",
0<names.size() ? ")" : ""
);
}
printf("\n");
}
if(printList || 0==strcmp(name, "help")) {
return 0;
}
printf("use:\n");
printf(" \"parser man\" for complete documentation of all commands.\n");
printf(" \"parser help\" for a short summary of available commands.\n\n");
exit(-1);
}
static void printHelp(
const Callback *c,
bool longHelp
) {
printf("\n--------------------------------------------------------------------------------------\n");
printf("USAGE:\n\n parser %s ", c->name());
const auto parser = c->optionParser();
if(parser) {
auto usg = parser->usage();
printf("%s\n\n", usg.c_str());
auto opt = parser->format_option_help();
if(opt.size()) {
printf("OPTIONS:\n\n%s\n", opt.c_str());
}
}
}
void Callback::showHelpFor(
const char *name,
bool longHelp
) {
auto cb = find(name);
if(cb) {
printHelp(cb, longHelp);
}
}
struct Cmp {
bool operator()(
const Callback *a,
const Callback *b
) {
auto an = a->name();
auto bn = b->name();
auto n = strcmp(an, bn);
return n<0;
}
};
void Callback::showAllHelps(
bool longHelp
) {
std::sort(
callbacks->begin(),
callbacks->end(),
Cmp()
);
for(const auto c:*callbacks) {
printHelp(c, longHelp);
}
printf("\n");
}