lib/options.c File Reference

#include "common.h"
#include "hash.h"
#include "verbose.h"
#include "options.h"

Include dependency graph for options.c:

Go to the source code of this file.

Defines

#define OPTION_TYPE_MASK   (OPTION_BOOLEAN|OPTION_STRING|OPTION_INT|OPTION_FLOAT|OPTION_LONG_LONG|OPTION_DOUBLE)

Functions

static struct optionSpecmatchingOption (char *name, struct optionSpec *optionSpecs)
static void validateOption (char *name, char *val, struct optionSpec *optionSpecs)
static void parseMultiOption (struct hash *hash, char *name, char *val, struct optionSpec *spec)
static boolean parseAnOption (struct hash *hash, char *arg, struct optionSpec *optionSpecs)
static struct hashparseOptions (int *pArgc, char *argv[], boolean justFirst, struct optionSpec *optionSpecs)
hashoptionParseIntoHash (int *pArgc, char *argv[], boolean justFirst)
static void setOptions (struct hash *hash)
void optionHashSome (int *pArgc, char *argv[], boolean justFirst)
void optionHash (int *pArgc, char *argv[])
void optionInit (int *pArgc, char *argv[], struct optionSpec *optionSpecs)
static char * optGet (char *name)
char * optionVal (char *name, char *defaultVal)
int optionInt (char *name, int defaultVal)
long long optionLongLong (char *name, long long defaultVal)
float optionFloat (char *name, float defaultVal)
slNameoptionMultiVal (char *name, struct slName *defaultVal)
double optionDouble (char *name, double defaultVal)
boolean optionExists (char *name)
void optionMustExist (char *name)

Variables

static char const rcsid [] = "$Id: options.c,v 1.24 2005/12/12 04:03:40 kent Exp $"
static struct optionSpec commonOptions []
static struct hashoptions = NULL
static struct optionSpecoptionSpecification = NULL


Define Documentation

#define OPTION_TYPE_MASK   (OPTION_BOOLEAN|OPTION_STRING|OPTION_INT|OPTION_FLOAT|OPTION_LONG_LONG|OPTION_DOUBLE)

Definition at line 22 of file options.c.

Referenced by parseMultiOption(), and validateOption().


Function Documentation

static struct optionSpec* matchingOption ( char *  name,
struct optionSpec optionSpecs 
) [static, read]

Definition at line 29 of file options.c.

References optionSpec::name, optionSpecs, and sameString.

Referenced by optionVal(), parseAnOption(), and validateOption().

00032 {
00033 while (optionSpecs->name != NULL)
00034     {
00035     if (sameString(optionSpecs->name, name))
00036         return optionSpecs;
00037     optionSpecs += 1;
00038     }
00039 return NULL;
00040 }

Here is the caller graph for this function:

static char* optGet ( char *  name  )  [static]

Definition at line 291 of file options.c.

References errAbort(), hashFindVal(), and options.

Referenced by optionDouble(), optionExists(), optionFloat(), optionInt(), optionLongLong(), optionMustExist(), and optionVal().

00293 {
00294 if (options == NULL)
00295     errAbort("optGet called before optionHash");
00296 return hashFindVal(options, name);
00297 }

Here is the call graph for this function:

Here is the caller graph for this function:

double optionDouble ( char *  name,
double  defaultVal 
)

Definition at line 379 of file options.c.

References errAbort(), and optGet().

00381 {
00382 char *s = optGet(name);
00383 char *valEnd;
00384 double val;
00385 if (s == NULL)
00386     return defaultVal;
00387 
00388 val = strtod(s, &valEnd);
00389 if ((*s == '\0') || (*valEnd != '\0'))
00390     errAbort("value of -%s is not a valid double: \"%s\"", name, s);
00391 return val;
00392 }

Here is the call graph for this function:

boolean optionExists ( char *  name  ) 

Definition at line 394 of file options.c.

References optGet().

Referenced by gfClient(), logDaemonize(), main(), searchOneStrand(), and setOptions().

00396 {
00397 return optGet(name) != NULL;
00398 }

Here is the call graph for this function:

Here is the caller graph for this function:

float optionFloat ( char *  name,
float  defaultVal 
)

Definition at line 350 of file options.c.

References errAbort(), and optGet().

Referenced by main().

00352 {
00353 char *s = optGet(name);
00354 char *valEnd;
00355 float val;
00356 if (s == NULL)
00357     return defaultVal;
00358 
00359 val = strtod(s, &valEnd);
00360 if ((*s == '\0') || (*valEnd != '\0'))
00361     errAbort("value of -%s is not a valid float: \"%s\"", name, s);
00362 return val;
00363 }

Here is the call graph for this function:

