static void validateOption(char *name, char *val, struct optionSpec *optionSpecs) /* validate an option against a list of values */ { char *valEnd; struct optionSpec *optionSpec = matchingOption(name, optionSpecs); if (optionSpec == NULL) optionSpec = matchingOption(name, commonOptions); if (optionSpec == NULL) errAbort("-%s is not a valid option", name); long long discardMe = 0; switch (optionSpec->flags & OPTION_TYPE_MASK) { case OPTION_BOOLEAN: if (val != NULL) errAbort("boolean option -%s must not have value", name); break; case OPTION_STRING: if (val == NULL) errAbort("string option -%s must have a value", name); break; case OPTION_INT: if (val == NULL) errAbort("int option -%s must have a value", name); discardMe = strtol(val, &valEnd, 10); if ((*val == '\0') || (*valEnd != '\0')) errAbort("value of -%s is not a valid integer: \"%s\"", name, val); break; case OPTION_LONG_LONG: if (val == NULL) errAbort("int option -%s must have a value", name); discardMe = strtoll(val, &valEnd, 10); if ((*val == '\0') || (*valEnd != '\0')) errAbort("value of -%s is not a valid long long: \"%s\"", name, val); break; case OPTION_FLOAT: if (val == NULL) errAbort("float option -%s must have a value", name); discardMe = (long long)strtod(val, &valEnd); if ((*val == '\0') || (*valEnd != '\0')) errAbort("value of -%s is not a valid float: \"%s\"", name, val); break; case OPTION_DOUBLE: if (val == NULL) errAbort("double option -%s must have a value", name); discardMe = (long long)strtod(val, &valEnd); if ((*val == '\0') || (*valEnd != '\0')) errAbort("value of -%s is not a valid double: \"%s\"", name, val); break; default: errAbort("bug: invalid type in optionSpec for %s", optionSpec->name); } }
static boolean parseAnOption(struct hash *hash, char *arg, struct optionSpec *optionSpecs) /* Parse a single option argument and add to the hash, validating if * optionSpecs is not NULL. Return TRUE if it's arg is an option argument * FALSE if it's not. */ { char *name, *val; char *eqPtr = strchr(arg, '='); if (!((eqPtr != NULL) || (arg[0] == '-'))) return FALSE; /* not an option */ /* A dash by itself is not an option. It can mean * negative strand for some of the DNA oriented utilities. */ if (arg[0] == '-' && (arg[1] == 0 || isspace(arg[1]))) return FALSE; /* It's nice to be able to use url's in the command line, but they * may have = in them... */ if (startsWith("http://", arg) || startsWith("https://", arg) || startsWith("ftp://", arg)) return FALSE; name = arg; if (name[0] == '-') name++; if (eqPtr != NULL) { *eqPtr = '\0'; val = eqPtr+1; } else val = NULL; if (optionSpecs != NULL) validateOption(name, val, optionSpecs); if (val == NULL) val = "on"; if (optionSpecs == NULL) hashAdd(hash, name, val); else { struct optionSpec *spec = matchingOption(name, optionSpecs); if (spec != NULL && (spec->flags & OPTION_MULTI)) /* process multiple instances of option */ parseMultiOption(hash, name, val, spec); else hashAdd(hash, name, val); } if (eqPtr != NULL) *eqPtr = '='; return TRUE; }
char *optionVal(char *name, char *defaultVal) /* Return named option if in options hash, otherwise default. */ { char *ret; /* if a optionSpec was used, make sure this option is not a multi option */ if(optionSpecification != NULL) { struct optionSpec *spec = matchingOption(name, optionSpecification); if(spec != NULL && (spec->flags & OPTION_MULTI)) errAbort("ERROR: optionVal cannot be used to get the value of an OPTION_MULTI"); } ret = optGet(name); if (ret == NULL) ret = defaultVal; return ret; }
static boolean parseAnOption(struct hash *hash, char *arg, struct optionSpec *optionSpecs) /* Parse a single option argument and add to the hash, validating if * optionSpecs is not NULL. Return TRUE if it's arg is an option argument * FALSE if it's not. */ { char *name, *val; char *eqPtr = strchr(arg, '='); if (!((eqPtr != NULL) || (arg[0] == '-'))) return FALSE; /* not an option */ /* A dash by itself is not an option. It can mean * negative strand for some of the DNA oriented utilities. */ if (arg[0] == '-' && (arg[1] == 0 || isspace(arg[1]))) return FALSE; /* We treat this=that as an option only if the '=' happens before any non-alphanumeric * characters. This lets us have URLs and SQL statements in the command line even though * they can have equals in them. */ if (eqPtr != NULL) { char *s, c; for (s=arg; s < eqPtr; ++s) { c = *s; if (c != '_' && c != '-' && !isalnum(c)) return FALSE; } } name = arg; if (name[0] == '-') name++; if (eqPtr != NULL) { *eqPtr = '\0'; val = eqPtr+1; } else val = NULL; if (optionSpecs != NULL) validateOption(name, val, optionSpecs); if (val == NULL) val = "on"; if (optionSpecs == NULL) hashAdd(hash, name, val); else { struct optionSpec *spec = matchingOption(name, optionSpecs); if (spec != NULL && (spec->flags & OPTION_MULTI)) /* process multiple instances of option */ parseMultiOption(hash, name, val, spec); else hashAdd(hash, name, val); } if (eqPtr != NULL) *eqPtr = '='; return TRUE; }