1 #ifndef ARGPARSE_H_ 2 #define ARGPARSE_H_ 3 4 #include <stddef.h> 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 typedef enum OptionType { 11 OPTION_NULL, 12 OPTION_FLAG, 13 OPTION_HELP, 14 OPTION_INCREMENT, 15 OPTION_DECREMENT, 16 OPTION_INT, 17 OPTION_UINT, 18 OPTION_LONGLONG, 19 OPTION_ULONGLONG, 20 OPTION_FLOAT, 21 OPTION_STRING, 22 OPTION_USER0, 23 OPTION_USER1, 24 } OptionType; 25 26 typedef struct ArgparseOption { 27 OptionType type; 28 const char *short_name; 29 const char *long_name; 30 size_t offset; 31 int (*func)(const struct ArgparseOption *opt, void *out, const char *param, int negated); 32 const char *description; 33 } ArgparseOption; 34 35 typedef struct ArgparseCommandLine { 36 const ArgparseOption *switches; /* Terminated by OPTION_NULL. */ 37 const ArgparseOption *positional; /* Terminated by OPTION_NULL. */ 38 const char *program_name; 39 const char *summary; 40 const char *help_message; 41 } ArgparseCommandLine; 42 43 enum { 44 ARGPARSE_HELP_MESSAGE = -1, 45 ARGPARSE_INSUFFICIENT_ARGS = -2, 46 ARGPARSE_INVALID_SWITCH = -3, 47 ARGPARSE_BAD_PARAMETER = -4, 48 ARGPARSE_FATAL = -128 49 }; 50 51 /* Returns number of arguments parsed, or negative error code. */ 52 int argparse_parse(const ArgparseCommandLine *cmd, void *out, int argc, char **argv); 53 54 #ifdef __cplusplus 55 } /* extern "C" */ 56 #endif 57 58 #endif /* ARGPARSE_H_ */ 59