1 /* Command line, config file, and environment variable
2  * configuration of an application. Extends standard
3  * UNIX/POSIX/GNU getopt().
4  *
5  */
6 #ifndef eslGETOPTS_INCLUDED
7 #define eslGETOPTS_INCLUDED
8 #include "esl_config.h"
9 #include "easel.h"
10 #ifdef __cplusplus // magic to make C++ compilers happy
11 extern "C" {
12 #endif
13 /* Object: ESL_OPTIONS
14  *
15  * The application main.c defines an array of <ESL_OPTIONS> structures to
16  * define what configuration options are used. The array is
17  * terminated by a structure containing { NULL, NULL, NULL, 0, NULL,
18  * NULL, NULL, NULL} (or more simply, just 0 in all 8 fields.)
19  */
20 /*::cexcerpt::options_object::begin::*/
21 typedef struct {
22   char *name;           /* either short "-a" or long "--foo" style               */
23   int   type;           /* arg type, for type checking: (eslARG_INT, etc.)       */
24   char *defval;         /* default setting, or NULL ("default" is a C keyword)   */
25   char *envvar;         /* associated environ var ("BLASTDB"), or NULL           */
26   char *range;          /* for range checking arg: ("0<=x<=1", etc.)             */
27   char *toggle_opts;    /* comma-sep'd optlist: turn these off if this opt is on */
28   char *required_opts;  /* comma-sep'd optlist: these must also be set           */
29   char *incompat_opts;  /* comma-sep'd optlist: these must not be set            */
30   char *help;           /* help/usage string                                     */
31   int   docgrouptag;    /* integer tag for documentation groups                  */
32 } ESL_OPTIONS;
33 /*::cexcerpt::options_object::end::*/
34 
35 /* Argument types: the "type" variable in <ESL_OPTIONS>.
36  */
37 #define eslARG_NONE      0	/* option takes no argument (so, is boolean)   */
38 #define eslARG_INT       1	/* arg convertable by atoi()               <n> */
39 #define eslARG_REAL      2	/* arg convertable by atof()               <x> */
40 #define eslARG_CHAR      3	/* arg is a single character               <c> */
41 #define eslARG_STRING    4	/* unchecked arg type                      <s> */
42 #define eslARG_INFILE    5      /* input file - same as string, shown as   <f> */
43 #define eslARG_OUTFILE   6      /* output file - same as string, shown as  <f> */
44 
45 
46 
47 /* Object: ESL_GETOPTS
48  *
49  * An <ESL_GETOPTS> object is created to parse configuration
50  * from command line options, config file(s), and environment
51  * variables.
52  */
53 typedef struct {
54   const ESL_OPTIONS *opt; /* array of app-defined options              */
55   int          nopts;     /* number of options                         */
56 
57   int    argc;		  /* argc from command line                    */
58   char **argv;		  /* argv from command line                    */
59   int    optind;	  /* position in argc; eventually 1st arg idx  */
60   int    nfiles;	  /* # of cfgfiles that have been processed    */
61 
62   char **val;		  /* config'ed val for each option (as string) */
63   int   *setby;		  /* array [0..nopts-1] for who set option i   */
64   int   *valloc;          /* 0, or length of alloc for val[i]          */
65 
66   char  *optstring;	  /* internal: ptr into string of 1-char opts in argv[]          */
67   char  *spoof;	    	  /* internal allocation: ProcessSpoof() stores cmdline          */
68   char **spoof_argv;	  /* internal allocation: ProcessSpoof()'s ptrs into its cmdline */
69 
70   char  errbuf[eslERRBUFSIZE];	/* buffer for reporting user error     */
71 } ESL_GETOPTS;
72 
73 
74 /* Possible values of the <setby> variable in ESL_GETOPTS.
75  * Additionally, values of >3 also indicate a config file, in order
76  * of _ProcessConfigFile() calls (that is, setby=3 is the first
77  * config file, setby=4 is the second, etc.).
78  */
79 #define eslARG_SETBY_DEFAULT  0
80 #define eslARG_SETBY_CMDLINE  1
81 #define eslARG_SETBY_ENV      2
82 #define eslARG_SETBY_CFGFILE  3
83 
84 
85 /* The visible API.
86  */
87 extern ESL_GETOPTS *esl_getopts_Create(const ESL_OPTIONS *opt);
88 extern ESL_GETOPTS *esl_getopts_CreateDefaultApp(const ESL_OPTIONS *options, int nargs, int argc, char **argv, char *banner, char *usage);
89 extern int          esl_getopts_Reuse  (ESL_GETOPTS *g);
90 extern void         esl_getopts_Destroy(ESL_GETOPTS *g);
91 extern void         esl_getopts_Dump(FILE *ofp, ESL_GETOPTS *g);
92 
93 extern int esl_opt_ProcessConfigfile (ESL_GETOPTS *g, char *filename, FILE *fp);
94 extern int esl_opt_ProcessEnvironment(ESL_GETOPTS *g);
95 extern int esl_opt_ProcessCmdline    (ESL_GETOPTS *g, int argc, char **argv);
96 extern int esl_opt_ProcessSpoof      (ESL_GETOPTS *g, const char *cmdline);
97 extern int esl_opt_VerifyConfig      (ESL_GETOPTS *g);
98 extern int esl_opt_ArgNumber   (const ESL_GETOPTS *g);
99 extern int esl_opt_SpoofCmdline(const ESL_GETOPTS *g, char **ret_cmdline);
100 
101 extern int esl_opt_GetSetter(const ESL_GETOPTS *g, char *optname);
102 
103 extern int    esl_opt_IsDefault (const ESL_GETOPTS *g, char *optname);
104 extern int    esl_opt_IsOn      (const ESL_GETOPTS *g, char *optname);
105 extern int    esl_opt_IsUsed    (const ESL_GETOPTS *g, char *optname);
106 
107 extern int    esl_opt_GetBoolean(const ESL_GETOPTS *g, char *optname);
108 extern int    esl_opt_GetInteger(const ESL_GETOPTS *g, char *optname);
109 extern double esl_opt_GetReal   (const ESL_GETOPTS *g, char *optname);
110 extern char   esl_opt_GetChar   (const ESL_GETOPTS *g, char *optname);
111 extern char  *esl_opt_GetString (const ESL_GETOPTS *g, char *optname);
112 extern char  *esl_opt_GetArg    (const ESL_GETOPTS *g, int which);
113 
114 extern int esl_opt_DisplayHelp(FILE *ofp, const ESL_GETOPTS *go, int docgroup, int indent, int textwidth);
115 #ifdef __cplusplus // magic to make C++ compilers happy
116 }
117 #endif
118 #endif /*eslGETOPTS_INCLUDED*/
119