librnr  1.14.5
RoadNarrows Robotics Common Library 1
opts.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Standard command-line options options and parsing.
5  *
6  * Every application that uses librnr options facilities are provided with
7  * a set of built-in options (described below) to standardize RoadNarrows
8  * application interfaces.
9  *
10  * \par Built-In Options
11  * <dl>
12  *
13  * <dt> <b>-l, --log=<i>level</i></b></dt>
14  * <dd>
15  * Set logging threshold level. All logging events
16  * with priority \h_le <b><i>level</i></b> will be logged. All
17  * others will be ignored. The <b><i>level</i></b> argument is one of:
18  * <table style="border:0">
19  * <tr><td>\c 'off' or \c 0</td><td>Disable all logging.</td></tr>
20  * <tr><td>\c 'error' or \c 1</td>
21  * <td>Enable error and warning logging.</td></tr>
22  * <tr><td>\c 'diag1' or \c 2</td><td>Enable diagnostics 1 logging.</td></tr>
23  * <tr><td>\c 'diag2' or \c 3</td><td>Enable diagnostics 2 logging.</td></tr>
24  * <tr><td>\c 'diag3' or \c 4</td><td>Enable diagnostics 3 logging.</td></tr>
25  * <tr><td>\c 'diag4' or \c 5</td><td>Enable diagnostics 4 logging.</td></tr>
26  * <tr><td>\c 'diag5' or \c 6</td><td>Enable diagnostics 5 logging.</td></tr>
27  * <tr><td>\h_gt \c 6 </td><td>Enable user-defined logging.</td></tr>
28  * </table>
29  * \b DEFAULT: \c off
30  * </dd>
31  *
32  * <dt> <b>--logfile=<i>file</i></b></dt>
33  * <dd>
34  * Set log file <b><i>file</i></b>. Special <b><i>file</i></b> names:
35  * <table style="border:0">
36  * <tr><td>\c 'stderr'</td><td>log to standard error.</td></tr>
37  * <tr><td>\c 'stdout'</td><td>log to standard output.</td></tr>
38  * </table>
39  * \b DEFAULT: \c stderr
40  * </dd>
41  *
42  * <dt> <b>--log-no-color</b></dt>
43  * <dd>
44  * Disable logging with compiled ANSI color strings.<br>
45  * \b DEFAULT: \c false
46  * </dd>
47  *
48  * <dt> <b>--log-no-timestamp</b></dt>
49  * <dd>
50  * Disable logging with timestamps.<br>
51  * \b DEFAULT: \c false
52  *
53  * </dd>
54  * <dt> <b>--help</b></dt>
55  * <dd>Display this help and exit.</dd>
56  *
57  * <dt> <b>--version</b></dt>
58  * <dd>Output version information and exit.</dd>
59  *
60  * </dl>
61  *
62  * \sa
63  * Page \ref example_log under "Related Pages" for an example usage of options.
64  *
65  * \pkgsynopsis
66  * RoadNarrows Robotics Common Library 1
67  *
68  * \pkgcomponent{Library}
69  * librnr
70  *
71  * \pkgfile{rnr/opts.h}
72  *
73  * \author Robin Knight (robin.knight@roadnarrows.com)
74  *
75  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
76  *
77  * \license{MIT}
78  *
79  * \EulaBegin
80  * See the README and EULA files for any copyright and licensing information.
81  * \EulaEnd
82  */
83 ////////////////////////////////////////////////////////////////////////////////
84 
85 #ifndef _RNR_OPTS_H
86 #define _RNR_OPTS_H
87 
88 #include <limits.h>
89 #include <getopt.h>
90 
91 #include "rnr/rnrconfig.h"
92 #include "rnr/pkg.h"
93 
95 
96 //
97 // No short option
98 //
99 #define OPTS_NO_SHORT 0 ///< no short option equivalent
100 
101 //
102 // Reserved short and long option return values.
103 //
104 #define OPTS_RVAL_ERROR '?' ///< options error
105 #define OPTS_RVAL_LOG 'l' ///< -l, --log return value
106 #define OPTS_RVAL_LONG (CHAR_MAX + 1) ///< long return value start
107 #define OPTS_RVAL_HELP (OPTS_RVAL_LONG) ///< --help return value
108 #define OPTS_RVAL_VERSION (OPTS_RVAL_LONG + 1) ///< --version return value
109 #define OPTS_RVAL_LOGFILE (OPTS_RVAL_LONG + 2) ///< --logfile return value
110 #define OPTS_RVAL_LOGNOCOLOR (OPTS_RVAL_LONG + 3) ///< --log-no-color rval
111 #define OPTS_RVAL_LOGNOTIME (OPTS_RVAL_LONG + 4) ///< --log-no-timestamp rval
112 #define OPTS_RVAL_USER (OPTS_RVAL_LONG + 5) ///< start of user available
113 
114 
115 /*!
116  * \brief Option Argument Conversion Function Type.
117  */
118 typedef int (*OptsCvtFunc_T)(const char *argv0, const char *sOptName,
119  char *optarg, void *pOptVal);
120 
121 /*!
122  * \brief Option Value String Formatter Type.
123  */
124 typedef char *(*OptsFmtFunc_T)(char *buf, size_t buflen, void *pOptVal);
125 
126 /*!
127  * \brief Short and Long Options Info.
128  *
129  * Short options are assumed to be a proper subset of the long options.
130  *
131  * \note The member naming convention is not RN Hungarian to accommodate
132  * readability of .name=val structure initialization.
133  */
134 typedef struct
135 {
136  /*! \brief Long option string name. */
137  const char *long_opt;
138 
139  /*! \brief Short option character.
140  *
141  * Set to \p OPTS_NO_SHORT if no short option equivalent exists.
142  */
144 
145  /*! \brief Option does [not] have an argument of type.
146  *
147  * One of: \p no_argument, \p required_argument, \p optional_argument.
148  */
149  int has_arg;
150 
151  /*! \brief Option does [not] have a default value.
152  *
153  * \note *opt_addr must hold the default at OptsGet() call.
154  */
156 
157  /*! \brief Address of option variable.
158  *
159  * The option variable holds the parsed option converted value. If there
160  * is a default value the variable must be initialized to the default.
161  */
162  void *opt_addr;
163 
164  /*! \brief Option conversion function.
165  *
166  * \sa OptsCvtArgStr(), OptsCvtArgBool(), OptsCvtArgInt(),
167  * OptsCvtArgLogLevel() for built-in conversion functions.
168  *
169  */
171 
172  /*! \brief Option value string formatting function.
173  *
174  * \sa OptsFmtStr(), OptsFmtInt(), OptsFmtBool(), OptsFmtChar(),
175  * OptsFmtLogLevel() for built-in option string formatters.
176  */
178 
179  /*! \brief Option argument name string.
180  *
181  * \note Only used in help.
182  */
183  const char *arg_name;
184 
185  /*! \brief Option description string.
186  *
187  * The option description may contain the following formatting directives:
188  * \n \p \%P - print program name (argv0)
189  * \n \p \%L - print name of Long argument
190  * \n \p \%S - print name of Short argument
191  * \n \p \%A - print name of option Argument
192  * \n \p \%D - print value of Default
193  * \n \p %% - print %
194  */
195  const char *opt_desc;
196 
197  /*! \brief Return value.
198  *
199  * Expected return value of parsed option. from \p getopt_long().
200  * If short option exist, then short option ascii value will be used.
201  *
202  * \note Internal (private) use by opts processing.
203  */
205 
206  /*! \brief Option default value in string format
207  *
208  * Converts option default using specified string formatter.
209  *
210  * \note Internal (private) use by opts processing.
211  */
212  char *pvt_opt_dft;
213 } OptsInfo_T;
214 
215 /*!
216  * \brief Program Description Strings Info Structure.
217  *
218  * Strings may use the \p "%P" (program name) formatting directive,
219  * plus \p '\\n' and \p '\\t' spacing characters.
220  *
221  * Set any string to \p NULL to ignore.
222  *
223  * \note The member naming convention is not RN Hungarian to accommodate
224  * readability of .name=val structure initialization.
225  */
226 typedef struct
227 {
228  /*! \brief Single line description string of non-option arguments.
229  *
230  * \par Example:
231  * \p "THING1 [THING2] [FILE...]"
232  */
233  const char *usage_args;
234 
235  /*! \brief Simple program synopsis string.
236  *
237  * \par Example:
238  * \p "Do THING1, maybe THING2 to FILE(s)"
239  */
240  const char *synopsis;
241 
242  /*! \brief Program full description string.
243  *
244  * \par Example:
245  * \p "By actions of %P the Cat-In-The-Hat does things.\nBad things."
246  */
247  const char *long_desc;
248 
249  /*! \brief Programs diagnostics subsection string.
250  *
251  * \par Example:
252  * \p "Exit statis is 0 if THING(s) are bad, 1 if captured."
253  */
254  const char *diagnostics;
255 } OptsPgmInfo_T;
256 
257 /*!
258  * \brief Standard bad option error message reporter
259  * \param argv0 Command name string.
260  * \param opt Option name string.
261  * \param arg Option argument name string.
262  */
263 #define OPTSBADARG(argv0, opt, arg) \
264  OptsInvalid(argv0, "Invalid '%s' argument to '%s' option.", arg, opt)
265 
266 
267 //
268 // Prototypes
269 //
270 
271 // The options parser
272 extern char **OptsGet(const char *argv0,
273  const PkgInfo_T *pPkgInfo,
274  OptsPgmInfo_T *pPgmInfo,
275  OptsInfo_T *pOptsInfo,
276  bool_t bHasLogging,
277  int *pargc, char *argv[]);
278 
279 // Standard option convsersion functions
280 extern int OptsCvtArgStr(const char *argv0, const char *sOptName,
281  char *optarg, void *pOptVal);
282 extern int OptsCvtArgBool(const char *argv0, const char *sOptName,
283  char *optarg, void *pOptVal);
284 extern int OptsCvtArgInt(const char *argv0, const char *sOptName,
285  char *optarg, void *pOptVal);
286 extern int OptsCvtArgFloat(const char *argv0, const char *sOptName,
287  char *optarg, void *pOptVal);
288 extern int OptsCvtArgLogLevel(const char *argv0, const char *sOptName,
289  char *optarg, void *pOptVal);
290 
291 // Standard option value string formatter functions
292 extern char *OptsFmtStr(char *buf, size_t buflen, void *pOptVal);
293 extern char *OptsFmtInt(char *buf, size_t buflen, void *pOptVal);
294 extern char *OptsFmtFloat(char *buf, size_t buflen, void *pOptVal);
295 extern char *OptsFmtBool(char *buf, size_t buflen, void *pOptVal);
296 extern char *OptsFmtChar(char *buf, size_t buflen, void *pOptVal);
297 extern char *OptsFmtLogLevel(char *buf, size_t buflen, void *pOptVal);
298 
299 // Error reporters
300 extern void OptsInvalid(const char *argv0, const char *sFmt, ...);
301 
303 
304 
305 #endif // _RNR_OPTS_H
int(* OptsCvtFunc_T)(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Option Argument Conversion Function Type.
Definition: opts.h:118
const char * diagnostics
Programs diagnostics subsection string.
Definition: opts.h:254
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
Definition: opts.c:1018
int short_opt
Short option character.
Definition: opts.h:143
OptsFmtFunc_T fn_fmt
Option value string formatting function.
Definition: opts.h:177
char * OptsFmtLogLevel(char *buf, size_t buflen, void *pOptVal)
Log Level option value string formatter.
Definition: opts.c:1289
Program Description Strings Info Structure.
Definition: opts.h:226
int OptsCvtArgFloat(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options float argument to double.
Definition: opts.c:1109
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
Definition: opts.c:1215
void * opt_addr
Address of option variable.
Definition: opts.h:162
RoadNarrows Robotics standard package information.
const char * long_desc
Program full description string.
Definition: opts.h:247
const char * synopsis
Simple program synopsis string.
Definition: opts.h:240
char * pvt_opt_dft
Option default value in string format.
Definition: opts.h:212
char * OptsFmtStr(char *buf, size_t buflen, void *pOptVal)
String option value string formatter.
Definition: opts.c:1191
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Definition: opts.c:1065
Definition: pkg.h:34
const char * usage_args
Single line description string of non-option arguments.
Definition: opts.h:233
const char * arg_name
Option argument name string.
Definition: opts.h:183
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
char *(* OptsFmtFunc_T)(char *buf, size_t buflen, void *pOptVal)
Option Value String Formatter Type.
Definition: opts.h:124
void OptsInvalid(const char *argv0, const char *sFmt,...)
Invalid option or option argument print and exit.
Definition: opts.c:1313
char * OptsFmtChar(char *buf, size_t buflen, void *pOptVal)
Character option value string formatter.
Definition: opts.c:1273
OptsCvtFunc_T fn_cvt
Option conversion function.
Definition: opts.h:170
int pvt_retval
Return value.
Definition: opts.h:204
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
RoadNarrows Robotics common configuration file.
char ** OptsGet(const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
Gets, validates, and sets all command line options.
Definition: opts.c:861
bool_t has_default
Option does [not] have a default value.
Definition: opts.h:155
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
int OptsCvtArgStr(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to string.
Definition: opts.c:987
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
Definition: opts.c:1247
int has_arg
Option does [not] have an argument of type.
Definition: opts.h:149
char * OptsFmtFloat(char *buf, size_t buflen, void *pOptVal)
Float option value string formatter.
Definition: opts.c:1231
const char * opt_desc
Option description string.
Definition: opts.h:195
const char * long_opt
Long option string name.
Definition: opts.h:137
Short and Long Options Info.
Definition: opts.h:134
int OptsCvtArgLogLevel(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to log threshold level.
Definition: opts.c:1151