Here is the caller graph for this function:

void optionHash ( int *  pArgc,
char *  argv[] 
)

Definition at line 253 of file options.c.

References FALSE, and optionHashSome().

00255                               :
00256  *      -option         words starting with dash
00257  *      option=val      words with = in the middle
00258  *      -option=val     combining the two.
00259  * The resulting hash will be keyed by the option name with the val
00260  * string for value.  For '-option' types the value is 'on'. */
00261 {
00262 optionHashSome(pArgc, argv, FALSE);
00263 }

Here is the call graph for this function:

void optionHashSome ( int *  pArgc,
char *  argv[],
boolean  justFirst 
)

Definition at line 242 of file options.c.

References options, parseOptions(), and setOptions().

Referenced by optionHash().

00245 {
00246 if (options == NULL)
00247     {
00248     struct hash *hash = parseOptions(pArgc, argv, justFirst, NULL);
00249     setOptions(hash);
00250     }
00251 }

Here is the call graph for this function:

Here is the caller graph for this function:

void optionInit ( int *  pArgc,
char *  argv[],
struct optionSpec optionSpecs 
)

Definition at line 265 of file options.c.

References FALSE, options, optionSpecification, optionSpecs, parseOptions(), and setOptions().

Referenced by main().

00267                               :
00268  *      -option         words starting with dash
00269  *      option=val      words with = in the middle
00270  *      -option=val     combining the two.
00271  * The resulting hash will be keyed by the option name with the val
00272  * string for value.  For '-option' types the value is 'on'.
00273  * The words in argv are parsed in assending order.  If a word of
00274  * "--" is encountered, argument parsing stops.
00275  * If optionSpecs is not NULL, it is an array of optionSpec that are
00276  * used to validate the options.  An option must exist in the array
00277  * and the value must be convertable to the type specified in flags.
00278  * Boolean options must no value, all other options must have one.
00279  * Array is terminated by a optionSpec with a NULL name.
00280  * If array NULL, no validation is done.
00281  */
00282 {
00283 if (options == NULL)
00284     {
00285     struct hash *hash = parseOptions(pArgc, argv, FALSE, optionSpecs);
00286     setOptions(hash);
00287     optionSpecification = optionSpecs;
00288     }
00289 }

Here is the call graph for this function:

Here is the caller graph for this function:

int optionInt ( char *  name,
int  defaultVal 
)

Definition at line 316 of file options.c.

References errAbort(), optGet(), and sameString.

Referenced by main(), and setOptions().

00319 {
00320 char *s = optGet(name);
00321 char *valEnd;
00322 int val;
00323 if (s == NULL)
00324     return defaultVal;
00325 if (sameString(s,"on"))
00326     return defaultVal;
00327 val = strtol(s, &valEnd, 10);
00328 if ((*s == '\0') || (*valEnd != '\0'))
00329     errAbort("value of -%s is not a valid integer: \"%s\"", name, s);
00330 return val;
00331 }

Here is the call graph for this function:

Here is the caller graph for this function:

long long optionLongLong ( char *  name,
long long  defaultVal 
)

Definition at line 333 of file options.c.

References errAbort(), optGet(), and sameString.

00336 {
00337 char *s = optGet(name);
00338 char *valEnd;
00339 long long val;
00340 if (s == NULL)
00341     return defaultVal;
00342 if (sameString(s,"on"))
00343     return defaultVal;
00344 val = strtoll(s, &valEnd, 10);
00345 if ((*s == '\0') || (*valEnd != '\0'))
00346     errAbort("value of -%s is not a valid long long: \"%s\"", name, s);
00347 return val;
00348 }

Here is the call graph for this function:

struct slName* optionMultiVal ( char *  name,
struct slName defaultVal 
) [read]

Definition at line 365 of file options.c.

References errAbort(), hashFindVal(), options, and optionSpecification.

00367 {
00368 struct slName *ret;
00369 if(optionSpecification == NULL)
00370     errAbort("ERROR: optionMultiVal can only be used after optionInit is called "
00371              "with a non-NULL optionSpecs");
00372 
00373 ret = hashFindVal(options, name);
00374 if (ret == NULL)
00375      ret = defaultVal;
00376 return ret;
00377 }

Here is the call graph for this function:

void optionMustExist ( char *  name  ) 

Definition at line 400 of file options.c.

References errAbort(), and optGet().

00402 {
00403 if (optGet(name) == NULL)
00404     errAbort("Missing required command line flag %s", name);
00405 }

Here is the call graph for this function:

struct hash* optionParseIntoHash ( int *  pArgc,
char *  argv[],
boolean  justFirst 
) [read]

Definition at line 218 of file options.c.

References parseOptions().

