79
85
printf ("%s %s\n", NO_OPTGIVEN_CMD_PARSER_PACKAGE, NO_OPTGIVEN_CMD_PARSER_VERSION);
83
no_optgiven_cmd_parser_print_help (void)
88
static void print_help_common(void) {
86
89
no_optgiven_cmd_parser_print_version ();
88
91
if (strlen(gengetopt_args_info_purpose) > 0)
89
92
printf("\n%s\n", gengetopt_args_info_purpose);
91
printf("\n%s\n\n", gengetopt_args_info_usage);
94
if (strlen(gengetopt_args_info_usage) > 0)
95
printf("\n%s\n", gengetopt_args_info_usage);
93
99
if (strlen(gengetopt_args_info_description) > 0)
94
100
printf("%s\n", gengetopt_args_info_description);
104
no_optgiven_cmd_parser_print_help (void)
96
108
while (gengetopt_args_info_help[i])
97
109
printf("%s\n", gengetopt_args_info_help[i++]);
108
120
args_info->inputs_num = 0;
111
struct no_optgiven_cmd_parser_params *
112
no_optgiven_cmd_parser_params_init()
124
no_optgiven_cmd_parser_params_init(struct no_optgiven_cmd_parser_params *params)
114
struct no_optgiven_cmd_parser_params *params =
115
(struct no_optgiven_cmd_parser_params *)malloc(sizeof(struct no_optgiven_cmd_parser_params));
119
128
params->override = 0;
120
params->initialize = 0;
121
params->check_required = 0;
129
params->initialize = 1;
130
params->check_required = 1;
122
131
params->check_ambiguity = 0;
132
params->print_errors = 1;
136
struct no_optgiven_cmd_parser_params *
137
no_optgiven_cmd_parser_params_create(void)
139
struct no_optgiven_cmd_parser_params *params =
140
(struct no_optgiven_cmd_parser_params *)malloc(sizeof(struct no_optgiven_cmd_parser_params));
141
no_optgiven_cmd_parser_params_init(params);
146
free_string_field (char **s)
129
157
no_optgiven_cmd_parser_release (struct gengetopt_args_info *args_info)
133
if (args_info->foo_orig)
135
free (args_info->foo_orig); /* free previous argument */
136
args_info->foo_orig = 0;
160
free_string_field (&(args_info->foo_orig));
139
163
for (i = 0; i < args_info->inputs_num; ++i)
140
164
free (args_info->inputs [i]);
142
166
if (args_info->inputs_num)
143
167
free (args_info->inputs);
169
clear_given (args_info);
174
write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
177
fprintf(outfile, "%s=\"%s\"\n", opt, arg);
179
fprintf(outfile, "%s\n", opt);
185
no_optgiven_cmd_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
191
fprintf (stderr, "%s: cannot dump options to stream\n", NO_OPTGIVEN_CMD_PARSER_PACKAGE);
195
if (args_info->help_given)
196
write_into_file(outfile, "help", 0, 0 );
197
if (args_info->version_given)
198
write_into_file(outfile, "version", 0, 0 );
199
if (args_info->foo_given)
200
write_into_file(outfile, "foo", args_info->foo_orig, 0);
145
clear_given (args_info);
159
218
return EXIT_FAILURE;
162
if (args_info->help_given) {
163
fprintf(outfile, "%s\n", "help");
165
if (args_info->version_given) {
166
fprintf(outfile, "%s\n", "version");
168
if (args_info->foo_given) {
169
if (args_info->foo_orig) {
170
fprintf(outfile, "%s=\"%s\"\n", "foo", args_info->foo_orig);
172
fprintf(outfile, "%s\n", "foo");
221
i = no_optgiven_cmd_parser_dump(outfile, args_info);
176
222
fclose (outfile);
311
static char *package_name = 0;
314
* @brief updates an option
315
* @param field the generic pointer to the field to update
316
* @param orig_field the pointer to the orig field
317
* @param field_given the pointer to the number of occurrence of this option
318
* @param prev_given the pointer to the number of occurrence already seen
319
* @param value the argument for this option (if null no arg was specified)
320
* @param possible_values the possible values for this option (if specified)
321
* @param default_value the default value (in case the option only accepts fixed values)
322
* @param arg_type the type of this option
323
* @param check_ambiguity @see no_optgiven_cmd_parser_params.check_ambiguity
324
* @param override @see no_optgiven_cmd_parser_params.override
325
* @param no_free whether to free a possible previous value
326
* @param multiple_option whether this is a multiple option
327
* @param long_opt the corresponding long option
328
* @param short_opt the corresponding short option (or '-' if none)
329
* @param additional_error possible further error specification
332
int update_arg(void *field, char **orig_field,
333
unsigned int *field_given, unsigned int *prev_given,
334
char *value, char *possible_values[], const char *default_value,
335
no_optgiven_cmd_parser_arg_type arg_type,
336
int check_ambiguity, int override,
337
int no_free, int multiple_option,
338
const char *long_opt, char short_opt,
339
const char *additional_error)
342
const char *val = value;
348
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
350
if (short_opt != '-')
351
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
352
package_name, long_opt, short_opt,
353
(additional_error ? additional_error : ""));
355
fprintf (stderr, "%s: `--%s' option given more than once%s\n",
356
package_name, long_opt,
357
(additional_error ? additional_error : ""));
358
return 1; /* failure */
362
if (field_given && *field_given && ! override)
369
val = possible_values[found];
373
if (val) *((int *)field) = strtol (val, &stop_char, 0);
379
/* check numeric conversion */
382
if (val && !(stop_char && *stop_char == '\0')) {
383
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
384
return 1; /* failure */
391
/* store the original value */
396
if (value && orig_field) {
401
free (*orig_field); /* free previous string */
402
*orig_field = gengetopt_strdup (value);
267
412
no_optgiven_cmd_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
268
413
struct no_optgiven_cmd_parser_params *params, const char *additional_error)
322
467
exit (EXIT_SUCCESS);
324
469
case 'i': /* foo option. */
325
if (local_args_info.foo_given || (check_ambiguity && args_info->foo_given))
327
fprintf (stderr, "%s: `--foo' (`-i') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
330
if (args_info->foo_given && ! override)
332
local_args_info.foo_given = 1;
333
args_info->foo_given = 1;
334
args_info->foo_arg = strtol (optarg, &stop_char, 0);
335
if (!(stop_char && *stop_char == '\0')) {
336
fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
472
if (update_arg( (void *)&(args_info->foo_arg),
473
&(args_info->foo_orig), &(args_info->foo_given),
474
&(local_args_info.foo_given), optarg, 0, 0, ARG_INT,
475
check_ambiguity, override, 0, 0,
339
if (args_info->foo_orig)
340
free (args_info->foo_orig); /* free previous string */
341
args_info->foo_orig = gengetopt_strdup (optarg);
345
482
case 0: /* Long option with no short option */
346
483
case '?': /* Invalid option. */
347
484
/* `getopt_long' already printed an error message. */