-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_cmd.h
327 lines (270 loc) · 8.08 KB
/
rjd_cmd.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
#define RJD_CMD_H 1
struct rjd_mem_allocator;
struct rjd_cmd_argv
{
const char* shortname;
const char* longname;
const char* argname;
const char* description;
};
struct rjd_cmd
{
int argc;
const char** argv;
struct rjd_cmd_argv* opts;
struct rjd_cmd_argv* reqs;
struct rjd_mem_allocator* allocator;
};
struct rjd_cmd rjd_cmd_init(int argc, const char** argv, struct rjd_mem_allocator* allocator);
void rjd_cmd_free(struct rjd_cmd* cmd);
void rjd_cmd_add_opt(struct rjd_cmd* cmd, const char* shortname, const char* longname, const char* argname, const char* description);
void rjd_cmd_add_req(struct rjd_cmd* cmd, const char* argname, const char* description);
bool rjd_cmd_ok(const struct rjd_cmd* cmd);
void rjd_cmd_usage(const struct rjd_cmd* cmd);
void rjd_cmd_help(const struct rjd_cmd* cmd);
int rjd_cmd_int(const struct rjd_cmd* cmd, const char* shortname, int _default);
unsigned rjd_cmd_uint(const struct rjd_cmd* cmd, const char* shortname, unsigned _default);
double rjd_cmd_float(const struct rjd_cmd* cmd, const char* shortname, double _default);
bool rjd_cmd_bool(const struct rjd_cmd* cmd, const char* shortname);
const char* rjd_cmd_str(const struct rjd_cmd* cmd, const char* shortname);
#if RJD_IMPL
struct rjd_cmd rjd_cmd_init(int argc, const char** argv, struct rjd_mem_allocator* allocator)
{
struct rjd_cmd cmd = {argc, argv, NULL, NULL, allocator};
cmd.opts = rjd_array_alloc(struct rjd_cmd_argv, 8, allocator);
cmd.reqs = rjd_array_alloc(struct rjd_cmd_argv, 8, allocator);
rjd_cmd_add_opt(&cmd, "-h", "--help", NULL, "Prints help");
return cmd;
}
void rjd_cmd_free(struct rjd_cmd* cmd)
{
rjd_array_free(cmd->opts);
rjd_array_free(cmd->reqs);
}
void rjd_cmd_add_opt(struct rjd_cmd* cmd, const char* shortname, const char* longname, const char* argname, const char* description)
{
RJD_ASSERT(cmd);
RJD_ASSERT(shortname);
RJD_ASSERT(longname);
RJD_ASSERT(description);
struct rjd_cmd_argv opt = { shortname, longname, argname, description };
rjd_array_push(cmd->opts, opt);
}
void rjd_cmd_add_req(struct rjd_cmd* cmd, const char* argname, const char* description)
{
RJD_ASSERT(cmd);
RJD_ASSERT(argname);
RJD_ASSERT(description);
struct rjd_cmd_argv req = { NULL, NULL, argname, description };
rjd_array_push(cmd->reqs, req);
}
static const struct rjd_cmd_argv* rjd_cmd_matchopt(const struct rjd_cmd* cmd, const char* argv);
static int rjd_cmd_firstreq(const struct rjd_cmd* cmd);
static const struct rjd_cmd_argv* rjd_cmd_getopt(const struct rjd_cmd* cmd, const char* shortname);
static const char* rjd_cmd_findopt(const struct rjd_cmd* cmd, const char* shortname);
static const char* rjd_cmd_findreq(const struct rjd_cmd* cmd, const char* argname);
bool rjd_cmd_ok(const struct rjd_cmd* cmd)
{
RJD_ASSERT(cmd);
int count = rjd_array_count(cmd->reqs);
if (cmd->argc - 1 < count) {
return false;
}
const int firstreq = rjd_cmd_firstreq(cmd);
for (int i = 1; i < firstreq; ++i) {
const struct rjd_cmd_argv* opt = rjd_cmd_matchopt(cmd, cmd->argv[i]);
if (!opt) {
return false;
}
if (opt->argname) {
if (!strcmp(cmd->argv[i], opt->shortname)) {
// since we're expecting an argument, this shouldn't match any other options
if (rjd_cmd_matchopt(cmd, cmd->argv[i+1])) {
return false;
}
} else {
const char* eq = strstr(cmd->argv[i], "=");
if (!eq) {
return false;
}
const char* arg = eq + 1;
if (*arg == 0) {
return false;
}
}
++i;
}
}
return (cmd->argc - 1 - firstreq) == (int) rjd_array_count(cmd->reqs);
}
void rjd_cmd_usage(const struct rjd_cmd* cmd)
{
// TODO rjd_stringbuilder
size_t offset = 0;
char optString[4096];
for (size_t i = 0; i < rjd_array_count(cmd->opts); ++i) {
offset += snprintf(optString + offset, sizeof(optString) - offset, "%s", cmd->opts[i].shortname);
if (i < rjd_array_count(cmd->opts) - 1) {
offset += snprintf(optString + offset, sizeof(optString) - offset, " ");
}
}
optString[offset] = 0;
offset = 0;
char reqString[4096];
for (size_t i = 0; i < rjd_array_count(cmd->reqs); ++i) {
offset += snprintf(reqString + offset, sizeof(reqString) - offset, "%s", cmd->reqs[i].argname);
if (i < rjd_array_count(cmd->opts) - 1) {
offset += snprintf(reqString + offset, sizeof(reqString) - offset, " ");
}
}
reqString[offset] = 0;
RJD_LOG("Usage: %s [%s] %s", cmd->argv[0], optString, reqString);
}
void rjd_cmd_help(const struct rjd_cmd* cmd)
{
rjd_cmd_usage(cmd);
for (size_t i = 0; i < rjd_array_count(cmd->reqs); ++i) {
RJD_LOG("%s\n\t%s", cmd->reqs[i].argname, cmd->reqs[i].description);
}
for (size_t i = 0; i < rjd_array_count(cmd->opts); ++i) {
const struct rjd_cmd_argv* arg = cmd->opts + i;
if (arg->argname) {
RJD_LOG("%s %s, %s=%s\n\t%s", arg->shortname, arg->argname, arg->longname, arg->argname, arg->description);
} else {
RJD_LOG("%s, %s\n\t%s", arg->shortname, arg->longname, arg->description);
}
}
}
int rjd_cmd_int(const struct rjd_cmd* cmd, const char* name, int _default)
{
return (int)rjd_cmd_float(cmd, name, _default);
}
unsigned rjd_cmd_uint(const struct rjd_cmd* cmd, const char* name, unsigned _default)
{
double v = rjd_cmd_float(cmd, name, _default);
if (v < 0) {
return _default;
}
return (unsigned)v;
}
double rjd_cmd_float(const struct rjd_cmd* cmd, const char* name, double _default)
{
const char* str = rjd_cmd_str(cmd, name);
if (!str) {
return _default;
}
char* end = NULL;
double v = strtod(str, &end);
if (v == 0 && end != NULL) {
return _default;
}
return v;
}
bool rjd_cmd_bool(const struct rjd_cmd* cmd, const char* name)
{
const char* str = rjd_cmd_str(cmd, name);
if (!str) {
return false;
}
if (!strcmp(str, "true")) {
return true;
} else if (!strcmp(str, "false")) {
return false;
}
const struct rjd_cmd_argv* opt = rjd_cmd_getopt(cmd, name);
return opt && !strcmp(opt->shortname, name);
}
const char* rjd_cmd_str(const struct rjd_cmd* cmd, const char* name)
{
const char* opt = rjd_cmd_findopt(cmd, name);
if (opt) {
return opt;
}
const char* req = rjd_cmd_findreq(cmd, name);
if (req) {
return req;
}
return NULL;
}
static const struct rjd_cmd_argv* rjd_cmd_matchopt(const struct rjd_cmd* cmd, const char* argv)
{
if (!argv) {
return NULL;
}
for (uint32_t i = 0; i < rjd_array_count(cmd->opts); ++i) {
const char* shortname = cmd->opts[i].shortname;
const char* longname = cmd->opts[i].longname;
if (!strcmp(shortname, argv)) {
return cmd->opts + i;
}
if (strstr(argv, longname) == argv) {
return cmd->opts + i;
}
}
return NULL;
}
static int rjd_cmd_firstreq(const struct rjd_cmd* cmd)
{
int index = 0;
for (int i = 1; i < cmd->argc; ++i) {
const struct rjd_cmd_argv* opt = rjd_cmd_matchopt(cmd, cmd->argv[i]);
if (opt) {
// skip the argument (assuming the format is ok)
if (!strcmp(cmd->argv[i], opt->shortname) && opt->argname) {
++i;
}
index = i;
} else {
break;
}
}
return index + 1;
}
static const struct rjd_cmd_argv* rjd_cmd_getopt(const struct rjd_cmd* cmd, const char* shortname)
{
for (uint32_t i = 0; i < rjd_array_count(cmd->opts); ++i) {
if (!strcmp(cmd->opts[i].shortname, shortname)) {
return cmd->opts + i;
}
}
return NULL;
}
static const char* rjd_cmd_findopt(const struct rjd_cmd* cmd, const char* shortname)
{
for (int i = 0; i < cmd->argc; ++i) {
const struct rjd_cmd_argv* opt = rjd_cmd_matchopt(cmd, cmd->argv[i]);
if (opt && !strcmp(opt->shortname, shortname)) {
if (!opt->argname) {
return cmd->argv[i];
}
if (!strcmp(cmd->argv[i], opt->shortname)) {
return cmd->argv[i + 1];
}
const char* eq = strstr(cmd->argv[i], "=");
if (eq) {
return eq + 1;
}
break;
}
}
return NULL;
}
static const char* rjd_cmd_findreq(const struct rjd_cmd* cmd, const char* argname)
{
int reqindex = -1;
for (int i = 0; i < (int)rjd_array_count(cmd->reqs); ++i) {
if (!strcmp(cmd->reqs[i].argname, argname)) {
reqindex = i;
break;
}
}
if (reqindex == -1) {
return NULL;
}
int optindex = rjd_cmd_firstreq(cmd) - 1; // -1 to get to first opt index
int argvindex = optindex + reqindex + 1; // +1 to skip exe arg
RJD_ASSERT(argvindex < cmd->argc);
return cmd->argv[argvindex];
}
#endif