40 #define GOO_BOOL_TYPE 1 41 #define GOO_STRING_TYPE 2 126 if (eq_index == -1) {
132 for (
int i = 0; i < eq_index; i++) {
133 if (arg[i] ==
'\'' || arg[i] ==
'"') {
138 return &arg[eq_index];
148 for (
int i = 1; i < argc; i++) {
150 char *arg = strdup(argv[i]);
159 char *val = strdup(&eq[1]);
166 size_t last = strlen(val) - 1;
169 char *valclean = strdup(&val[1]);
191 if (!strncmp(tok,
"--", 2)) {
192 char *optname = &tok[2];
198 printf(
"Unknown option --%s\n", optname);
210 if (!strcmp(val,
"true")) {
215 if (!strcmp(val,
"false")) {
238 printf(
"Option %s requires a string argument.\n",optname);
242 if (!strncmp(tok,
"-",1) && strncmp(tok,
"--",2)) {
243 size_t len = strlen(tok);
245 for (pos = 1; pos < len; pos++) {
249 char *sopt_ptr = (
char*) &sopt;
255 if (pos==1 && isdigit(tok[pos])) {
262 printf(
"Unknown option -%c\n", tok[pos]);
284 printf(
"Ran out of arguments for option block %s\n", tok);
293 printf(
"Option -%c requires a string argument.\n", tok[pos]);
317 goo->
help = strdup(s);
326 char *sname_ptr = (
char*) &sname;
328 if (strlen(lname) < 1) {
329 fprintf (stderr,
"getopt_add_bool(): must supply option name\n");
334 fprintf (stderr,
"getopt_add_bool(): invalid option character: '%c'\n", sopt);
339 fprintf (stderr,
"getopt_add_bool(): duplicate option name: --%s\n", lname);
344 fprintf (stderr,
"getopt_add_bool(): duplicate option: -%s ('%s')\n", sname, lname);
349 goo->
sname=strdup(sname);
350 goo->
lname=strdup(lname);
351 goo->
svalue=strdup(def ?
"true" :
"false");
353 goo->
help=strdup(help);
376 char *sname_ptr = (
char*) &sname;
378 if (strlen(lname) < 1) {
379 fprintf (stderr,
"getopt_add_string(): must supply option name\n");
384 fprintf (stderr,
"getopt_add_string(): invalid option character: '%c'\n", sopt);
389 fprintf (stderr,
"getopt_add_string(): duplicate option name: --%s\n", lname);
394 fprintf (stderr,
"getopt_add_string(): duplicate option: -%s ('%s')\n", sname, lname);
399 goo->
sname=strdup(sname);
400 goo->
lname=strdup(lname);
403 goo->
help=strdup(help);
416 assert (goo != NULL);
426 char *endptr = (
char *) v;
427 long val = strtol(v, &endptr, 10);
430 fprintf (stderr,
"--%s argument: strtol failed: %s\n", lname, strerror(errno));
435 fprintf (stderr,
"--%s argument cannot be parsed as an int\n", lname);
446 int val = !strcmp(v,
"true");
456 char *endptr = (
char *) v;
457 double d = strtod (v, &endptr);
460 fprintf (stderr,
"--%s argument: strtod failed: %s\n", lname, strerror(errno));
465 fprintf (stderr,
"--%s argument cannot be parsed as a double\n", lname);
509 longwidth =
max(longwidth, (
int) strlen(goo->
lname));
512 valuewidth =
max(valuewidth, (
int) strlen(goo->
svalue));
521 if (goo->
help==NULL || strlen(goo->
help)==0)
530 if (goo->
sname[0]==0)
int zhash_get(const zhash_t *zh, const void *key, void *out_value)
double getopt_get_double(getopt_t *getopt, const char *lname)
uint32_t zhash_str_hash(const void *_a)
void getopt_add_int(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
int zhash_put(zhash_t *zh, const void *key, const void *value, void *oldkey, void *oldvalue)
void getopt_add_double(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
static int zarray_size(const zarray_t *za)
int getopt_parse(getopt_t *gopt, int argc, char *argv[], int showErrors)
void getopt_add_string(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
void getopt_option_destroy(getopt_option_t *goo)
static void zarray_destroy(zarray_t *za)
char * getopt_get_usage(getopt_t *gopt)
char * string_buffer_to_string(string_buffer_t *sb)
void getopt_add_bool(getopt_t *gopt, char sopt, const char *lname, int def, const char *help)
getopt_t * getopt_create()
static zarray_t * zarray_create(size_t el_sz)
const char * getopt_get_string(getopt_t *gopt, const char *lname)
int getopt_was_specified(getopt_t *getopt, const char *lname)
void string_buffer_appendf(string_buffer_t *sb, const char *fmt,...)
void getopt_add_spacer(getopt_t *gopt, const char *s)
string_buffer_t * string_buffer_create()
const zarray_t * getopt_get_extra_args(getopt_t *gopt)
int zhash_contains(const zhash_t *zh, const void *key)
bool str_starts_with(const char *haystack, const char *needle)
static void zarray_get(const zarray_t *za, int idx, void *p)
static void getopt_modify_string(char **str, char *newvalue)
void zhash_destroy(zhash_t *zh)
int getopt_get_bool(getopt_t *getopt, const char *lname)
int getopt_get_int(getopt_t *getopt, const char *lname)
int str_indexof(const char *haystack, const char *needle)
void getopt_destroy(getopt_t *gopt)
void string_buffer_destroy(string_buffer_t *sb)
zhash_t * zhash_create(size_t keysz, size_t valuesz, uint32_t(*hash)(const void *a), int(*equals)(const void *a, const void *b))
static char * get_arg_assignment(char *arg)
void zarray_vmap(zarray_t *za, void(*f)())
void getopt_do_usage(getopt_t *gopt)
int zhash_str_equals(const void *_a, const void *_b)
static void zarray_add(zarray_t *za, const void *p)