diff options
Diffstat (limited to 'src/include/gnunet_getopt_lib.h')
-rw-r--r-- | src/include/gnunet_getopt_lib.h | 349 |
1 files changed, 198 insertions, 151 deletions
diff --git a/src/include/gnunet_getopt_lib.h b/src/include/gnunet_getopt_lib.h index b04020a70..c2bd72340 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 | */ |
@@ -130,255 +131,301 @@ struct GNUNET_GETOPT_CommandLineOption | |||
130 | int require_argument; | 131 | int require_argument; |
131 | 132 | ||
132 | /** | 133 | /** |
134 | * Is the presence of this option mandatory? | ||
135 | */ | ||
136 | int option_mandatory; | ||
137 | |||
138 | /** | ||
133 | * Handler for the option. | 139 | * Handler for the option. |
134 | */ | 140 | */ |
135 | GNUNET_GETOPT_CommandLineOptionProcessor processor; | 141 | GNUNET_GETOPT_CommandLineOptionProcessor processor; |
136 | 142 | ||
137 | /** | 143 | /** |
144 | * Function to call on @e scls to clean up after processing all | ||
145 | * the arguments. Can be NULL. | ||
146 | */ | ||
147 | void (*cleaner)(void *cls); | ||
148 | |||
149 | /** | ||
138 | * Specific closure to pass to the processor. | 150 | * Specific closure to pass to the processor. |
139 | */ | 151 | */ |
140 | void *scls; | 152 | void *scls; |
141 | 153 | ||
142 | }; | 154 | }; |
143 | 155 | ||
156 | |||
144 | /** | 157 | /** |
145 | * Macro defining the option to print the command line | 158 | * Defining the option to print the command line |
146 | * help text (-h option). | 159 | * help text (-h option). |
147 | * | 160 | * |
148 | * @param about string with brief description of the application | 161 | * @param about string with brief description of the application |
149 | */ | 162 | */ |
150 | #define GNUNET_GETOPT_OPTION_HELP(about) \ | 163 | struct GNUNET_GETOPT_CommandLineOption |
151 | { 'h', "help", (const char *) NULL, gettext_noop("print this help"), 0, &GNUNET_GETOPT_format_help_, (void *) about } | 164 | GNUNET_GETOPT_OPTION_HELP (const char *about); |
152 | 165 | ||
153 | 166 | ||
154 | /** | 167 | /** |
155 | * Macro defining the option to print the version of | 168 | * Define the option to print the version of |
156 | * the application (-v option) | 169 | * the application (-v option) |
157 | * | 170 | * |
158 | * @param version string with the version number | 171 | * @param version string with the version number |
159 | */ | 172 | */ |
160 | #define GNUNET_GETOPT_OPTION_VERSION(version) \ | 173 | struct GNUNET_GETOPT_CommandLineOption |
161 | { 'v', "version", (const char *) NULL, gettext_noop("print the version number"), 0, &GNUNET_GETOPT_print_version_, (void *) version } | 174 | GNUNET_GETOPT_OPTION_VERSION (const char *version); |
175 | |||
162 | 176 | ||
163 | 177 | ||
164 | /** | 178 | /** |
165 | * Allow user to specify log file name (-l option) | 179 | * Allow user to specify log file name (-l option) |
166 | * | 180 | * |
167 | * @param logfn set to the name of the logfile | 181 | * @param[out] logfn set to the name of the logfile |
168 | */ | 182 | */ |
169 | #define GNUNET_GETOPT_OPTION_LOGFILE(logfn) \ | 183 | struct GNUNET_GETOPT_CommandLineOption |
170 | { 'l', "logfile", "LOGFILE", gettext_noop("configure logging to write logs to LOGFILE"), 1, &GNUNET_GETOPT_set_string, (void *) logfn } | 184 | GNUNET_GETOPT_OPTION_LOGFILE (char **logfn); |
171 | 185 | ||
172 | 186 | ||
173 | /** | 187 | /** |
174 | * Allow user to specify log level (-L option) | 188 | * Allow user to specify a string. |
175 | * | 189 | * |
176 | * @param loglev set to the log level | 190 | * @param shortName short name of the option |
191 | * @param name long name of the option | ||
192 | * @param argumentHelp help text for the option argument | ||
193 | * @param description long help text for the option | ||
194 | * @param[out] str set to the string | ||
177 | */ | 195 | */ |
178 | #define GNUNET_GETOPT_OPTION_LOGLEVEL(loglev) \ | 196 | struct GNUNET_GETOPT_CommandLineOption |
179 | { 'L', "log", "LOGLEVEL", gettext_noop("configure logging to use LOGLEVEL"), 1, &GNUNET_GETOPT_set_string, (void *) loglev } | 197 | GNUNET_GETOPT_OPTION_STRING (char shortName, |
198 | const char *name, | ||
199 | const char *argumentHelp, | ||
200 | const char *description, | ||
201 | char **str); | ||
202 | |||
203 | /** | ||
204 | * Allow user to specify a filename (automatically path expanded). | ||
205 | * | ||
206 | * @param shortName short name of the option | ||
207 | * @param name long name of the option | ||
208 | * @param argumentHelp help text for the option argument | ||
209 | * @param description long help text for the option | ||
210 | * @param[out] str set to the string | ||
211 | */ | ||
212 | struct GNUNET_GETOPT_CommandLineOption | ||
213 | GNUNET_GETOPT_OPTION_FILENAME (char shortName, | ||
214 | const char *name, | ||
215 | const char *argumentHelp, | ||
216 | const char *description, | ||
217 | char **str); | ||
180 | 218 | ||
181 | 219 | ||
182 | /** | 220 | /** |
183 | * Get number of verbose (-V) flags | 221 | * Allow user to specify a binary value using Crockford |
222 | * Base32 encoding. | ||
184 | * | 223 | * |
185 | * @param level where to store the verbosity level (should be an 'int') | 224 | * @param shortName short name of the option |
225 | * @param name long name of the option | ||
226 | * @param argumentHelp help text for the option argument | ||
227 | * @param description long help text for the option | ||
228 | * @param[out] val binary value decoded from Crockford Base32-encoded argument | ||
229 | * @param val_size size of @a val in bytes | ||
186 | */ | 230 | */ |
187 | #define GNUNET_GETOPT_OPTION_VERBOSE(level) \ | 231 | struct GNUNET_GETOPT_CommandLineOption |
188 | { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level } | 232 | GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE (char shortName, |
233 | const char *name, | ||
234 | const char *argumentHelp, | ||
235 | const char *description, | ||
236 | void *val, | ||
237 | size_t val_size); | ||
189 | 238 | ||
190 | 239 | ||
191 | /** | 240 | /** |
192 | * Get configuration file name (-c option) | 241 | * Allow user to specify a binary value using Crockford |
242 | * Base32 encoding where the size of the binary value is | ||
243 | * automatically determined from its type. | ||
193 | * | 244 | * |
194 | * @param fn set to the configuration file name | 245 | * @param shortName short name of the option |
246 | * @param name long name of the option | ||
247 | * @param argumentHelp help text for the option argument | ||
248 | * @param description long help text for the option | ||
249 | * @param[out] val binary value decoded from Crockford Base32-encoded argument; | ||
250 | * size is determined by type (sizeof (*val)). | ||
195 | */ | 251 | */ |
196 | #define GNUNET_GETOPT_OPTION_CFG_FILE(fn) \ | 252 | #define GNUNET_GETOPT_OPTION_SET_BASE32_AUTO(shortName,name,argumentHelp,description,val) \ |
197 | { 'c', "config", "FILENAME", gettext_noop("use configuration file FILENAME"), 1, &GNUNET_GETOPT_set_string, (void *) fn } | 253 | GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE(shortName,name,argumentHelp,description,val,sizeof(*val)) |
198 | 254 | ||
199 | 255 | ||
200 | /** | 256 | /** |
201 | * Marker for the end of the list of options. | 257 | * Allow user to specify a flag (which internally means setting |
258 | * an integer to 1/#GNUNET_YES/#GNUNET_OK. | ||
259 | * | ||
260 | * @param shortName short name of the option | ||
261 | * @param name long name of the option | ||
262 | * @param description long help text for the option | ||
263 | * @param[out] val set to 1 if the option is present | ||
202 | */ | 264 | */ |
203 | #define GNUNET_GETOPT_OPTION_END \ | 265 | struct GNUNET_GETOPT_CommandLineOption |
204 | { '\0', NULL, NULL, NULL, 0, NULL, NULL } | 266 | GNUNET_GETOPT_OPTION_SET_ONE (char shortName, |
267 | const char *name, | ||
268 | const char *description, | ||
269 | int *val); | ||
205 | 270 | ||
206 | 271 | ||
207 | /** | 272 | /** |
208 | * Parse the command line. | 273 | * Allow user to specify an `unsigned int`. |
209 | * | 274 | * |
210 | * @param binaryOptions Name of application with option summary | 275 | * @param shortName short name of the option |
211 | * @param allOptions defined options and handlers | 276 | * @param name long name of the option |
212 | * @param argc number of arguments in @a argv | 277 | * @param argumentHelp help text for the option argument |
213 | * @param argv actual arguments | 278 | * @param description long help text for the option |
214 | * @return index into argv with first non-option | 279 | * @param[out] val set to the value specified at the command line |
215 | * argument, or #GNUNET_SYSERR on error | ||
216 | */ | 280 | */ |
217 | int | 281 | struct GNUNET_GETOPT_CommandLineOption |
218 | GNUNET_GETOPT_run (const char *binaryOptions, | 282 | GNUNET_GETOPT_OPTION_SET_UINT (char shortName, |
219 | const struct GNUNET_GETOPT_CommandLineOption *allOptions, | 283 | const char *name, |
220 | unsigned int argc, char *const *argv); | 284 | const char *argumentHelp, |
285 | const char *description, | ||
286 | unsigned int *val); | ||
221 | 287 | ||
222 | 288 | ||
223 | /** | 289 | /** |
224 | * Set an option of type 'unsigned long long' from the command line. | 290 | * Allow user to specify an `unsigned long long`. |
225 | * A pointer to this function should be passed as part of the | ||
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 | * | 291 | * |
230 | * @param ctx command line processing context | 292 | * @param shortName short name of the option |
231 | * @param scls additional closure (will point to the 'unsigned long long') | 293 | * @param name long name of the option |
232 | * @param option name of the option | 294 | * @param argumentHelp help text for the option argument |
233 | * @param value actual value of the option as a string. | 295 | * @param description long help text for the option |
234 | * @return #GNUNET_OK if parsing the value worked | 296 | * @param[out] val set to the value specified at the command line |
235 | */ | 297 | */ |
236 | int | 298 | struct GNUNET_GETOPT_CommandLineOption |
237 | GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 299 | GNUNET_GETOPT_OPTION_SET_ULONG (char shortName, |
238 | void *scls, const char *option, const char *value); | 300 | const char *name, |
301 | const char *argumentHelp, | ||
302 | const char *description, | ||
303 | unsigned long long *val); | ||
239 | 304 | ||
240 | 305 | ||
241 | /** | 306 | /** |
242 | * Set an option of type 'struct GNUNET_TIME_Relative' from the command line. | 307 | * Allow user to specify a `struct GNUNET_TIME_Relative` |
243 | * A pointer to this function should be passed as part of the | 308 | * (using human-readable "fancy" time). |
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 | * | 309 | * |
248 | * @param ctx command line processing context | 310 | * @param shortName short name of the option |
249 | * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative') | 311 | * @param name long name of the option |
250 | * @param option name of the option | 312 | * @param argumentHelp help text for the option argument |
251 | * @param value actual value of the option as a string. | 313 | * @param description long help text for the option |
252 | * @return #GNUNET_OK if parsing the value worked | 314 | * @param[out] val set to the time specified at the command line |
253 | */ | 315 | */ |
254 | int | 316 | struct GNUNET_GETOPT_CommandLineOption |
255 | GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 317 | GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName, |
256 | void *scls, const char *option, const char *value); | 318 | const char *name, |
319 | const char *argumentHelp, | ||
320 | const char *description, | ||
321 | struct GNUNET_TIME_Relative *val); | ||
257 | 322 | ||
258 | 323 | ||
259 | /** | 324 | /** |
260 | * Set an option of type 'unsigned int' from the command line. | 325 | * Allow user to specify a `struct GNUNET_TIME_Absolute` |
261 | * A pointer to this function should be passed as part of the | 326 | * (using human-readable "fancy" time). |
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 | * | 327 | * |
266 | * @param ctx command line processing context | 328 | * @param shortName short name of the option |
267 | * @param scls additional closure (will point to the 'unsigned int') | 329 | * @param name long name of the option |
268 | * @param option name of the option | 330 | * @param argumentHelp help text for the option argument |
269 | * @param value actual value of the option as a string. | 331 | * @param description long help text for the option |
270 | * @return #GNUNET_OK if parsing the value worked | 332 | * @param[out] val set to the time specified at the command line |
271 | */ | 333 | */ |
272 | int | 334 | struct GNUNET_GETOPT_CommandLineOption |
273 | GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 335 | GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME (char shortName, |
274 | void *scls, const char *option, const char *value); | 336 | const char *name, |
337 | const char *argumentHelp, | ||
338 | const char *description, | ||
339 | struct GNUNET_TIME_Absolute *val); | ||
275 | 340 | ||
276 | 341 | ||
277 | /** | 342 | /** |
278 | * Set an option of type 'int' from the command line to 1 if the | 343 | * Increment @a val each time the option flag is given by one. |
279 | * given option is present. | ||
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 | * | 344 | * |
285 | * @param ctx command line processing context | 345 | * @param shortName short name of the option |
286 | * @param scls additional closure (will point to the `int`) | 346 | * @param name long name of the option |
287 | * @param option name of the option | 347 | * @param argumentHelp help text for the option argument |
288 | * @param value not used (NULL) | 348 | * @param description long help text for the option |
289 | * @return #GNUNET_OK (always) | 349 | * @param[out] val set to 1 if the option is present |
290 | */ | 350 | */ |
291 | int | 351 | struct GNUNET_GETOPT_CommandLineOption |
292 | GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 352 | GNUNET_GETOPT_OPTION_INCREMENT_VALUE (char shortName, |
293 | void *scls, const char *option, const char *value); | 353 | const char *name, |
354 | const char *description, | ||
355 | unsigned int *val); | ||
294 | 356 | ||
295 | 357 | ||
296 | /** | 358 | /** |
297 | * Set an option of type 'char *' from the command line. | 359 | * Define the '-L' log level option. Note that we do not check |
298 | * A pointer to this function should be passed as part of the | 360 | * that the log level is valid here. |
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 | * | 361 | * |
303 | * @param ctx command line processing context | 362 | * @param[out] level set to the log level |
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 | */ | 363 | */ |
310 | int | 364 | struct GNUNET_GETOPT_CommandLineOption |
311 | GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 365 | GNUNET_GETOPT_OPTION_LOGLEVEL (char **level); |
312 | void *scls, const char *option, const char *value); | ||
313 | 366 | ||
314 | 367 | ||
315 | /** | 368 | /** |
316 | * Set an option of type 'char *' from the command line doing fs expansion. | 369 | * Define the '-V' verbosity option. Using the option more |
317 | * A pointer to this function should be passed as part of the | 370 | * than once increments @a level each time. |
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 | * | 371 | * |
322 | * @param ctx command line processing context | 372 | * @param[out] level set to the verbosity level |
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 | */ | 373 | */ |
329 | int | 374 | struct GNUNET_GETOPT_CommandLineOption |
330 | GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 375 | GNUNET_GETOPT_OPTION_VERBOSE (unsigned int *level); |
331 | void *scls, const char *option, const char *value); | 376 | |
332 | 377 | ||
333 | /** | 378 | /** |
334 | * Set an option of type 'unsigned int' from the command line. Each | 379 | * Allow user to specify log file name (-l option) |
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 | * | 380 | * |
341 | * @param ctx command line processing context | 381 | * @param[out] logfn set to the name of the logfile |
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 | */ | 382 | */ |
347 | int | 383 | struct GNUNET_GETOPT_CommandLineOption |
348 | GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext | 384 | GNUNET_GETOPT_OPTION_LOGFILE (char **logfn); |
349 | *ctx, void *scls, const char *option, | ||
350 | const char *value); | ||
351 | 385 | ||
352 | 386 | ||
353 | /* *************** internal prototypes - use macros above! ************* */ | 387 | /** |
388 | * Allow user to specify configuration file name (-c option) | ||
389 | * | ||
390 | * @param[out] fn set to the name of the configuration file | ||
391 | */ | ||
392 | struct GNUNET_GETOPT_CommandLineOption | ||
393 | GNUNET_GETOPT_OPTION_CFG_FILE (char **fn); | ||
394 | |||
354 | 395 | ||
355 | /** | 396 | /** |
356 | * Print out details on command line options (implements --help). | 397 | * Make the given option mandatory. |
357 | * | 398 | * |
358 | * @param ctx command line processing context | 399 | * @param opt option to modify |
359 | * @param scls additional closure (points to about text) | 400 | * @return @a opt with the mandatory flag set. |
360 | * @param option name of the option | ||
361 | * @param value not used (NULL) | ||
362 | * @return #GNUNET_NO (do not continue, not an error) | ||
363 | */ | 401 | */ |
364 | int | 402 | struct GNUNET_GETOPT_CommandLineOption |
365 | GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext | 403 | GNUNET_GETOPT_OPTION_MANDATORY (struct GNUNET_GETOPT_CommandLineOption opt); |
366 | *ctx, void *scls, const char *option, | 404 | |
367 | const char *value); | ||
368 | 405 | ||
369 | /** | 406 | /** |
370 | * Print out program version (implements --version). | 407 | * Marker for the end of the list of options. |
408 | */ | ||
409 | #define GNUNET_GETOPT_OPTION_END \ | ||
410 | { '\0', NULL, NULL, NULL, 0, 0, NULL, NULL, NULL } | ||
411 | |||
412 | |||
413 | /** | ||
414 | * Parse the command line. | ||
371 | * | 415 | * |
372 | * @param ctx command line processing context | 416 | * @param binaryOptions Name of application with option summary |
373 | * @param scls additional closure (points to version string) | 417 | * @param allOptions defined options and handlers |
374 | * @param option name of the option | 418 | * @param argc number of arguments in @a argv |
375 | * @param value not used (NULL) | 419 | * @param argv actual arguments |
376 | * @return #GNUNET_NO (do not continue, not an error) | 420 | * @return index into argv with first non-option |
421 | * argument, or #GNUNET_SYSERR on error | ||
377 | */ | 422 | */ |
378 | int | 423 | int |
379 | GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext | 424 | GNUNET_GETOPT_run (const char *binaryOptions, |
380 | *ctx, void *scls, const char *option, | 425 | const struct GNUNET_GETOPT_CommandLineOption *allOptions, |
381 | const char *value); | 426 | unsigned int argc, |
427 | char *const *argv); | ||
428 | |||
382 | 429 | ||
383 | #if 0 /* keep Emacsens' auto-indent happy */ | 430 | #if 0 /* keep Emacsens' auto-indent happy */ |
384 | { | 431 | { |