82
89
printf ("%s %s\n", MORE_THAN_ONCE_CMD_PARSER_PACKAGE, MORE_THAN_ONCE_CMD_PARSER_VERSION);
86
more_than_once_cmd_parser_print_help (void)
92
static void print_help_common(void) {
89
93
more_than_once_cmd_parser_print_version ();
91
95
if (strlen(gengetopt_args_info_purpose) > 0)
92
96
printf("\n%s\n", gengetopt_args_info_purpose);
94
printf("\n%s\n\n", gengetopt_args_info_usage);
98
if (strlen(gengetopt_args_info_usage) > 0)
99
printf("\n%s\n", gengetopt_args_info_usage);
96
103
if (strlen(gengetopt_args_info_description) > 0)
97
104
printf("%s\n", gengetopt_args_info_description);
108
more_than_once_cmd_parser_print_help (void)
99
112
while (gengetopt_args_info_help[i])
100
113
printf("%s\n", gengetopt_args_info_help[i++]);
111
124
args_info->inputs_num = 0;
114
struct more_than_once_cmd_parser_params *
115
more_than_once_cmd_parser_params_init()
128
more_than_once_cmd_parser_params_init(struct more_than_once_cmd_parser_params *params)
117
struct more_than_once_cmd_parser_params *params =
118
(struct more_than_once_cmd_parser_params *)malloc(sizeof(struct more_than_once_cmd_parser_params));
122
132
params->override = 0;
123
params->initialize = 0;
124
params->check_required = 0;
133
params->initialize = 1;
134
params->check_required = 1;
125
135
params->check_ambiguity = 0;
136
params->print_errors = 1;
140
struct more_than_once_cmd_parser_params *
141
more_than_once_cmd_parser_params_create(void)
143
struct more_than_once_cmd_parser_params *params =
144
(struct more_than_once_cmd_parser_params *)malloc(sizeof(struct more_than_once_cmd_parser_params));
145
more_than_once_cmd_parser_params_init(params);
150
free_string_field (char **s)
132
161
more_than_once_cmd_parser_release (struct gengetopt_args_info *args_info)
136
if (args_info->foo_orig)
138
free (args_info->foo_orig); /* free previous argument */
139
args_info->foo_orig = 0;
141
if (args_info->bar_arg)
143
free (args_info->bar_arg); /* free previous argument */
144
args_info->bar_arg = 0;
146
if (args_info->bar_orig)
148
free (args_info->bar_orig); /* free previous argument */
149
args_info->bar_orig = 0;
164
free_string_field (&(args_info->foo_orig));
165
free_string_field (&(args_info->bar_arg));
166
free_string_field (&(args_info->bar_orig));
152
169
for (i = 0; i < args_info->inputs_num; ++i)
153
170
free (args_info->inputs [i]);
155
172
if (args_info->inputs_num)
156
173
free (args_info->inputs);
175
clear_given (args_info);
180
write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
183
fprintf(outfile, "%s=\"%s\"\n", opt, arg);
185
fprintf(outfile, "%s\n", opt);
191
more_than_once_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
197
fprintf (stderr, "%s: cannot dump options to stream\n", MORE_THAN_ONCE_CMD_PARSER_PACKAGE);
201
if (args_info->help_given)
202
write_into_file(outfile, "help", 0, 0 );
203
if (args_info->version_given)
204
write_into_file(outfile, "version", 0, 0 );
205
if (args_info->foo_given)
206
write_into_file(outfile, "foo", args_info->foo_orig, 0);
207
if (args_info->bar_given)
208
write_into_file(outfile, "bar", args_info->bar_orig, 0);
158
clear_given (args_info);
172
226
return EXIT_FAILURE;
175
if (args_info->help_given) {
176
fprintf(outfile, "%s\n", "help");
178
if (args_info->version_given) {
179
fprintf(outfile, "%s\n", "version");
181
if (args_info->foo_given) {
182
if (args_info->foo_orig) {
183
fprintf(outfile, "%s=\"%s\"\n", "foo", args_info->foo_orig);
185
fprintf(outfile, "%s\n", "foo");
188
if (args_info->bar_given) {
189
if (args_info->bar_orig) {
190
fprintf(outfile, "%s=\"%s\"\n", "bar", args_info->bar_orig);
192
fprintf(outfile, "%s\n", "bar");
229
i = more_than_once_cmd_parser_dump(outfile, args_info);
196
230
fclose (outfile);
272
304
return EXIT_SUCCESS;
308
static char *package_name = 0;
311
* @brief updates an option
312
* @param field the generic pointer to the field to update
313
* @param orig_field the pointer to the orig field
314
* @param field_given the pointer to the number of occurrence of this option
315
* @param prev_given the pointer to the number of occurrence already seen
316
* @param value the argument for this option (if null no arg was specified)
317
* @param possible_values the possible values for this option (if specified)
318
* @param default_value the default value (in case the option only accepts fixed values)
319
* @param arg_type the type of this option
320
* @param check_ambiguity @see more_than_once_cmd_parser_params.check_ambiguity
321
* @param override @see more_than_once_cmd_parser_params.override
322
* @param no_free whether to free a possible previous value
323
* @param multiple_option whether this is a multiple option
324
* @param long_opt the corresponding long option
325
* @param short_opt the corresponding short option (or '-' if none)
326
* @param additional_error possible further error specification
329
int update_arg(void *field, char **orig_field,
330
unsigned int *field_given, unsigned int *prev_given,
331
char *value, char *possible_values[], const char *default_value,
332
more_than_once_cmd_parser_arg_type arg_type,
333
int check_ambiguity, int override,
334
int no_free, int multiple_option,
335
const char *long_opt, char short_opt,
336
const char *additional_error)
339
const char *val = value;
346
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
348
if (short_opt != '-')
349
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
350
package_name, long_opt, short_opt,
351
(additional_error ? additional_error : ""));
353
fprintf (stderr, "%s: `--%s' option given more than once%s\n",
354
package_name, long_opt,
355
(additional_error ? additional_error : ""));
356
return 1; /* failure */
360
if (field_given && *field_given && ! override)
367
val = possible_values[found];
371
if (val) *((int *)field) = strtol (val, &stop_char, 0);
375
string_field = (char **)field;
376
if (!no_free && *string_field)
377
free (*string_field); /* free previous string */
378
*string_field = gengetopt_strdup (val);
385
/* check numeric conversion */
388
if (val && !(stop_char && *stop_char == '\0')) {
389
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
390
return 1; /* failure */
397
/* store the original value */
402
if (value && orig_field) {
407
free (*orig_field); /* free previous string */
408
*orig_field = gengetopt_strdup (value);
276
418
more_than_once_cmd_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
277
419
struct more_than_once_cmd_parser_params *params, const char *additional_error)
332
474
exit (EXIT_SUCCESS);
334
476
case 'f': /* foo option. */
335
if (local_args_info.foo_given || (check_ambiguity && args_info->foo_given))
337
fprintf (stderr, "%s: `--foo' (`-f') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
340
if (args_info->foo_given && ! override)
342
local_args_info.foo_given = 1;
343
args_info->foo_given = 1;
344
args_info->foo_arg = strtol (optarg, &stop_char, 0);
345
if (!(stop_char && *stop_char == '\0')) {
346
fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
479
if (update_arg( (void *)&(args_info->foo_arg),
480
&(args_info->foo_orig), &(args_info->foo_given),
481
&(local_args_info.foo_given), optarg, 0, 0, ARG_INT,
482
check_ambiguity, override, 0, 0,
349
if (args_info->foo_orig)
350
free (args_info->foo_orig); /* free previous string */
351
args_info->foo_orig = gengetopt_strdup (optarg);
354
488
case 'b': /* bar option. */
355
if (local_args_info.bar_given || (check_ambiguity && args_info->bar_given))
357
fprintf (stderr, "%s: `--bar' (`-b') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
360
if (args_info->bar_given && ! override)
362
local_args_info.bar_given = 1;
363
args_info->bar_given = 1;
364
if (args_info->bar_arg)
365
free (args_info->bar_arg); /* free previous string */
366
args_info->bar_arg = gengetopt_strdup (optarg);
367
if (args_info->bar_orig)
368
free (args_info->bar_orig); /* free previous string */
369
args_info->bar_orig = gengetopt_strdup (optarg);
491
if (update_arg( (void *)&(args_info->bar_arg),
492
&(args_info->bar_orig), &(args_info->bar_given),
493
&(local_args_info.bar_given), optarg, 0, 0, ARG_STRING,
494
check_ambiguity, override, 0, 0,
373
501
case 0: /* Long option with no short option */
374
502
case '?': /* Invalid option. */
375
503
/* `getopt_long' already printed an error message. */