Teach `gargoyle_digest_argv` to report errors intelligently

This commit is contained in:
Nick Chambers 2022-07-01 14:46:13 -05:00
parent db8c84bbe0
commit 47332f3777
1 changed files with 37 additions and 14 deletions

View File

@ -16,7 +16,7 @@ uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc
flags |= GARGOYLE_FLG_ICASE;
}
gargoyle_cpy(err->func, __func__, GARGOYLE_MAX_FUNC_SZ, GARGOYLE_FLG_FILL0);
GARGOYLE_NEW_ERR(err);
while(1) {
const char *arg = **argv;
@ -37,10 +37,9 @@ uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc
if(!opt) {
if(flags & GARGOYLE_FLG_STRCT) {
err->code = GARGOYLE_ERR_UNKNOWN_OPT;
char *eql = index(brand, '=');
snprintf(err->msg, GARGOYLE_MAX_ERR_SZ, "unknown option --%.*s", (int) (eql - brand), brand);
return GARGOYLE_ERR_UNKNOWN_OPT;
GARGOYLE_MK_ERR(err, GARGOYLE_ERR_UNKNOWN_OPT, "unknown option '--%.*s'", (int) (eql - brand), brand);
return err->code;
} else {
*argc -= 1;
*argv += 1;
@ -49,23 +48,33 @@ uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc
}
uint8_t res = 0;
const char *val = NULL;
if(opt->type & GARGOYLE_TYPE_BOOL) {
res = gargoyle_from_bool(opt, brand, "no-", 3, flags);
} else if(*(brand + opt->brand_sz) == '=') {
res = gargoyle_from_rope(opt, brand + opt->brand_sz + 1, flags);
val = brand + opt->brand_sz + 1;
res = gargoyle_from_rope(opt, val, flags);
} else {
if(!*(*argv + 1)) {
return GARGOYLE_ERR_VALUE_REQUIRED;
GARGOYLE_MK_ERR(err, GARGOYLE_ERR_VALUE_REQUIRED, "option '--%s' requires a value", opt->brand);
return err->code;
}
*argc -= 1;
*argv += 1;
res = gargoyle_from_rope(opt, **argv, flags);
val = **argv;
res = gargoyle_from_rope(opt, val, flags);
}
if(res) {
return res;
if(val) {
GARGOYLE_MK_ERR(err, res, "invalid value '%s' for option '--%s'", val, opt->brand);
} else {
GARGOYLE_MK_ERR(err, res, "invalid value for option '--%s'", opt->brand);
}
return err->code;
}
} else {
const char *idx = arg + 1;
@ -75,29 +84,43 @@ uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc
if(!opt) {
if(flags & GARGOYLE_FLG_STRCT) {
return GARGOYLE_ERR_UNKNOWN_OPT;
GARGOYLE_MK_ERR(err, GARGOYLE_ERR_UNKNOWN_OPT, "unknown option '-%c'", *idx);
return err->code;
} else {
*argc -= 1;
*argv += 1;
idx += 1;
continue;
}
}
uint8_t res = 0;
const char *val = NULL;
if(opt->type & GARGOYLE_TYPE_BOOL) {
res = gargoyle_from_bool(opt, "", "*", 1, flags);
} else if(*(idx + 1)) {
res = gargoyle_from_rope(opt, idx + 1, flags);
val = idx + 1;
res = gargoyle_from_rope(opt, val, flags);
break;
} else {
if(!*(*argv + 1)) {
return GARGOYLE_ERR_VALUE_REQUIRED;
GARGOYLE_MK_ERR(err, GARGOYLE_ERR_VALUE_REQUIRED, "option '-%c' requires a value", opt->emblem);
return err->code;
}
*argc -= 1;
*argv += 1;
res = gargoyle_from_rope(opt, **argv, flags);
val = **argv;
res = gargoyle_from_rope(opt, val, flags);
}
if(res) {
if(val) {
GARGOYLE_MK_ERR(err, res, "invalid value '%s' for option '-%c'", val, opt->emblem);
} else {
GARGOYLE_MK_ERR(err, res, "invalid value for option '-%c'", opt->emblem);
}
return err->code;
}
}
}