00220                                               :
00221  *      -option         words starting with dash
00222  *      option=val      words with = in the middle
00223  *      -option=val     combining the two.
00224  * The resulting hash will be keyed by the option name with the val
00225  * string for value.  For '-option' types the value is 'on'. */
00226 {
00227 return parseOptions(pArgc, argv, justFirst, NULL);
00228 }

Here is the call graph for this function:

char* optionVal ( char *  name,
char *  defaultVal 
)

Definition at line 299 of file options.c.

References errAbort(), optionSpec::flags, matchingOption(), optGet(), OPTION_MULTI, and optionSpecification.

Referenced by logDaemonize(), and main().

00301 {
00302 char *ret;
00303 /* if a optionSpec was used, make sure this option is not a multi option */
00304 if(optionSpecification != NULL) {
00305     struct optionSpec *spec = matchingOption(name, optionSpecification);
00306     if(spec != NULL && (spec->flags & OPTION_MULTI))    
00307         errAbort("ERROR: optionVal cannot be used to get the value of an OPTION_MULTI");
00308 }
00309 
00310 ret = optGet(name);
00311 if (ret == NULL)
00312      ret = defaultVal;
00313 return ret;
00314 }

Here is the call graph for this function:

Here is the caller graph for this function:

static boolean parseAnOption ( struct hash hash,
char *  arg,
struct optionSpec optionSpecs 
) [static]

Definition at line 122 of file options.c.

References FALSE, optionSpec::flags, hashAdd(), matchingOption(), name, OPTION_MULTI, optionSpecs, parseMultiOption(), startsWith(), TRUE, and validateOption().

Referenced by parseOptions().

