28
28
const char *gengetopt_args_info_description = "";
30
30
const char *gengetopt_args_info_help[] = {
31
" -h, --height height",
32
" -V, --vavoom vavoom instead of version",
31
" --help Print help and exit",
32
" --version Print version and exit",
33
" -h, --height height",
34
" -V, --vavoom vavoom instead of version",
39
} test_redef_help_cmd_parser_arg_type;
37
42
void clear_given (struct gengetopt_args_info *args_info);
63
70
void init_args_info(struct gengetopt_args_info *args_info)
65
args_info->height_help = gengetopt_args_info_help[0] ;
66
args_info->vavoom_help = gengetopt_args_info_help[1] ;
74
args_info->help_help = gengetopt_args_info_help[0] ;
75
args_info->version_help = gengetopt_args_info_help[1] ;
76
args_info->height_help = gengetopt_args_info_help[2] ;
77
args_info->vavoom_help = gengetopt_args_info_help[3] ;
73
84
printf ("%s %s\n", TEST_REDEF_HELP_CMD_PARSER_PACKAGE, TEST_REDEF_HELP_CMD_PARSER_VERSION);
77
test_redef_help_cmd_parser_print_help (void)
87
static void print_help_common(void) {
80
88
test_redef_help_cmd_parser_print_version ();
82
90
if (strlen(gengetopt_args_info_purpose) > 0)
83
91
printf("\n%s\n", gengetopt_args_info_purpose);
85
printf("\n%s\n\n", gengetopt_args_info_usage);
93
if (strlen(gengetopt_args_info_usage) > 0)
94
printf("\n%s\n", gengetopt_args_info_usage);
87
98
if (strlen(gengetopt_args_info_description) > 0)
88
99
printf("%s\n", gengetopt_args_info_description);
103
test_redef_help_cmd_parser_print_help (void)
90
107
while (gengetopt_args_info_help[i])
91
108
printf("%s\n", gengetopt_args_info_help[i++]);
102
119
args_info->inputs_num = 0;
105
struct test_redef_help_cmd_parser_params *
106
test_redef_help_cmd_parser_params_init()
123
test_redef_help_cmd_parser_params_init(struct test_redef_help_cmd_parser_params *params)
108
struct test_redef_help_cmd_parser_params *params =
109
(struct test_redef_help_cmd_parser_params *)malloc(sizeof(struct test_redef_help_cmd_parser_params));
113
127
params->override = 0;
114
params->initialize = 0;
115
params->check_required = 0;
128
params->initialize = 1;
129
params->check_required = 1;
116
130
params->check_ambiguity = 0;
131
params->print_errors = 1;
135
struct test_redef_help_cmd_parser_params *
136
test_redef_help_cmd_parser_params_create(void)
138
struct test_redef_help_cmd_parser_params *params =
139
(struct test_redef_help_cmd_parser_params *)malloc(sizeof(struct test_redef_help_cmd_parser_params));
140
test_redef_help_cmd_parser_params_init(params);
123
147
test_redef_help_cmd_parser_release (struct gengetopt_args_info *args_info)
128
152
for (i = 0; i < args_info->inputs_num; ++i)
129
153
free (args_info->inputs [i]);
131
155
if (args_info->inputs_num)
132
156
free (args_info->inputs);
158
clear_given (args_info);
163
write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
166
fprintf(outfile, "%s=\"%s\"\n", opt, arg);
168
fprintf(outfile, "%s\n", opt);
174
test_redef_help_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
180
fprintf (stderr, "%s: cannot dump options to stream\n", TEST_REDEF_HELP_CMD_PARSER_PACKAGE);
184
if (args_info->help_given)
185
write_into_file(outfile, "help", 0, 0 );
186
if (args_info->version_given)
187
write_into_file(outfile, "version", 0, 0 );
188
if (args_info->height_given)
189
write_into_file(outfile, "height", 0, 0 );
190
if (args_info->vavoom_given)
191
write_into_file(outfile, "vavoom", 0, 0 );
134
clear_given (args_info);
148
209
return EXIT_FAILURE;
151
if (args_info->height_given) {
152
fprintf(outfile, "%s\n", "height");
154
if (args_info->vavoom_given) {
155
fprintf(outfile, "%s\n", "vavoom");
212
i = test_redef_help_cmd_parser_dump(outfile, args_info);
158
213
fclose (outfile);
234
287
return EXIT_SUCCESS;
291
static char *package_name = 0;
294
* @brief updates an option
295
* @param field the generic pointer to the field to update
296
* @param orig_field the pointer to the orig field
297
* @param field_given the pointer to the number of occurrence of this option
298
* @param prev_given the pointer to the number of occurrence already seen
299
* @param value the argument for this option (if null no arg was specified)
300
* @param possible_values the possible values for this option (if specified)
301
* @param default_value the default value (in case the option only accepts fixed values)
302
* @param arg_type the type of this option
303
* @param check_ambiguity @see test_redef_help_cmd_parser_params.check_ambiguity
304
* @param override @see test_redef_help_cmd_parser_params.override
305
* @param no_free whether to free a possible previous value
306
* @param multiple_option whether this is a multiple option
307
* @param long_opt the corresponding long option
308
* @param short_opt the corresponding short option (or '-' if none)
309
* @param additional_error possible further error specification
312
int update_arg(void *field, char **orig_field,
313
unsigned int *field_given, unsigned int *prev_given,
314
char *value, char *possible_values[], const char *default_value,
315
test_redef_help_cmd_parser_arg_type arg_type,
316
int check_ambiguity, int override,
317
int no_free, int multiple_option,
318
const char *long_opt, char short_opt,
319
const char *additional_error)
322
const char *val = value;
328
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
330
if (short_opt != '-')
331
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
332
package_name, long_opt, short_opt,
333
(additional_error ? additional_error : ""));
335
fprintf (stderr, "%s: `--%s' option given more than once%s\n",
336
package_name, long_opt,
337
(additional_error ? additional_error : ""));
338
return 1; /* failure */
342
if (field_given && *field_given && ! override)
349
val = possible_values[found];
357
/* store the original value */
362
if (value && orig_field) {
367
free (*orig_field); /* free previous string */
368
*orig_field = gengetopt_strdup (value);
238
378
test_redef_help_cmd_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
239
379
struct test_redef_help_cmd_parser_params *params, const char *additional_error)
405
opterr = params->print_errors;
268
410
int option_index = 0;
271
412
static struct option long_options[] = {
413
{ "help", 0, NULL, 0 },
414
{ "version", 0, NULL, 0 },
272
415
{ "height", 0, NULL, 'h' },
273
416
{ "vavoom", 0, NULL, 'V' },
274
417
{ NULL, 0, NULL, 0 }
278
420
c = getopt_long (argc, argv, "hV", long_options, &option_index);
280
422
if (c == -1) break; /* Exit from `while (1)' loop. */
284
426
case 'h': /* height. */
285
if (local_args_info.height_given || (check_ambiguity && args_info->height_given))
287
fprintf (stderr, "%s: `--height' (`-h') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
290
if (args_info->height_given && ! override)
292
local_args_info.height_given = 1;
293
args_info->height_given = 1;
430
0 , &(args_info->height_given),
431
&(local_args_info.height_given), optarg, 0, 0, ARG_NO,
432
check_ambiguity, override, 0, 0,
296
438
case 'V': /* vavoom instead of version. */
297
if (local_args_info.vavoom_given || (check_ambiguity && args_info->vavoom_given))
299
fprintf (stderr, "%s: `--vavoom' (`-V') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
302
if (args_info->vavoom_given && ! override)
304
local_args_info.vavoom_given = 1;
305
args_info->vavoom_given = 1;
442
0 , &(args_info->vavoom_given),
443
&(local_args_info.vavoom_given), optarg, 0, 0, ARG_NO,
444
check_ambiguity, override, 0, 0,
309
451
case 0: /* Long option with no short option */
452
if (strcmp (long_options[option_index].name, "help") == 0) {
453
test_redef_help_cmd_parser_print_help ();
454
test_redef_help_cmd_parser_free (&local_args_info);
458
if (strcmp (long_options[option_index].name, "version") == 0) {
459
test_redef_help_cmd_parser_print_version ();
460
test_redef_help_cmd_parser_free (&local_args_info);
310
464
case '?': /* Invalid option. */
311
465
/* `getopt_long' already printed an error message. */