From 3ce1743d09011304ceec8366bba8604026096fb1 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 18 Oct 2025 15:58:02 -0400 Subject: Lazily initialize args --- src/stdlib/cli.c | 18 +++++++++--------- src/stdlib/cli.h | 13 +++++-------- 2 files changed, 14 insertions(+), 17 deletions(-) (limited to 'src/stdlib') diff --git a/src/stdlib/cli.c b/src/stdlib/cli.c index 6e0a4b81..0f8bff4a 100644 --- a/src/stdlib/cli.c +++ b/src/stdlib/cli.c @@ -195,15 +195,14 @@ static bool pop_boolean_cli_flag(List_t *args, char short_flag, const char *flag } public -void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const char *version, int spec_len, - cli_arg_t spec[spec_len]) { - bool *parsed = GC_MALLOC_ATOMIC(sizeof(bool[spec_len])); +void tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const char *version, int spec_len, + cli_arg_t spec[spec_len]) { List_t args = EMPTY_LIST; for (int i = 1; i < argc; i++) { List$insert(&args, &argv[i], I(0), sizeof(const char *)); } for (int i = 0; i < spec_len; i++) { - parsed[i] = pop_cli_flag(&args, spec[i].short_flag, spec[i].name, spec[i].dest, spec[i].type); + spec[i].populated = pop_cli_flag(&args, spec[i].short_flag, spec[i].name, spec[i].dest, spec[i].type); } bool show_help = false; @@ -230,18 +229,19 @@ void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const c } for (int i = 0; i < spec_len && before_double_dash.length > 0; i++) { - if (!parsed[i]) { - parsed[i] = pop_cli_positional(&before_double_dash, spec[i].name, spec[i].dest, spec[i].type, false); + if (!spec[i].populated) { + spec[i].populated = + pop_cli_positional(&before_double_dash, spec[i].name, spec[i].dest, spec[i].type, false); } } for (int i = 0; i < spec_len && after_double_dash.length > 0; i++) { - if (!parsed[i]) { - parsed[i] = pop_cli_positional(&after_double_dash, spec[i].name, spec[i].dest, spec[i].type, true); + if (!spec[i].populated) { + spec[i].populated = pop_cli_positional(&after_double_dash, spec[i].name, spec[i].dest, spec[i].type, true); } } for (int i = 0; i < spec_len; i++) { - if (!parsed[i] && spec[i].required) print_err("Missing required flag: --", spec[i].name, "\n", usage); + if (!spec[i].populated && spec[i].required) print_err("Missing required flag: --", spec[i].name, "\n", usage); } List_t remaining_args = List$concat(before_double_dash, after_double_dash, sizeof(const char *)); diff --git a/src/stdlib/cli.h b/src/stdlib/cli.h index 596409c1..5026edb3 100644 --- a/src/stdlib/cli.h +++ b/src/stdlib/cli.h @@ -9,17 +9,14 @@ typedef struct { const char *name; - char short_flag; - bool required; - const TypeInfo_t *type; void *dest; + const TypeInfo_t *type; + char short_flag; + bool required, populated; } cli_arg_t; -void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const char *version, int spec_len, - cli_arg_t spec[spec_len]); -#define tomo_parse_args(argc, argv, usage, help, version, ...) \ - _tomo_parse_args(argc, argv, usage, help, version, sizeof((cli_arg_t[]){__VA_ARGS__}) / sizeof(cli_arg_t), \ - (cli_arg_t[]){__VA_ARGS__}) +void tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const char *version, int spec_len, + cli_arg_t spec[spec_len]); bool pop_cli_flag(List_t *args, char short_flag, const char *flag, void *dest, const TypeInfo_t *type); bool pop_cli_positional(List_t *args, const char *flag, void *dest, const TypeInfo_t *type, bool allow_dashes); -- cgit v1.2.3