00127 {
00128 char *name, *val;
00129 char *eqPtr = strchr(arg, '=');
00130 
00131 if (!((eqPtr != NULL) || (arg[0] == '-')))
00132     return FALSE;  /* not an option */
00133 
00134 /* A dash by itself is not an option.   It can mean
00135  * negative strand for some of the DNA oriented utilities. */
00136 if (arg[0] == '-' && (arg[1] == 0 || isspace(arg[1])))
00137     return FALSE;
00138 
00139 /* It's nice to be able to use url's in the command line, but they
00140  * may have = in them... */
00141 if (startsWith("http://", arg))
00142     return FALSE;
00143 
00144 name = arg;
00145 if (name[0] == '-')
00146     name++;
00147 if (eqPtr != NULL)
00148     {
00149     *eqPtr = '\0';
00150     val = eqPtr+1;
00151     }
00152 else
00153     val = NULL;
00154 
00155 if (optionSpecs != NULL)
00156     validateOption(name, val, optionSpecs);
00157 if (val == NULL)
00158     val = "on";
00159 if (optionSpecs == NULL)
00160     hashAdd(hash, name, val);
00161 else
00162     {
00163     struct optionSpec *spec = matchingOption(name, optionSpecs);
00164     if (spec != NULL && (spec->flags & OPTION_MULTI))    /* process multiple instances of option */
00165         parseMultiOption(hash, name, val, spec);
00166     else
00167         hashAdd(hash, name, val);
00168     }
00169 
00170 if (eqPtr != NULL)
00171     *eqPtr = '=';
00172 return TRUE;
00173 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void parseMultiOption ( struct hash hash,
char *  name,
char *  val,
struct optionSpec spec 
) [static]

Definition at line 98 of file options.c.

References errAbort(), optionSpec::flags, hashAdd(), hashFindVal(), newSlName(), OPTION_STRING, OPTION_TYPE_MASK, and slAddTail().

Referenced by parseAnOption().

00100 {
00101 struct slName *valList;
00102 switch (spec->flags & OPTION_TYPE_MASK)
00103     {
00104     case OPTION_STRING:
00105         valList = hashFindVal(hash, name);
00106         if (valList == NULL)   /* first multi option */
00107             {
00108             valList = newSlName(val);
00109             hashAdd(hash, name, valList);
00110             }
00111         else
00112             {
00113             struct slName *el = newSlName(val);
00114             slAddTail(valList, el); /* added next multi option */
00115             }
00116         break;
00117     default:
00118         errAbort("UNIMPLEMENTED: multiple instances of a non-string option is not currently implemented");
00119     }
00120 }

Here is the call graph for this function:

Here is the caller graph for this function:

static struct hash* parseOptions ( int *  pArgc,
char *  argv[],
boolean  justFirst,
struct optionSpec optionSpecs 
) [static, read]

Definition at line 176 of file options.c.

References newHash(), optionSpecs, parseAnOption(), and sameString.

Referenced by optionHashSome(), optionInit(), and optionParseIntoHash().

00179 {
00180 int i, origArgc, newArgc = 1;
00181 char **rdPt = argv+1, **wrPt = argv+1;
00182 struct hash *hash = newHash(6);
00183 
00184 origArgc = *pArgc;
00185 
00186 /* parse arguments */
00187 for (i=1; i<origArgc; ++i)
00188     {
00189     if (sameString(*rdPt, "--"))
00190         {
00191         rdPt++;
00192         i++;
00193         break;
00194         }
00195     if (!parseAnOption(hash, *rdPt, optionSpecs))
00196         {
00197         /* not an option */
00198         if (justFirst)
00199             break;
00200         *wrPt++ = *rdPt;
00201         newArgc++;
00202         }
00203     rdPt++;
00204     }
00205 
00206 /* copy any remaining positional args */
00207 for (; i<origArgc; ++i)
00208     {
00209     *wrPt++ = *rdPt++;
00210     newArgc++;
00211     }
00212 
00213 *pArgc = newArgc;
00214 *wrPt = NULL; 
00215 return hash;
00216 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void setOptions ( struct hash hash  )  [static]

Definition at line 233 of file options.c.

References optionExists(), optionInt(), options, and verboseSetLevel().

Referenced by optionHashSome(), and optionInit().

00236 {
00237 options = hash;
00238 if (optionExists("verbose"))
00239     verboseSetLevel(optionInt("verbose", 0));
00240 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void validateOption ( char *  name,
char *  val,
struct optionSpec optionSpecs 
) [static]

Definition at line 42 of file options.c.

References commonOptions, errAbort(), optionSpec::flags, matchingOption(), optionSpec::name, OPTION_BOOLEAN, OPTION_DOUBLE, OPTION_FLOAT, OPTION_INT, OPTION_LONG_LONG, OPTION_STRING, OPTION_TYPE_MASK, and optionSpecs.

Referenced by parseAnOption().

00044 {
00045 char *valEnd;
00046 struct optionSpec *optionSpec = matchingOption(name, optionSpecs);
00047 if (optionSpec == NULL)
00048     optionSpec = matchingOption(name, commonOptions);
00049 if (optionSpec == NULL)
00050     errAbort("-%s is not a valid option", name);
00051 
00052 switch (optionSpec->flags & OPTION_TYPE_MASK) {
00053 case OPTION_BOOLEAN:
00054     if (val != NULL)
00055         errAbort("boolean option -%s must not have value", name);
00056     break;
00057 case OPTION_STRING:
00058     if (val == NULL)
00059         errAbort("string option -%s must have a value", name);
00060     break;
00061 case OPTION_INT:
00062     if (val == NULL)
00063         errAbort("int option -%s must have a value", name);
00064     strtol(val, &valEnd, 10);
00065     if ((*val == '\0') || (*valEnd != '\0'))
00066         errAbort("value of -%s is not a valid integer: \"%s\"",
00067                  name, val);
00068     break;
00069 case OPTION_LONG_LONG:
00070     if (val == NULL)
00071         errAbort("int option -%s must have a value", name);
00072     strtoll(val, &valEnd, 10);
00073     if ((*val == '\0') || (*valEnd != '\0'))
00074         errAbort("value of -%s is not a valid long long: \"%s\"",
00075                  name, val);
00076     break;
00077 case OPTION_FLOAT:
00078     if (val == NULL)
00079         errAbort("float option -%s must have a value", name);
00080     strtod(val, &valEnd);
00081     if ((*val == '\0') || (*valEnd != '\0'))
00082         errAbort("value of -%s is not a valid float: \"%s\"",
00083                  name, val);
00084     break;
00085 case OPTION_DOUBLE:
00086     if (val == NULL)
00087         errAbort("double option -%s must have a value", name);
00088     strtod(val, &valEnd);
00089     if ((*val == '\0') || (*valEnd != '\0'))
00090         errAbort("value of -%s is not a valid double: \"%s\"",
00091                  name, val);
00092     break;
00093 default:
00094     errAbort("bug: invalid type in optionSpec for %s", optionSpec->name);
00095 }
00096 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct optionSpec commonOptions[] [static]

Initial value:

 {
   {"verbose", OPTION_INT},
   {NULL, 0},
}

Definition at line 24 of file options.c.

Referenced by validateOption().

struct hash* options = NULL [static]

Definition at line 230 of file options.c.

struct optionSpec* optionSpecification = NULL [static]

Definition at line 231 of file options.c.

Referenced by optionInit(), optionMultiVal(), and optionVal().

char const rcsid[] = "$Id: options.c,v 1.24 2005/12/12 04:03:40 kent Exp $" [static]

Definition at line 15 of file options.c.


Generated on Tue Dec 25 20:07:38 2007 for blat by  doxygen 1.5.2