2
File autogenerated by gengetopt
3
generated with the following command:
4
../src/gengetopt --gen-version --input=../../tests/test_only_flags_cmd.ggo --func-name=test_only_flags_cmd_parser --file-name=test_only_flags_cmd --unamed-opt --show-required
6
The developers of gengetopt consider the fixed text that goes in all
7
gengetopt output files to be in the public domain:
8
we make no copyright claims on it.
11
/* If we use autoconf. */
21
#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
26
#include "test_only_flags_cmd.h"
28
const char *gengetopt_args_info_purpose = "";
30
const char *gengetopt_args_info_usage = "Usage: test_only_flags [OPTIONS]... [FILES]...";
32
const char *gengetopt_args_info_description = "";
34
const char *gengetopt_args_info_help[] = {
35
" -h, --help Print help and exit",
36
" -V, --version Print version and exit",
37
" -f, --foo foo option (default=on)",
43
} test_only_flags_cmd_parser_arg_type;
46
void clear_given (struct gengetopt_args_info *args_info);
48
void clear_args (struct gengetopt_args_info *args_info);
51
test_only_flags_cmd_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
52
struct test_only_flags_cmd_parser_params *params, const char *additional_error);
56
gengetopt_strdup (const char *s);
59
void clear_given (struct gengetopt_args_info *args_info)
61
args_info->help_given = 0 ;
62
args_info->version_given = 0 ;
63
args_info->foo_given = 0 ;
67
void clear_args (struct gengetopt_args_info *args_info)
69
FIX_UNUSED (args_info);
70
args_info->foo_flag = 1;
75
void init_args_info(struct gengetopt_args_info *args_info)
79
args_info->help_help = gengetopt_args_info_help[0] ;
80
args_info->version_help = gengetopt_args_info_help[1] ;
81
args_info->foo_help = gengetopt_args_info_help[2] ;
86
test_only_flags_cmd_parser_print_version (void)
89
(strlen(TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE_NAME) ? TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE_NAME : TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE),
90
TEST_ONLY_FLAGS_CMD_PARSER_VERSION);
93
static void print_help_common(void) {
94
test_only_flags_cmd_parser_print_version ();
96
if (strlen(gengetopt_args_info_purpose) > 0)
97
printf("\n%s\n", gengetopt_args_info_purpose);
99
if (strlen(gengetopt_args_info_usage) > 0)
100
printf("\n%s\n", gengetopt_args_info_usage);
104
if (strlen(gengetopt_args_info_description) > 0)
105
printf("%s\n\n", gengetopt_args_info_description);
109
test_only_flags_cmd_parser_print_help (void)
113
while (gengetopt_args_info_help[i])
114
printf("%s\n", gengetopt_args_info_help[i++]);
118
test_only_flags_cmd_parser_init (struct gengetopt_args_info *args_info)
120
clear_given (args_info);
121
clear_args (args_info);
122
init_args_info (args_info);
124
args_info->inputs = 0;
125
args_info->inputs_num = 0;
129
test_only_flags_cmd_parser_params_init(struct test_only_flags_cmd_parser_params *params)
133
params->override = 0;
134
params->initialize = 1;
135
params->check_required = 1;
136
params->check_ambiguity = 0;
137
params->print_errors = 1;
141
struct test_only_flags_cmd_parser_params *
142
test_only_flags_cmd_parser_params_create(void)
144
struct test_only_flags_cmd_parser_params *params =
145
(struct test_only_flags_cmd_parser_params *)malloc(sizeof(struct test_only_flags_cmd_parser_params));
146
test_only_flags_cmd_parser_params_init(params);
153
test_only_flags_cmd_parser_release (struct gengetopt_args_info *args_info)
158
for (i = 0; i < args_info->inputs_num; ++i)
159
free (args_info->inputs [i]);
161
if (args_info->inputs_num)
162
free (args_info->inputs);
164
clear_given (args_info);
169
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
173
fprintf(outfile, "%s=\"%s\"\n", opt, arg);
175
fprintf(outfile, "%s\n", opt);
181
test_only_flags_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
187
fprintf (stderr, "%s: cannot dump options to stream\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE);
191
if (args_info->help_given)
192
write_into_file(outfile, "help", 0, 0 );
193
if (args_info->version_given)
194
write_into_file(outfile, "version", 0, 0 );
195
if (args_info->foo_given)
196
write_into_file(outfile, "foo", 0, 0 );
204
test_only_flags_cmd_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
209
outfile = fopen(filename, "w");
213
fprintf (stderr, "%s: cannot open file for writing: %s\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE, filename);
217
i = test_only_flags_cmd_parser_dump(outfile, args_info);
224
test_only_flags_cmd_parser_free (struct gengetopt_args_info *args_info)
226
test_only_flags_cmd_parser_release (args_info);
229
/** @brief replacement of strdup, which is not standard */
231
gengetopt_strdup (const char *s)
237
result = (char*)malloc(strlen(s) + 1);
238
if (result == (char*)0)
245
test_only_flags_cmd_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
247
return test_only_flags_cmd_parser2 (argc, argv, args_info, 0, 1, 1);
251
test_only_flags_cmd_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
252
struct test_only_flags_cmd_parser_params *params)
255
result = test_only_flags_cmd_parser_internal (argc, argv, args_info, params, 0);
257
if (result == EXIT_FAILURE)
259
test_only_flags_cmd_parser_free (args_info);
267
test_only_flags_cmd_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
270
struct test_only_flags_cmd_parser_params params;
272
params.override = override;
273
params.initialize = initialize;
274
params.check_required = check_required;
275
params.check_ambiguity = 0;
276
params.print_errors = 1;
278
result = test_only_flags_cmd_parser_internal (argc, argv, args_info, ¶ms, 0);
280
if (result == EXIT_FAILURE)
282
test_only_flags_cmd_parser_free (args_info);
290
test_only_flags_cmd_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
292
FIX_UNUSED (args_info);
293
FIX_UNUSED (prog_name);
298
static char *package_name = 0;
301
* @brief updates an option
302
* @param field the generic pointer to the field to update
303
* @param orig_field the pointer to the orig field
304
* @param field_given the pointer to the number of occurrence of this option
305
* @param prev_given the pointer to the number of occurrence already seen
306
* @param value the argument for this option (if null no arg was specified)
307
* @param possible_values the possible values for this option (if specified)
308
* @param default_value the default value (in case the option only accepts fixed values)
309
* @param arg_type the type of this option
310
* @param check_ambiguity @see test_only_flags_cmd_parser_params.check_ambiguity
311
* @param override @see test_only_flags_cmd_parser_params.override
312
* @param no_free whether to free a possible previous value
313
* @param multiple_option whether this is a multiple option
314
* @param long_opt the corresponding long option
315
* @param short_opt the corresponding short option (or '-' if none)
316
* @param additional_error possible further error specification
319
int update_arg(void *field, char **orig_field,
320
unsigned int *field_given, unsigned int *prev_given,
321
char *value, const char *possible_values[],
322
const char *default_value,
323
test_only_flags_cmd_parser_arg_type arg_type,
324
int check_ambiguity, int override,
325
int no_free, int multiple_option,
326
const char *long_opt, char short_opt,
327
const char *additional_error)
330
const char *val = value;
337
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
339
if (short_opt != '-')
340
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
341
package_name, long_opt, short_opt,
342
(additional_error ? additional_error : ""));
344
fprintf (stderr, "%s: `--%s' option given more than once%s\n",
345
package_name, long_opt,
346
(additional_error ? additional_error : ""));
347
return 1; /* failure */
350
FIX_UNUSED (default_value);
352
if (field_given && *field_given && ! override)
359
val = possible_values[found];
363
*((int *)field) = !*((int *)field);
370
/* store the original value */
376
if (value && orig_field) {
381
free (*orig_field); /* free previous string */
382
*orig_field = gengetopt_strdup (value);
392
test_only_flags_cmd_parser_internal (
393
int argc, char **argv, struct gengetopt_args_info *args_info,
394
struct test_only_flags_cmd_parser_params *params, const char *additional_error)
396
int c; /* Character of the parsed option. */
399
struct gengetopt_args_info local_args_info;
406
package_name = argv[0];
408
override = params->override;
409
initialize = params->initialize;
410
check_required = params->check_required;
411
check_ambiguity = params->check_ambiguity;
414
test_only_flags_cmd_parser_init (args_info);
416
test_only_flags_cmd_parser_init (&local_args_info);
420
opterr = params->print_errors;
425
int option_index = 0;
427
static struct option long_options[] = {
428
{ "help", 0, NULL, 'h' },
429
{ "version", 0, NULL, 'V' },
430
{ "foo", 0, NULL, 'f' },
434
c = getopt_long (argc, argv, "hVf", long_options, &option_index);
436
if (c == -1) break; /* Exit from `while (1)' loop. */
440
case 'h': /* Print help and exit. */
441
test_only_flags_cmd_parser_print_help ();
442
test_only_flags_cmd_parser_free (&local_args_info);
445
case 'V': /* Print version and exit. */
446
test_only_flags_cmd_parser_print_version ();
447
test_only_flags_cmd_parser_free (&local_args_info);
450
case 'f': /* foo option. */
453
if (update_arg((void *)&(args_info->foo_flag), 0, &(args_info->foo_given),
454
&(local_args_info.foo_given), optarg, 0, 0, ARG_FLAG,
455
check_ambiguity, override, 1, 0, "foo", 'f',
461
case 0: /* Long option with no short option */
462
case '?': /* Invalid option. */
463
/* `getopt_long' already printed an error message. */
466
default: /* bug: option not considered. */
467
fprintf (stderr, "%s: option unknown: %c%s\n", TEST_ONLY_FLAGS_CMD_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
475
test_only_flags_cmd_parser_release (&local_args_info);
478
return (EXIT_FAILURE);
483
int found_prog_name = 0;
484
/* whether program name, i.e., argv[0], is in the remaining args
485
(this may happen with some implementations of getopt,
486
but surely not with the one included by gengetopt) */
490
if (argv[i++] == argv[0]) {
496
args_info->inputs_num = argc - optind - found_prog_name;
498
(char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
499
while (optind < argc)
500
if (argv[optind++] != argv[0])
501
args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
508
test_only_flags_cmd_parser_release (&local_args_info);
509
return (EXIT_FAILURE);