Find the appropriate entry for short options

This commit is contained in:
Nick Chambers 2022-06-19 00:56:39 -05:00
parent 9dfca6f3c1
commit e7d0b2c114
4 changed files with 29 additions and 16 deletions

View File

@ -4,7 +4,6 @@
#include <stdint.h>
const uint8_t GARGOYLE_ERR_SUCCESS = 0;
const uint8_t GARGOYLE_ERR_RESERVED = 1;
const uint8_t GARGOYLE_ERR_UNKNOWN_OPT = 2;
const uint8_t GARGOYLE_ERR_UNKNOWN_OPT = 1;
#endif

View File

@ -22,5 +22,6 @@ struct gargoyle_opt {
#define GARGOYLE_EZ_OPT(brand) brand, (sizeof(brand) - 1), 0[brand] // >:)
struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *optv, const char *brand);
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem);
#endif

View File

@ -2,37 +2,40 @@
#include <stddef.h>
uint8_t gargoyle_digest(uint16_t optc, struct gargoyle_opt *optv, int *argc, char ***argv) {
uint8_t status = 1;
while(status) {
while(1) {
const char *arg = **argv;
if(!arg || *arg != '-' || !*(arg + 1)) {
status = 0;
break;
return GARGOYLE_ERR_SUCCESS;
} else if(*arg == '-' && *(arg + 1) == '-' && !*(arg + 2)) {
*argc -= 1;
*argv += 1;
status = 0;
break;
return GARGOYLE_ERR_SUCCESS;
}
struct gargoyle_opt *opt = NULL;
if(*(arg + 1) == '-') {
opt = gargoyle_find_brand(optc, optv, arg + 2);
} else {
/* loop through short opts */
}
if(!opt) {
status = GARGOYLE_ERR_UNKNOWN_OPT;
break;
if(!opt) {
return GARGOYLE_ERR_UNKNOWN_OPT;
}
} else {
const char *idx = arg + 1;
for(; *idx; idx += 1) {
opt = gargoyle_find_emblem(optc, optv, *idx);
if(!opt) {
return GARGOYLE_ERR_UNKNOWN_OPT;
}
}
}
*argc -= 1;
*argv += 1;
}
return status;
return GARGOYLE_ERR_SUCCESS;
}

View File

@ -11,3 +11,13 @@ struct gargoyle_opt *gargoyle_find_brand(uint16_t optc, struct gargoyle_opt *opt
return NULL;
}
struct gargoyle_opt *gargoyle_find_emblem(uint16_t optc, struct gargoyle_opt *optv, const char emblem) {
for(; optc; optc -= 1, optv += 1) {
if(emblem == optv->emblem) {
return optv;
}
}
return NULL;
}