aboutsummaryrefslogtreecommitdiff
path: root/ls_args.h
diff options
context:
space:
mode:
authorLion Kortlepel <[email protected]>2026-01-25 16:22:45 +0000
committerLion Kortlepel <[email protected]>2026-01-25 16:22:45 +0000
commitc781f2c7a4a41fad1a7e5e66ebb258c1fc8a415a (patch)
treef56f9198b5edf96269c3fc1d13726c4f7bcc0a80 /ls_args.h
parent4ad26f81a127d7be032bbe53d8141d6bc6f20e40 (diff)
downloadargs-c781f2c7a4a41fad1a7e5e66ebb258c1fc8a415a.tar.zst
args-c781f2c7a4a41fad1a7e5e66ebb258c1fc8a415a.zip
feat: implement required arguments, add tests
bump ls_test to 1.3 for ASSERT_STR_{EQ, NEQ}
Diffstat (limited to 'ls_args.h')
-rw-r--r--ls_args.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/ls_args.h b/ls_args.h
index 38592fa..bd57e2a 100644
--- a/ls_args.h
+++ b/ls_args.h
@@ -103,6 +103,7 @@ typedef struct ls_args_arg {
ls_args_type type;
void* val_ptr;
ls_args_mode mode;
+ int found;
} ls_args_arg;
typedef struct ls_args {
@@ -325,6 +326,7 @@ end:
}
void _lsa_apply(ls_args_arg* arg, ls_args_arg** prev_arg) {
+ arg->found = 1;
switch (arg->type) {
case LS_ARGS_TYPE_BOOL:
*(int*)arg->val_ptr = 1;
@@ -403,6 +405,11 @@ int ls_args_parse(ls_args* a, int argc, char** argv) {
ls_args_arg* prev_arg = NULL;
assert(a != NULL);
assert(argv != NULL);
+ a->last_error = "Success";
+ /* set all args to not found in case this is called multiple times */
+ for (i = 0; i < (int)a->args_len; ++i) {
+ a->args[i].found = 0;
+ }
for (i = 1; i < argc; ++i) {
_lsa_parsed parsed = _lsa_parse(argv[i]);
if (prev_arg) {
@@ -462,6 +469,19 @@ int ls_args_parse(ls_args* a, int argc, char** argv) {
a->last_error = a->_allocated_error;
return 0;
}
+
+ for (i = 0; i < (int)a->args_len; ++i) {
+ if (a->args[i].mode == LS_ARGS_REQUIRED && !a->args[i].found) {
+ const size_t len = 64 + strlen(a->args[i].long_opt);
+ a->_allocated_error = LS_REALLOC(a->_allocated_error, len);
+ memset(a->_allocated_error, 0, len);
+ sprintf(a->_allocated_error, "Required argument '--%s' not found",
+ a->args[i].long_opt);
+ a->last_error = a->_allocated_error;
+ return 0;
+ }
+ }
+
return 1;
}