The pragma
directive is non-portable:
Though supported by most modern compilers, it is not standard C.Consider using include guards:
// #pragma once#ifndef FLAGUTIL_H#define FLAGUTIL_H...#endif /* FLAGUTIL_H */
Avoid using magic numbers:
// Why 100?char name[100];
Perhaps defined a named constant:
#define MAX_NAME_LEN 100char name[MAX_NAME_LEN];
The functions that should not be visible outside of a translation unit should be declared as having internal linkage:
#if 0 // It is rather pointless to specify 'signed' here. char *flag_nextarg(signed int argc, char **argv) #else static char *flag_nextarg(int argc, char **argv) #endif
Calculate the length once:
#if 0 for(char **cur_flag = argv; cur_flag < argv + argc; cur_flag++) { if(strncmp(flag, *cur_flag, strlen(flag)) == 0 && (*(*cur_flag + strlen(flag)) == '=' || *(*cur_flag + strlen(flag)) == '\0')) { present = true; } }#else for(char **cur_flag = argv; cur_flag < argv + argc; cur_flag++) { const size_t flag_len = strlen(flag); if(strncmp(flag, *cur_flag, flag_len) == 0 && (*(*cur_flag + flag_len) == '=' || *(*cur_flag + flag_len) == '\0')) { present = true; } }#endif
Do we need to continue searching once we have found a match? Can we not simply return true
? That would also eliminate the need for present
.
Similarly, in flag_get()
, calculate the length of flag
once.
Send error messages to stderr
:
if(*flag != '-' || !(*(flag + 1))) {#if 0 printf("Invalid flag structure for flag " bold("%s") " passed to get_flag.\n", flag);#else fprintf(stderr, "Invalid flag structure for flag " bold("%s") " passed to get_flag.\n", flag);#endif return NULL;}
See: stderr
.