From 26525b24fb94cc715ea19ef0092de204b8376dab Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Thu, 30 Jun 2022 03:08:37 -0500 Subject: [PATCH] Return a proper error if a parameter is unknown --- include/gargoyle.h | 5 +++-- include/gargoyle/codex.h | 14 +++++++------- src/gargoyle.c | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/include/gargoyle.h b/include/gargoyle.h index 70c2da0..ceac561 100644 --- a/include/gargoyle.h +++ b/include/gargoyle.h @@ -2,10 +2,11 @@ #define __GARGOYLE_H_ #include +#include #include #include -uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags); -uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const char *prefix, uint16_t prefix_sz, char ***envh, gargoyle_flag_type flags); +uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, struct gargoyle_err *err, gargoyle_flag_type flags); +uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const char *prefix, uint16_t prefix_sz, char ***envh, struct gargoyle_err *err, gargoyle_flag_type flags); #endif diff --git a/include/gargoyle/codex.h b/include/gargoyle/codex.h index cb0ec3a..d7f146b 100644 --- a/include/gargoyle/codex.h +++ b/include/gargoyle/codex.h @@ -13,13 +13,13 @@ static const gargoyle_opt_type GARGOYLE_TYPE_ROPE = 1 << 4; typedef uint8_t gargoyle_err_type; -static const gargoyle_err_type GARGOYLE_ERR_SUCCESS = 1 << 0; -static const gargoyle_err_type GARGOYLE_ERR_UNKNOWN_OPT = 1 << 1; -static const gargoyle_err_type GARGOYLE_ERR_VALUE_REQUIRED = 1 << 2; -static const gargoyle_err_type GARGOYLE_ERR_INVALID_UINT = 1 << 3; -static const gargoyle_err_type GARGOYLE_ERR_INVALID_SINT = 1 << 4; -static const gargoyle_err_type GARGOYLE_ERR_INVALID_DBLE = 1 << 5; -static const gargoyle_err_type GARGOYLE_ERR_UNKNOWN_TYPE = 1 << 6; +static const gargoyle_err_type GARGOYLE_ERR_SUCCESS = 0; +static const gargoyle_err_type GARGOYLE_ERR_UNKNOWN_OPT = 1 << 0; +static const gargoyle_err_type GARGOYLE_ERR_VALUE_REQUIRED = 1 << 1; +static const gargoyle_err_type GARGOYLE_ERR_INVALID_UINT = 1 << 2; +static const gargoyle_err_type GARGOYLE_ERR_INVALID_SINT = 1 << 3; +static const gargoyle_err_type GARGOYLE_ERR_INVALID_DBLE = 1 << 4; +static const gargoyle_err_type GARGOYLE_ERR_UNKNOWN_TYPE = 1 << 5; typedef uint8_t gargoyle_flag_type; diff --git a/src/gargoyle.c b/src/gargoyle.c index a72c549..7d05f24 100644 --- a/src/gargoyle.c +++ b/src/gargoyle.c @@ -3,8 +3,10 @@ #include #include #include +#include +#include -uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, gargoyle_flag_type flags) { +uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv, struct gargoyle_err *err, gargoyle_flag_type flags) { if(!(flags & GARGOYLE_FLG_GREED)) { *argc -= 1; *argv += 1; @@ -14,6 +16,8 @@ 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); + while(1) { const char *arg = **argv; @@ -33,6 +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; } else { *argc -= 1; @@ -102,7 +109,7 @@ uint8_t gargoyle_digest_argv(uint16_t optc, struct gargoyle_opt *optv, int *argc return GARGOYLE_ERR_SUCCESS; } -uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const char *prefix, uint16_t prefix_sz, char ***envh, gargoyle_flag_type flags) { +uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const char *prefix, uint16_t prefix_sz, char ***envh, struct gargoyle_err *err, gargoyle_flag_type flags) { if(flags & GARGOYLE_FLG_BCASE) { flags |= GARGOYLE_FLG_ICASE; } @@ -119,6 +126,9 @@ uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const cha 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; } else { continue; @@ -138,5 +148,5 @@ uint8_t gargoyle_digest_envh(uint16_t optc, struct gargoyle_opt *optv, const cha } } - return 0; + return GARGOYLE_ERR_SUCCESS; }