Return a proper error if a parameter is unknown

This commit is contained in:
Nick Chambers 2022-06-30 03:08:37 -05:00
parent 169e76a93b
commit 26525b24fb
3 changed files with 23 additions and 12 deletions

View File

@ -2,10 +2,11 @@
#define __GARGOYLE_H_
#include <gargoyle/codex.h>
#include <gargoyle/delusion.h>
#include <gargoyle/privledge.h>
#include <stdint.h>
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

View File

@ -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;

View File

@ -3,8 +3,10 @@
#include <gargoyle/sleuth.h>
#include <gargoyle/twine.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
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;
}