1 // Comman-line argument parsing
10 #include <sys/param.h>
13 #include "../config.h"
16 #include "c_strings.h"
19 #include "metamethods.h"
21 #include "optionals.h"
29 static bool pop_boolean_cli_flag(List_t *args, char short_flag, const char *flag, bool *dest) {
30 const char *no_flag = String("no-", flag);
31 for (int64_t i = 0; i < (int64_t)args->length; i++) {
32 const char *arg = *(const char **)(args->data + i * args->stride);
33 if (arg[0] == '-' && arg[1] == '-') {
35 // Case: -- (end of flags and beginning of positional args)
37 } else if (streq(arg + 2, flag)) {
40 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
42 } else if (streq(arg + 2, no_flag)) {
45 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
47 } else if (starts_with(arg + 2, flag) && arg[2 + strlen(flag)] == '=') {
48 // Case: --flag=yes|no|true|false|on|off|0|1
49 OptionalBool_t b = Bool$parse(Text$from_str(arg + 2 + strlen(flag) + 1), NULL);
50 if (b == NONE_BOOL) print_err("Invalid boolean value for flag ", flag, ": ", arg);
52 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
55 } else if (short_flag && arg[0] == '-' && arg[1] != '-' && strchr(arg + 1, short_flag)) {
56 const char *loc = strchr(arg + 1, short_flag);
58 // Case: -f=yes|no|true|false|on|off|1|0
59 OptionalBool_t b = Bool$parse(Text$from_str(loc + 2), NULL);
61 char short_str[2] = {short_flag, '\0'};
62 print_err("Invalid boolean value for flag -", short_str, ": ", arg);
66 // Case: -abcdef=... -> -abcde
67 char *remainder = String(string_slice(arg, (size_t)(loc - arg)));
68 if unlikely (args->data_refcount > 0) List$compact(args, sizeof(const char *));
69 *(const char **)(args->data + i * args->stride) = remainder;
71 // Case: -f=... -> pop flag entirely
72 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
78 if (strlen(arg) == 2) {
79 // Case: -f -> pop flag entirely
80 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
82 // Case: -abcdefgh... -> -abcdegh...
84 String(string_slice(arg, (size_t)(loc - arg)), string_slice(loc + 1, strlen(loc + 1)));
85 if unlikely (args->data_refcount > 0) List$compact(args, sizeof(const char *));
86 *(const char **)(args->data + i * args->stride) = remainder;
96 void tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const char *version, int spec_len,
97 cli_arg_t spec[spec_len]) {
98 List_t args = EMPTY_LIST;
99 for (int i = 1; i < argc; i++) {
100 List$insert(&args, &argv[i], I(0), sizeof(const char *));
103 for (int i = 0; i < spec_len; i++) {
104 spec[i].populated = pop_cli_flag(&args, spec[i].short_flag, spec[i].name, spec[i].dest, spec[i].type);
107 bool show_help = false;
108 if (pop_boolean_cli_flag(&args, 'h', "help", &show_help) && show_help) {
112 bool show_version = false;
113 if (pop_boolean_cli_flag(&args, 'v', "version", &show_version) && show_version) {
118 List_t before_double_dash = args, after_double_dash = EMPTY_LIST;
119 for (int i = 0; i < (int64_t)args.length; i++) {
120 const char *arg = *(const char **)(args.data + i * args.stride);
121 if (streq(arg, "--")) {
122 before_double_dash = List$slice(args, I(1), I(i));
123 after_double_dash = List$slice(args, I(i + 2), I(-1));
128 for (int i = 0; i < spec_len && before_double_dash.length > 0; i++) {
129 if (!spec[i].populated) {
131 pop_cli_positional(&before_double_dash, spec[i].name, spec[i].dest, spec[i].type, false);
135 for (int i = 0; i < spec_len && after_double_dash.length > 0; i++) {
136 if (!spec[i].populated) {
137 spec[i].populated = pop_cli_positional(&after_double_dash, spec[i].name, spec[i].dest, spec[i].type, true);
141 for (int i = 0; i < spec_len; i++) {
142 if (!spec[i].populated && spec[i].required) print_err("Missing required flag: ", spec[i].name, "\n", usage);
145 List_t remaining_args = List$concat(before_double_dash, after_double_dash, sizeof(const char *));
146 if (remaining_args.length > 0) {
147 print_err("Unknown flag values: ", CString$join(" ", remaining_args));
151 static List_t parse_arg_list(List_t args, const char *flag, void *dest, const TypeInfo_t *type, bool allow_dashes) {
152 if (type->tag == ListInfo) {
153 void *item = type->ListInfo.item->size ? GC_MALLOC((size_t)type->ListInfo.item->size) : NULL;
154 while (args.length > 0) {
155 const char *arg = *(const char **)args.data;
156 if (arg[0] == '-' && !allow_dashes) break;
157 args = parse_arg_list(args, flag, item, type->ListInfo.item, allow_dashes);
158 List$insert(dest, item, I(0), type->ListInfo.item->size);
161 } else if (type->tag == TableInfo) {
162 // Arguments take the form key:value
163 void *key = type->TableInfo.key->size ? GC_MALLOC((size_t)type->TableInfo.key->size) : NULL;
164 void *value = type->TableInfo.value->size ? GC_MALLOC((size_t)type->TableInfo.value->size) : NULL;
165 while (args.length > 0) {
166 const char *arg = *(const char **)args.data;
167 if (arg[0] == '-' && !allow_dashes) break;
168 if (type->TableInfo.value->size == 0) {
169 List_t key_arg = List(arg);
170 (void)parse_arg_list(key_arg, flag, key, type->TableInfo.key, allow_dashes);
171 Table$set(dest, key, NULL, type);
172 args = List$from(args, I(2));
174 const char *colon = strchr(arg, ':');
176 List_t key_arg = List(String(string_slice(arg, (size_t)(colon - arg))));
177 (void)parse_arg_list(key_arg, flag, key, type->TableInfo.key, allow_dashes);
178 List_t value_arg = List(colon + 1);
179 (void)parse_arg_list(value_arg, flag, value, type->TableInfo.value, allow_dashes);
180 Table$set(dest, key, value, type);
181 args = List$from(args, I(2));
185 } else if (type->tag == StructInfo) {
186 for (int i = 0; i < type->StructInfo.num_fields; i++) {
187 const TypeInfo_t *field_type = type->StructInfo.fields[i].type;
188 if (field_type->align > 0 && (size_t)dest % (size_t)field_type->align > 0)
189 dest += (size_t)field_type->align - ((size_t)dest % (size_t)field_type->align);
190 args = parse_arg_list(args, String(flag, ".", type->StructInfo.fields[i].name), dest, field_type,
192 dest += field_type->size;
197 if (args.length == 0) print_err("No value provided for flag: ", flag);
199 const char *arg = *(const char **)args.data;
202 if ((type->tag == TextInfo || type == &CString$info) && arg[0] == '\\' && arg[1] == '-') {
204 } else if (arg[0] == '-') {
205 print_err("Not a valid flag: ", arg);
209 if (type->tag == OptionalInfo) {
210 const TypeInfo_t *nonnull = type->OptionalInfo.type;
211 if (streq(arg, "none")) {
212 if (nonnull == &Num$info) *(double *)dest = (double)NAN;
213 else if (nonnull == &Num32$info) *(float *)dest = (float)NAN;
214 else memset(dest, 0, (size_t)type->size);
215 return List$from(args, I(2));
217 args = parse_arg_list(args, flag, dest, nonnull, allow_dashes);
218 if (nonnull == &Int$info || nonnull == &Path$info || nonnull == &Num$info || nonnull == &Num32$info
219 || nonnull->tag == TextInfo || nonnull->tag == EnumInfo)
221 else if (nonnull == &Int64$info) ((OptionalInt64_t *)dest)->has_value = true;
222 else if (nonnull == &Int32$info) ((OptionalInt32_t *)dest)->has_value = true;
223 else if (nonnull == &Int16$info) ((OptionalInt16_t *)dest)->has_value = true;
224 else if (nonnull == &Int8$info) ((OptionalInt8_t *)dest)->has_value = true;
225 else if (nonnull == &Byte$info) ((OptionalByte_t *)dest)->has_value = true;
226 else if (nonnull->tag == StructInfo && nonnull != &Path$info) *(bool *)(dest + nonnull->size) = true;
227 else print_err("Unsupported type: ", generic_as_text(NULL, true, nonnull));
232 List_t rest_of_args = List$from(args, I(2));
234 if (type == &CString$info) {
235 *(const char **)dest = arg;
236 } else if (type == &Int$info) {
237 OptionalInt_t parsed = Int$from_str(arg);
238 if (parsed.small == 0) print_err("Could not parse argument for ", flag, ": ", arg);
239 *(Int_t *)dest = parsed;
240 } else if (type == &Int64$info) {
241 OptionalInt64_t parsed = Int64$parse(Text$from_str(arg), NONE_INT, NULL);
242 if (!parsed.has_value) print_err("Could not parse argument for ", flag, ": ", arg);
243 *(Int64_t *)dest = parsed.value;
244 } else if (type == &Int32$info) {
245 OptionalInt32_t parsed = Int32$parse(Text$from_str(arg), NONE_INT, NULL);
246 if (!parsed.has_value) print_err("Could not parse argument for ", flag, ": ", arg);
247 *(Int32_t *)dest = parsed.value;
248 } else if (type == &Int16$info) {
249 OptionalInt16_t parsed = Int16$parse(Text$from_str(arg), NONE_INT, NULL);
250 if (!parsed.has_value) print_err("Could not parse argument for ", flag, ": ", arg);
251 *(Int16_t *)dest = parsed.value;
252 } else if (type == &Int8$info) {
253 OptionalInt8_t parsed = Int8$parse(Text$from_str(arg), NONE_INT, NULL);
254 if (!parsed.has_value) print_err("Could not parse argument for ", flag, ": ", arg);
255 *(Int8_t *)dest = parsed.value;
256 } else if (type == &Byte$info) {
257 OptionalByte_t parsed = Byte$parse(Text$from_str(arg), NONE_INT, NULL);
258 if (!parsed.has_value) print_err("Could not parse argument for ", flag, ": ", arg);
259 *(Byte_t *)dest = parsed.value;
260 } else if (type == &Bool$info) {
261 OptionalBool_t parsed = Bool$parse(Text$from_str(arg), NULL);
262 if (parsed == NONE_BOOL) print_err("Could not parse argument for ", flag, ": ", arg);
263 *(Bool_t *)dest = parsed;
264 } else if (type == &Num$info) {
265 OptionalNum_t parsed = Num$parse(Text$from_str(arg), NULL);
266 if (isnan(parsed)) print_err("Could not parse argument for ", flag, ": ", arg);
267 *(Num_t *)dest = parsed;
268 } else if (type == &Num32$info) {
269 OptionalNum32_t parsed = Num32$parse(Text$from_str(arg), NULL);
270 if (isnan(parsed)) print_err("Could not parse argument for ", flag, ": ", arg);
271 *(Num32_t *)dest = parsed;
272 } else if (type->tag == PointerInfo) {
273 // For pointers, we can just allocate memory for the value and then parse the value
274 void *value = GC_MALLOC((size_t)type->PointerInfo.pointed->size);
275 args = parse_arg_list(args, flag, value, type->PointerInfo.pointed, allow_dashes);
276 *(void **)dest = value;
278 } else if (type == &Path$info) {
279 *(Path_t *)dest = Path$from_str(arg);
280 } else if (type->tag == TextInfo) {
281 *(Text_t *)dest = Text$from_str(arg);
282 } else if (type->tag == EnumInfo) {
283 List_t tag_names = EMPTY_LIST;
284 for (int t = 0; t < type->EnumInfo.num_tags; t++) {
285 NamedType_t named = type->EnumInfo.tags[t];
286 Text_t name_text = Text$from_str(named.name);
287 List$insert(&tag_names, &name_text, I(0), sizeof(name_text));
288 size_t len = strlen(named.name);
289 if (strncmp(arg, named.name, len) == 0 && (arg[len] == '\0' || arg[len] == ':')) {
290 *(int32_t *)dest = (t + 1);
292 // Simple tag (no associated data):
293 if (!named.type || (named.type->tag == StructInfo && named.type->StructInfo.num_fields == 0))
296 dest += sizeof(int32_t);
298 if (named.type->align > 0 && (size_t)dest % (size_t)named.type->align > 0)
299 dest += (size_t)named.type->align - ((size_t)dest % (size_t)named.type->align);
301 return parse_arg_list(rest_of_args, String(flag, ".", named.name), dest, named.type, allow_dashes);
304 print_err("Invalid enum name for ", type->EnumInfo.name, ": ", arg,
305 "\nValid names are: ", Text$join(Text(", "), tag_names));
307 Text_t t = generic_as_text(NULL, false, type);
308 print_err("Unsupported type for argument parsing: ", t);
313 bool pop_cli_flag(List_t *args, char short_flag, const char *flag, void *dest, const TypeInfo_t *type) {
314 if (type == &Bool$info) {
315 return pop_boolean_cli_flag(args, short_flag, flag, dest);
318 for (int64_t i = 0; i < (int64_t)args->length; i++) {
319 const char *arg = *(const char **)(args->data + i * args->stride);
320 if (arg[0] == '-' && arg[1] == '-') {
321 if (arg[2] == '\0') {
322 // Case: -- (end of flags and beginning of positional args)
324 } else if (streq(arg + 2, flag)) {
325 // Case: --flag values...
326 if (i + 1 >= (int64_t)args->length) print_err("No value provided for flag: ", flag);
327 List_t values = List$slice(*args, I(i + 2), I(-1));
328 List_t remaining_args = parse_arg_list(values, flag, dest, type, false);
329 *args = List$concat(List$to(*args, I(i)), remaining_args, sizeof(const char *));
331 } else if (starts_with(arg + 2, flag) && arg[2 + strlen(flag)] == '=') {
333 const char *arg_value = arg + 2 + strlen(flag) + 1;
335 if (type->tag == ListInfo || type->tag == TableInfo) {
336 // For lists and tables, --flag=a,b,c or --flag=a:1,b:2,c:3
337 List_t texts = Text$split(Text$from_str(arg_value), Text(","));
339 for (int64_t j = 0; j < (int64_t)texts.length; j++)
340 List$insert_value(&texts, Text$as_c_string(*(Text_t *)(texts.data + j * texts.stride)), I(0),
341 sizeof(const char *));
343 values = List(arg_value);
345 List_t remaining_args = parse_arg_list(values, flag, dest, type, false);
346 *args = List$concat(List$to(*args, I(i)), remaining_args, sizeof(const char *));
349 } else if (short_flag && arg[0] == '-' && arg[1] != '-' && strchr(arg + 1, short_flag)) {
350 const char *loc = strchr(arg + 1, short_flag);
351 char short_str[2] = {short_flag, '\0'};
354 const char *arg_value = loc + 2;
356 if (type->tag == ListInfo || type->tag == TableInfo) {
357 // For lists and tables, -f=a,b,c or -f=a:1,b:2,c:3
358 List_t texts = Text$split(Text$from_str(arg_value), Text(","));
360 for (int64_t j = 0; j < (int64_t)texts.length; j++)
361 List$insert_value(&texts, Text$as_c_string(*(Text_t *)(texts.data + j * texts.stride)), I(0),
362 sizeof(const char *));
365 values = List(arg_value);
367 values = parse_arg_list(values, flag, dest, type, false);
370 // Case: -abcdef=... -> -abcde
371 char *remainder = String(string_slice(arg, (size_t)(loc - arg)));
372 if unlikely (args->data_refcount > 0) List$compact(args, sizeof(const char *));
373 *(const char **)(args->data + i * args->stride) = remainder;
375 // Case: -f=... -> pop flag entirely
376 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
379 } else if (loc[1] == '\0') {
380 // Case: -...f value...
381 if (i + 1 >= (int64_t)args->length) print_err("No value provided for flag: -", short_str);
382 List_t values = List$slice(*args, I(i + 2), I(-1));
383 List_t remaining_values = parse_arg_list(values, flag, dest, type, false);
384 if (loc == arg + 1) {
385 // Case: -f values...
386 *args = List$concat(List$to(*args, I(i)), remaining_values, sizeof(const char *));
388 // Case: -abcdef values... -> -abcde
389 char *remainder = String(string_slice(arg, (size_t)(loc - arg)));
390 if unlikely (args->data_refcount > 0) List$compact(args, sizeof(const char *));
391 *args = List$concat(List$to(*args, I(i)),
392 List$concat(List(remainder), remaining_values, sizeof(const char *)),
393 sizeof(const char *));
397 // Case: -...fVALUE (e.g. -O3)
398 const char *arg_value = loc + 1;
400 if (type->tag == ListInfo || type->tag == TableInfo) {
401 // For lists and tables, -fa,b,c or -fa:1,b:2,c:3
402 List_t texts = Text$split(Text$from_str(arg_value), Text(","));
404 for (int64_t j = 0; j < (int64_t)texts.length; j++)
405 List$insert_value(&values, Text$as_c_string(*(Text_t *)(texts.data + j * texts.stride)), I(0),
406 sizeof(const char *));
409 values = List(arg_value);
411 (void)parse_arg_list(values, flag, dest, type, false);
413 // Case: -abcdefVALUE -> -abcde;
414 // NOTE: adding a semicolon means that `-ab1 2` won't parse as b=1, then a=2
415 char *remainder = String(string_slice(arg, (size_t)(loc - arg)), ";");
416 if unlikely (args->data_refcount > 0) List$compact(args, sizeof(const char *));
417 *(const char **)(args->data + i * args->stride) = remainder;
419 // Case: -fVALUE -> pop flag entirely
420 List$remove_at(args, I(i + 1), I(1), sizeof(const char *));
429 bool pop_cli_positional(List_t *args, const char *flag, void *dest, const TypeInfo_t *type, bool allow_dashes) {
430 if (args->length == 0) {
431 print_err("No value provided for flag: ", flag);
434 *args = parse_arg_list(*args, flag, dest, type, allow_dashes);