32
32
typedef void (*init_func_p)(const struct my_option *option, char **variable,
35
void default_reporter(enum loglevel level, const char *format, ...);
35
extern "C" void default_reporter(enum loglevel level, const char *format, ...);
36
36
my_error_reporter my_getopt_error_reporter= &default_reporter;
38
38
static int findopt(char *optpat, uint32_t length,
67
67
char *disabled_my_option= (char*) "0";
70
This is a flag that can be set in client programs. 0 means that
71
my_getopt will not print error messages, but the client should do
75
bool my_getopt_print_errors= 1;
78
70
This is a flag that can be set in client programs. 1 means that
79
71
my_getopt will skip over options it does not know how to handle.
82
74
bool my_getopt_skip_unknown= 0;
84
void default_reporter(enum loglevel level,
85
const char *format, ...)
76
extern "C" void default_reporter(enum loglevel level, const char *format, ...)
88
79
va_start(args, format);
109
100
static getopt_get_addr_func getopt_get_addr;
111
void my_getopt_register_get_addr(getopt_get_addr_func func_addr)
113
getopt_get_addr= func_addr;
116
102
int handle_options(int *argc, char ***argv,
117
103
const struct my_option *longopts,
118
104
my_get_one_option get_one_option)
156
142
/* the argument must be in next argv */
159
if (my_getopt_print_errors)
160
my_getopt_error_reporter(ERROR_LEVEL,
161
"%s: Option '-O' requires an argument",
145
my_getopt_error_reporter(ERROR_LEVEL,
146
"%s: Option '-O' requires an argument",
163
148
return EXIT_ARGUMENT_REQUIRED;
177
if (my_getopt_print_errors)
178
my_getopt_error_reporter(ERROR_LEVEL,
179
"%s: Option '--set-variable' requires an argument",
162
my_getopt_error_reporter(ERROR_LEVEL,
163
"%s: Option '--set-variable' requires an argument",
181
165
return EXIT_ARGUMENT_REQUIRED;
188
172
/* the argument must be in next argv */
191
if (my_getopt_print_errors)
192
my_getopt_error_reporter(ERROR_LEVEL,
193
"%s: Option '--set-variable' requires an argument",
175
my_getopt_error_reporter(ERROR_LEVEL,
176
"%s: Option '--set-variable' requires an argument",
195
178
return EXIT_ARGUMENT_REQUIRED;
255
238
if (opt_found > 1)
257
if (my_getopt_print_errors)
258
my_getopt_error_reporter(ERROR_LEVEL,
259
"%s: ambiguous option '--%s-%s' (--%s-%s)",
260
my_progname, special_opt_prefix[i],
261
cur_arg, special_opt_prefix[i],
240
my_getopt_error_reporter(ERROR_LEVEL,
241
"%s: ambiguous option '--%s-%s' (--%s-%s)",
242
my_progname, special_opt_prefix[i],
243
cur_arg, special_opt_prefix[i],
263
245
return EXIT_AMBIGUOUS_OPTION;
307
if (my_getopt_print_errors)
308
my_getopt_error_reporter(option_is_loose ?
309
WARNING_LEVEL : ERROR_LEVEL,
310
"%s: unknown variable '%s'",
311
my_progname, cur_arg);
289
my_getopt_error_reporter(option_is_loose ?
290
WARNING_LEVEL : ERROR_LEVEL,
291
"%s: unknown variable '%s'",
292
my_progname, cur_arg);
312
293
if (!option_is_loose)
313
294
return EXIT_UNKNOWN_VARIABLE;
317
if (my_getopt_print_errors)
318
my_getopt_error_reporter(option_is_loose ?
319
WARNING_LEVEL : ERROR_LEVEL,
320
"%s: unknown option '--%s'",
321
my_progname, cur_arg);
298
my_getopt_error_reporter(option_is_loose ?
299
WARNING_LEVEL : ERROR_LEVEL,
300
"%s: unknown option '--%s'",
301
my_progname, cur_arg);
322
302
if (!option_is_loose)
323
303
return EXIT_UNKNOWN_OPTION;
336
if (my_getopt_print_errors)
337
my_getopt_error_reporter(ERROR_LEVEL,
338
"%s: variable prefix '%s' is not unique",
339
my_progname, opt_str);
316
my_getopt_error_reporter(ERROR_LEVEL,
317
"%s: variable prefix '%s' is not unique",
318
my_progname, opt_str);
340
319
return EXIT_VAR_PREFIX_NOT_UNIQUE;
344
if (my_getopt_print_errors)
345
my_getopt_error_reporter(ERROR_LEVEL,
346
"%s: ambiguous option '--%s' (%s, %s)",
347
my_progname, opt_str, prev_found,
323
my_getopt_error_reporter(ERROR_LEVEL,
324
"%s: ambiguous option '--%s' (%s, %s)",
325
my_progname, opt_str, prev_found,
349
327
return EXIT_AMBIGUOUS_OPTION;
352
330
if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
354
if (my_getopt_print_errors)
356
_("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
357
option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
333
_("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
334
option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
358
335
if (option_is_loose)
365
342
if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG)
367
if (my_getopt_print_errors)
368
my_getopt_error_reporter(ERROR_LEVEL,
369
"%s: option '%s' cannot take an argument",
370
my_progname, optp->name);
344
my_getopt_error_reporter(ERROR_LEVEL,
345
"%s: option '%s' cannot take an argument",
346
my_progname, optp->name);
371
347
return EXIT_NO_ARGUMENT_ALLOWED;
373
349
value= optp->var_type & GET_ASK_ADDR ?
378
354
if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL)
380
if (my_getopt_print_errors)
381
my_getopt_error_reporter(ERROR_LEVEL,
382
"%s: option '--%s' cannot take an argument",
383
my_progname, optp->name);
356
my_getopt_error_reporter(ERROR_LEVEL,
357
"%s: option '--%s' cannot take an argument",
358
my_progname, optp->name);
384
359
return EXIT_NO_ARGUMENT_ALLOWED;
386
361
if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL)
430
405
/* Check if there are more arguments after this one */
433
if (my_getopt_print_errors)
434
my_getopt_error_reporter(ERROR_LEVEL,
435
"%s: option '--%s' requires an argument",
436
my_progname, optp->name);
408
my_getopt_error_reporter(ERROR_LEVEL,
409
"%s: option '--%s' requires an argument",
410
my_progname, optp->name);
437
411
return EXIT_ARGUMENT_REQUIRED;
456
430
if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED)
458
if (my_getopt_print_errors)
460
_("%s: ERROR: Option '-%c' used, but is disabled\n"),
461
my_progname, optp->id);
433
_("%s: ERROR: Option '-%c' used, but is disabled\n"),
434
my_progname, optp->id);
462
435
return EXIT_OPTION_DISABLED;
464
437
if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL &&
490
463
/* Check if there are more arguments after this one */
493
if (my_getopt_print_errors)
494
my_getopt_error_reporter(ERROR_LEVEL,
495
"%s: option '-%c' requires an argument",
496
my_progname, optp->id);
466
my_getopt_error_reporter(ERROR_LEVEL,
467
"%s: option '-%c' requires an argument",
468
my_progname, optp->id);
497
469
return EXIT_ARGUMENT_REQUIRED;
499
471
argument= *++pos;
518
if (my_getopt_print_errors)
519
my_getopt_error_reporter(ERROR_LEVEL,
520
"%s: unknown option '-%c'",
521
my_progname, *optend);
490
my_getopt_error_reporter(ERROR_LEVEL,
491
"%s: unknown option '-%c'",
492
my_progname, *optend);
522
493
return EXIT_UNKNOWN_OPTION;