diff options
Diffstat (limited to 'src/include/gnunet_getopt_lib.h')
-rw-r--r-- | src/include/gnunet_getopt_lib.h | 286 |
1 files changed, 129 insertions, 157 deletions
diff --git a/src/include/gnunet_getopt_lib.h b/src/include/gnunet_getopt_lib.h index b04020a70..cd546905e 100644 --- a/src/include/gnunet_getopt_lib.h +++ b/src/include/gnunet_getopt_lib.h | |||
@@ -97,6 +97,7 @@ typedef int | |||
97 | const char *option, | 97 | const char *option, |
98 | const char *value); | 98 | const char *value); |
99 | 99 | ||
100 | |||
100 | /** | 101 | /** |
101 | * @brief Definition of a command line option. | 102 | * @brief Definition of a command line option. |
102 | */ | 103 | */ |
@@ -141,244 +142,215 @@ struct GNUNET_GETOPT_CommandLineOption | |||
141 | 142 | ||
142 | }; | 143 | }; |
143 | 144 | ||
145 | |||
144 | /** | 146 | /** |
145 | * Macro defining the option to print the command line | 147 | * Defining the option to print the command line |
146 | * help text (-h option). | 148 | * help text (-h option). |
147 | * | 149 | * |
148 | * @param about string with brief description of the application | 150 | * @param about string with brief description of the application |
149 | */ | 151 | */ |
150 | #define GNUNET_GETOPT_OPTION_HELP(about) \ | 152 | struct GNUNET_GETOPT_CommandLineOption |
151 | { 'h', "help", (const char *) NULL, gettext_noop("print this help"), 0, &GNUNET_GETOPT_format_help_, (void *) about } | 153 | GNUNET_GETOPT_OPTION_HELP (const char *about); |
152 | 154 | ||
153 | 155 | ||
154 | /** | 156 | /** |
155 | * Macro defining the option to print the version of | 157 | * Define the option to print the version of |
156 | * the application (-v option) | 158 | * the application (-v option) |
157 | * | 159 | * |
158 | * @param version string with the version number | 160 | * @param version string with the version number |
159 | */ | 161 | */ |
160 | #define GNUNET_GETOPT_OPTION_VERSION(version) \ | 162 | struct GNUNET_GETOPT_CommandLineOption |
161 | { 'v', "version", (const char *) NULL, gettext_noop("print the version number"), 0, &GNUNET_GETOPT_print_version_, (void *) version } | 163 | GNUNET_GETOPT_OPTION_VERSION (const char *version); |
164 | |||
162 | 165 | ||
163 | 166 | ||
164 | /** | 167 | /** |
165 | * Allow user to specify log file name (-l option) | 168 | * Allow user to specify log file name (-l option) |
166 | * | 169 | * |
167 | * @param logfn set to the name of the logfile | 170 | * @param[out] logfn set to the name of the logfile |
168 | */ | 171 | */ |
169 | #define GNUNET_GETOPT_OPTION_LOGFILE(logfn) \ | 172 | struct GNUNET_GETOPT_CommandLineOption |
170 | { 'l', "logfile", "LOGFILE", gettext_noop("configure logging to write logs to LOGFILE"), 1, &GNUNET_GETOPT_set_string, (void *) logfn } | 173 | GNUNET_GETOPT_OPTION_LOGFILE (char **logfn); |
171 | 174 | ||
172 | 175 | ||
173 | /** | 176 | /** |
174 | * Allow user to specify log level (-L option) | 177 | * Allow user to specify a string. |
175 | * | 178 | * |
176 | * @param loglev set to the log level | 179 | * @param shortName short name of the option |
180 | * @param name long name of the option | ||
181 | * @param argumentHelp help text for the option argument | ||
182 | * @param description long help text for the option | ||
183 | * @param[out] str set to the string | ||
177 | */ | 184 | */ |
178 | #define GNUNET_GETOPT_OPTION_LOGLEVEL(loglev) \ | 185 | struct GNUNET_GETOPT_CommandLineOption |
179 | { 'L', "log", "LOGLEVEL", gettext_noop("configure logging to use LOGLEVEL"), 1, &GNUNET_GETOPT_set_string, (void *) loglev } | 186 | GNUNET_GETOPT_OPTION_STRING (char shortName, |
180 | 187 | const char *name, | |
188 | const char *argumentHelp, | ||
189 | const char *description, | ||
190 | char **str); | ||
181 | 191 | ||
182 | /** | 192 | /** |
183 | * Get number of verbose (-V) flags | 193 | * Allow user to specify a filename (automatically path expanded). |
184 | * | 194 | * |
185 | * @param level where to store the verbosity level (should be an 'int') | 195 | * @param shortName short name of the option |
196 | * @param name long name of the option | ||
197 | * @param argumentHelp help text for the option argument | ||
198 | * @param description long help text for the option | ||
199 | * @param[out] str set to the string | ||
186 | */ | 200 | */ |
187 | #define GNUNET_GETOPT_OPTION_VERBOSE(level) \ | 201 | struct GNUNET_GETOPT_CommandLineOption |
188 | { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level } | 202 | GNUNET_GETOPT_OPTION_FILENAME (char shortName, |
203 | const char *name, | ||
204 | const char *argumentHelp, | ||
205 | const char *description, | ||
206 | char **str); | ||
189 | 207 | ||
190 | 208 | ||
191 | /** | 209 | /** |
192 | * Get configuration file name (-c option) | 210 | * Allow user to specify a flag (which internally means setting |
211 | * an integer to 1/#GNUNET_YES/#GNUNET_OK. | ||
193 | * | 212 | * |
194 | * @param fn set to the configuration file name | 213 | * @param shortName short name of the option |
214 | * @param name long name of the option | ||
215 | * @param description long help text for the option | ||
216 | * @param[out] val set to 1 if the option is present | ||
195 | */ | 217 | */ |
196 | #define GNUNET_GETOPT_OPTION_CFG_FILE(fn) \ | 218 | struct GNUNET_GETOPT_CommandLineOption |
197 | { 'c', "config", "FILENAME", gettext_noop("use configuration file FILENAME"), 1, &GNUNET_GETOPT_set_string, (void *) fn } | 219 | GNUNET_GETOPT_OPTION_SET_ONE (char shortName, |
220 | const char *name, | ||
221 | const char *description, | ||
222 | int *val); | ||
198 | 223 | ||
199 | 224 | ||
200 | /** | 225 | /** |
201 | * Marker for the end of the list of options. | 226 | * Allow user to specify an `unsigned int`. |
227 | * | ||
228 | * @param shortName short name of the option | ||
229 | * @param name long name of the option | ||
230 | * @param argumentHelp help text for the option argument | ||
231 | * @param description long help text for the option | ||
232 | * @param[out] val set to the value specified at the command line | ||
202 | */ | 233 | */ |
203 | #define GNUNET_GETOPT_OPTION_END \ | 234 | struct GNUNET_GETOPT_CommandLineOption |
204 | { '\0', NULL, NULL, NULL, 0, NULL, NULL } | 235 | GNUNET_GETOPT_OPTION_SET_UINT (char shortName, |
236 | const char *name, | ||
237 | const char *argumentHelp, | ||
238 | const char *description, | ||
239 | unsigned int *val); | ||
205 | 240 | ||
206 | 241 | ||
207 | /** | 242 | /** |
208 | * Parse the command line. | 243 | * Allow user to specify an `unsigned long long`. |
209 | * | 244 | * |
210 | * @param binaryOptions Name of application with option summary | 245 | * @param shortName short name of the option |
211 | * @param allOptions defined options and handlers | 246 | * @param name long name of the option |
212 | * @param argc number of arguments in @a argv | 247 | * @param argumentHelp help text for the option argument |
213 | * @param argv actual arguments | 248 | * @param description long help text for the option |
214 | * @return index into argv with first non-option | 249 | * @param[out] val set to the value specified at the command line |
215 | * argument, or #GNUNET_SYSERR on error | ||
216 | */ | 250 | */ |
217 | int | 251 | struct GNUNET_GETOPT_CommandLineOption |
218 | GNUNET_GETOPT_run (const char *binaryOptions, | 252 | GNUNET_GETOPT_OPTION_SET_ULONG (char shortName, |
219 | const struct GNUNET_GETOPT_CommandLineOption *allOptions, | 253 | const char *name, |
220 | unsigned int argc, char *const *argv); | 254 | const char *argumentHelp, |
255 | const char *description, | ||
256 | unsigned long long *val); | ||
221 | 257 | ||
222 | 258 | ||
223 | /** | 259 | /** |
224 | * Set an option of type 'unsigned long long' from the command line. | 260 | * Allow user to specify a `struct GNUNET_TIME_Relative` |
225 | * A pointer to this function should be passed as part of the | 261 | * (using human-readable "fancy" time). |
226 | * `struct GNUNET_GETOPT_CommandLineOption` array to initialize options | ||
227 | * of this type. It should be followed by a pointer to a value of | ||
228 | * type `unsigned long long`. | ||
229 | * | 262 | * |
230 | * @param ctx command line processing context | 263 | * @param shortName short name of the option |
231 | * @param scls additional closure (will point to the 'unsigned long long') | 264 | * @param name long name of the option |
232 | * @param option name of the option | 265 | * @param argumentHelp help text for the option argument |
233 | * @param value actual value of the option as a string. | 266 | * @param description long help text for the option |
234 | * @return #GNUNET_OK if parsing the value worked | 267 | * @param[out] val set to the time specified at the command line |
235 | */ | 268 | */ |
236 | int | 269 | struct GNUNET_GETOPT_CommandLineOption |
237 | GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 270 | GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName, |
238 | void *scls, const char *option, const char *value); | 271 | const char *name, |
272 | const char *argumentHelp, | ||
273 | const char *description, | ||
274 | struct GNUNET_TIME_Relative *val); | ||
239 | 275 | ||
240 | 276 | ||
241 | /** | 277 | /** |
242 | * Set an option of type 'struct GNUNET_TIME_Relative' from the command line. | 278 | * Increment @a val each time the option flag is given by one. |
243 | * A pointer to this function should be passed as part of the | ||
244 | * `struct GNUNET_GETOPT_CommandLineOption` array to initialize options | ||
245 | * of this type. It should be followed by a pointer to a value of | ||
246 | * type `struct GNUNET_TIME_Relative`. | ||
247 | * | 279 | * |
248 | * @param ctx command line processing context | 280 | * @param shortName short name of the option |
249 | * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative') | 281 | * @param name long name of the option |
250 | * @param option name of the option | 282 | * @param argumentHelp help text for the option argument |
251 | * @param value actual value of the option as a string. | 283 | * @param description long help text for the option |
252 | * @return #GNUNET_OK if parsing the value worked | 284 | * @param[out] val set to 1 if the option is present |
253 | */ | 285 | */ |
254 | int | 286 | struct GNUNET_GETOPT_CommandLineOption |
255 | GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 287 | GNUNET_GETOPT_OPTION_INCREMENT_VALUE (char shortName, |
256 | void *scls, const char *option, const char *value); | 288 | const char *name, |
289 | const char *description, | ||
290 | unsigned int *val); | ||
257 | 291 | ||
258 | 292 | ||
259 | /** | 293 | /** |
260 | * Set an option of type 'unsigned int' from the command line. | 294 | * Define the '-L' log level option. Note that we do not check |
261 | * A pointer to this function should be passed as part of the | 295 | * that the log level is valid here. |
262 | * `struct GNUNET_GETOPT_CommandLineOption` array to initialize options | ||
263 | * of this type. It should be followed by a pointer to a value of | ||
264 | * type `unsigned int`. | ||
265 | * | 296 | * |
266 | * @param ctx command line processing context | 297 | * @param[out] level set to the log level |
267 | * @param scls additional closure (will point to the 'unsigned int') | ||
268 | * @param option name of the option | ||
269 | * @param value actual value of the option as a string. | ||
270 | * @return #GNUNET_OK if parsing the value worked | ||
271 | */ | 298 | */ |
272 | int | 299 | struct GNUNET_GETOPT_CommandLineOption |
273 | GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 300 | GNUNET_GETOPT_OPTION_LOGLEVEL (char **level); |
274 | void *scls, const char *option, const char *value); | ||
275 | 301 | ||
276 | 302 | ||
277 | /** | 303 | /** |
278 | * Set an option of type 'int' from the command line to 1 if the | 304 | * Define the '-V' verbosity option. Using the option more |
279 | * given option is present. | 305 | * than once increments @a level each time. |
280 | * A pointer to this function should be passed as part of the | ||
281 | * `struct GNUNET_GETOPT_CommandLineOption` array to initialize options | ||
282 | * of this type. It should be followed by a pointer to a value of | ||
283 | * type `int`. | ||
284 | * | 306 | * |
285 | * @param ctx command line processing context | 307 | * @param[out] level set to the verbosity level |
286 | * @param scls additional closure (will point to the `int`) | ||
287 | * @param option name of the option | ||
288 | * @param value not used (NULL) | ||
289 | * @return #GNUNET_OK (always) | ||
290 | */ | 308 | */ |
291 | int | 309 | struct GNUNET_GETOPT_CommandLineOption |
292 | GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 310 | GNUNET_GETOPT_OPTION_VERBOSE (int *level); |
293 | void *scls, const char *option, const char *value); | ||
294 | 311 | ||
295 | 312 | ||
296 | /** | 313 | /** |
297 | * Set an option of type 'char *' from the command line. | 314 | * Allow user to specify log file name (-l option) |
298 | * A pointer to this function should be passed as part of the | ||
299 | * `struct GNUNET_GETOPT_CommandLineOption` array to initialize options | ||
300 | * of this type. It should be followed by a pointer to a value of | ||
301 | * type `char *`, which will be allocated with the requested string. | ||
302 | * | 315 | * |
303 | * @param ctx command line processing context | 316 | * @param[out] logfn set to the name of the logfile |
304 | * @param scls additional closure (will point to the `char *`, | ||
305 | * which will be allocated) | ||
306 | * @param option name of the option | ||
307 | * @param value actual value of the option (a string) | ||
308 | * @return #GNUNET_OK (always) | ||
309 | */ | 317 | */ |
310 | int | 318 | struct GNUNET_GETOPT_CommandLineOption |
311 | GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 319 | GNUNET_GETOPT_OPTION_LOGFILE (char **logfn); |
312 | void *scls, const char *option, const char *value); | ||
313 | 320 | ||
314 | 321 | ||
315 | /** | 322 | /** |
316 | * Set an option of type 'char *' from the command line doing fs expansion. | 323 | * Allow user to specify configuration file name (-c option) |
317 | * A pointer to this function should be passed as part of the | ||
318 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
319 | * of this type. It should be followed by a pointer to a value of | ||
320 | * type 'char *', which will be allocated with the requested string. | ||
321 | * | 324 | * |
322 | * @param ctx command line processing context | 325 | * @param[out] fn set to the name of the configuration file |
323 | * @param scls additional closure (will point to the 'char *', | ||
324 | * which will be allocated) | ||
325 | * @param option name of the option | ||
326 | * @param value actual value of the option (a string) | ||
327 | * @return #GNUNET_OK (always) | ||
328 | */ | 326 | */ |
329 | int | 327 | struct GNUNET_GETOPT_CommandLineOption |
330 | GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 328 | GNUNET_GETOPT_OPTION_CFG_FILE (char **fn); |
331 | void *scls, const char *option, const char *value); | 329 | |
332 | 330 | ||
333 | /** | 331 | /** |
334 | * Set an option of type 'unsigned int' from the command line. Each | 332 | * Marker for the end of the list of options. |
335 | * time the option flag is given, the value is incremented by one. | ||
336 | * A pointer to this function should be passed as part of the | ||
337 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
338 | * of this type. It should be followed by a pointer to a value of | ||
339 | * type 'int'. | ||
340 | * | ||
341 | * @param ctx command line processing context | ||
342 | * @param scls additional closure (will point to the 'int') | ||
343 | * @param option name of the option | ||
344 | * @param value not used (NULL) | ||
345 | * @return #GNUNET_OK (always) | ||
346 | */ | 333 | */ |
347 | int | 334 | #define GNUNET_GETOPT_OPTION_END \ |
348 | GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext | 335 | { '\0', NULL, NULL, NULL, 0, NULL, NULL } |
349 | *ctx, void *scls, const char *option, | ||
350 | const char *value); | ||
351 | |||
352 | 336 | ||
353 | /* *************** internal prototypes - use macros above! ************* */ | ||
354 | 337 | ||
355 | /** | 338 | /** |
356 | * Print out details on command line options (implements --help). | 339 | * Parse the command line. |
357 | * | 340 | * |
358 | * @param ctx command line processing context | 341 | * @param binaryOptions Name of application with option summary |
359 | * @param scls additional closure (points to about text) | 342 | * @param allOptions defined options and handlers |
360 | * @param option name of the option | 343 | * @param argc number of arguments in @a argv |
361 | * @param value not used (NULL) | 344 | * @param argv actual arguments |
362 | * @return #GNUNET_NO (do not continue, not an error) | 345 | * @return index into argv with first non-option |
346 | * argument, or #GNUNET_SYSERR on error | ||
363 | */ | 347 | */ |
364 | int | 348 | int |
365 | GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext | 349 | GNUNET_GETOPT_run (const char *binaryOptions, |
366 | *ctx, void *scls, const char *option, | 350 | const struct GNUNET_GETOPT_CommandLineOption *allOptions, |
367 | const char *value); | 351 | unsigned int argc, |
352 | char *const *argv); | ||
368 | 353 | ||
369 | /** | ||
370 | * Print out program version (implements --version). | ||
371 | * | ||
372 | * @param ctx command line processing context | ||
373 | * @param scls additional closure (points to version string) | ||
374 | * @param option name of the option | ||
375 | * @param value not used (NULL) | ||
376 | * @return #GNUNET_NO (do not continue, not an error) | ||
377 | */ | ||
378 | int | ||
379 | GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext | ||
380 | *ctx, void *scls, const char *option, | ||
381 | const char *value); | ||
382 | 354 | ||
383 | #if 0 /* keep Emacsens' auto-indent happy */ | 355 | #if 0 /* keep Emacsens' auto-indent happy */ |
384 | { | 356 | { |