-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.module.c
256 lines (208 loc) · 5.84 KB
/
cli.module.c
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
#include <stdlib.h>
#include <stdio.h>
export {
#include <stdbool.h>
}
build depends "deps/hash/hash.c";
#include "deps/hash/hash.h"
export typedef struct {
const char * description;
const char * long_name;
const char * short_name;
bool required;
} flag_options;
enum flag_type {
flag_type_bool = 0,
flag_type_int,
flag_type_string,
};
typedef struct {
void * dest;
enum flag_type type;
const char * description;
char * long_name;
char * short_name;
char short_name_buf[3];
bool required;
bool has_value;
} flag_t;
static void flag_free(flag_t * f) {
global.free(f->long_name);
global.free(f);
}
export typedef struct {
void * cb;
void * ctx;
char * name;
const char * description;
} cmd_t;
static void cmd_free(cmd_t * c) {
global.free(c->name);
global.free(c);
}
export typedef struct {
hash_t * flags;
hash_t * commands;
cmd_t * default_command;
bool has_commands;
const char * usage;
const char * name;
int argc;
const char ** argv;
} cli_t as t;
export typedef int (*cmd_cb)(cli_t * cli, char * cmd, void * ctx);
export cli_t * new(const char * usage) {
cli_t * cli = calloc(1, sizeof(cli_t));
cli->flags = hash_new();
cli->commands = hash_new();
cli->usage = usage;
return cli;
}
static void flag(cli_t * cli, void * dest, enum flag_type type, flag_options options) {
flag_t * f = calloc(1, sizeof(flag_t));
f->type = type;
f->dest = dest;
f->required = options.required;
f->description = options.description;
f->long_name = malloc(strlen(options.long_name) + 3);
strcpy(f->long_name, "--");
strcat(f->long_name, options.long_name);
hash_set(cli->flags, f->long_name, f);
if (options.short_name == NULL) {
f->short_name = NULL;
} else {
f->short_name_buf[0] = '-';
f->short_name_buf[1] = options.short_name[0];
f->short_name_buf[2] = 0;
f->short_name = f->short_name_buf;
hash_set(cli->flags, f->short_name, f);
}
}
export void flag_bool(cli_t * cli, bool * out, flag_options options) {
options.required = false;
flag(cli, out, flag_type_bool, options);
}
export void flag_int(cli_t * cli, long * out, flag_options options) {
flag(cli, out, flag_type_string, options);
}
export void flag_string(cli_t * cli, const char ** out, flag_options options) {
flag(cli, out, flag_type_string, options);
}
export void command(cli_t * cli, const char * name, cmd_cb cb, const char * description, bool is_default, void * ctx) {
cli->has_commands = true;
cmd_t * cmd = calloc(1, sizeof(cmd_t));
cmd->name = strdup(name);
cmd->cb = cb;
cmd->ctx = ctx;
cmd->description = description;
if (is_default) cli->default_command = cmd;
hash_set(cli->commands, cmd->name, cmd);
}
void parse_flag(flag_t * flag, const char * arg) {
switch(flag->type) {
case flag_type_bool:
if (flag->dest) *((bool *) flag->dest) = true;
break;
case flag_type_int:
if (flag->dest) *((long *) flag->dest) = strtol(arg, NULL, 0);
break;
case flag_type_string:
if (flag->dest) *(const char**)flag->dest = arg;
}
flag->has_value = true;
}
export int usage(cli_t * cli) {
fprintf(stderr, "usage %s: [options]%s %s\n\n", cli->name, cli->has_commands ? " command" : "", cli->usage);
{ // flags
fprintf(stderr, "FLAGS: \n\n");
int width = strlen("--help");
hash_each_key(cli->flags, {
width = strlen(key) > width ? strlen(key) : width;
});
int w = width - strlen("--help");
fprintf(stderr, " %s%*.*s %s\t%s\n", "--help", w, w, " ", "-h", "prints this message");
hash_each(cli->flags, {
if (key[1] != '-') continue;
flag_t * flag = (flag_t *) val;
int w = width - strlen(key);
fprintf(stderr, " %s%*.*s %s\t%s\n", key, w, w, " ",
flag->short_name ? flag->short_name : " ", flag->description);
})
fprintf(stderr, "\n");
}
if (cli->has_commands) {
fprintf(stderr, "COMMANDS: \n\n");
int width = 0;
hash_each_key(cli->commands, {
width = strlen(key) > width ? strlen(key) : width;
});
hash_each(cli->commands, {
cmd_t * cmd = (cmd_t *) val;
int w = width - strlen(key);
fprintf(stderr, "%s%s%*.*s %s\n",
cmd==cli->default_command ? "(default) " : " ",
cmd->name, w, w, " ", cmd->description);
})
fprintf(stderr, "\n");
}
return -1;
}
export int parse(cli_t * cli, int argc, const char ** argv) {
cli->argv = malloc(sizeof(char*) * (argc - 1));
cli->name = argv[0];
cmd_t * cmd = NULL;
int i;
// collect args, commands, flags
for ( i = 1; i < argc; i++ ) {
const char * arg = argv[i];
if (strcmp("--", arg) == 0) break;
if (strcmp("-h", arg) == 0 || strcmp("--help", arg) == 0) return usage(cli);
char * arg_allocated = strdup(arg);
flag_t * flag = (flag_t *) hash_get(cli->flags, arg_allocated);
if (flag != NULL) {
parse_flag(flag, arg);
global.free(arg_allocated);
continue;
}
if (arg[0] == '-') {
fprintf(stderr, "Unrecognized flag '%s'\n\n", arg);
global.free(arg_allocated);
return usage(cli);
}
if (cmd == NULL) {
cmd = (cmd_t *) hash_get(cli->commands, arg_allocated);
if (cmd) {
global.free(arg_allocated);
continue;
}
}
cli->argv[cli->argc] = arg;
cli->argc++;
global.free(arg_allocated);
}
// collect args after a --
for (; i < argc; i++ ) {
cli->argv[cli->argc] = argv[i];
cli->argc++;
}
if (cmd == NULL) cmd = cli->default_command;
if (cmd == NULL) return cli->has_commands ? usage(cli) : 0;
cmd_cb cb = cmd->cb;
int result = cb(cli, cmd->name, cmd->ctx);
if (result == 0) return result;
return usage(cli);
}
export void free(cli_t * cli) {
hash_each(cli->flags, {
if (key[0] == '-' && key[1] == '-') {
flag_free(val);
}
});
hash_free(cli->flags);
hash_each_val(cli->commands, {
cmd_free(val);
});
hash_free(cli->commands);
global.free(cli->argv);
global.free(cli);
}