1 #ifndef MARISA_CMDOPT_H_ 2 #define MARISA_CMDOPT_H_ 3 4 #ifdef __cplusplus 5 extern "C" { 6 #endif 7 8 typedef struct cmdopt_option_ { 9 // `name' specifies the name of this option. 10 // An array of options must be terminated with an option whose name == NULL. 11 const char *name; 12 13 // `has_name' specifies whether an option takes an argument or not. 14 // 0 specifies that this option does not have any argument. 15 // 1 specifies that this option has an argument. 16 // 2 specifies that this option may have an argument. 17 int has_arg; 18 19 // `flag' specifies an integer variable which is overwritten by cmdopt_next() 20 // with its return value. 21 int *flag; 22 23 // `val' specifies a return value of cmdopt_next(). This value is returned 24 // when cmdopt_next() finds this option. 25 int val; 26 } cmdopt_option; 27 28 typedef struct cmdopt_t_ { 29 // Command line arguments. 30 int argc; 31 char **argv; 32 33 // Option settings. 34 const cmdopt_option *longopts; 35 const char *optstring; 36 37 int optind; // Index of the next argument. 38 char *nextchar; // Next character. 39 char *optarg; // Argument of the last option. 40 int optopt; // Label of the last option. 41 char *optlong; // Long option. 42 int opterr; // Warning level (0: nothing, 1: warning, 2: all). 43 int longindex; // Index of the last long option. 44 int optnum; // Number of options. 45 } cmdopt_t; 46 47 // cmdopt_init() initializes a cmdopt_t for successive cmdopt_next()s. 48 void cmdopt_init(cmdopt_t *h, int argc, char **argv, 49 const char *optstring, const cmdopt_option *longopts); 50 51 // cmdopt_get() analyzes command line arguments and gets the next option. 52 int cmdopt_get(cmdopt_t *h); 53 54 #ifdef __cplusplus 55 } // extern "C" 56 #endif 57 58 #endif // MARISA_CMDOPT_H_ 59