2 File autogenerated by gengetopt version 2.22.4
3 generated with the following command:
4 ../src/gengetopt --input=../../src/cmdline.ggo --output-dir=../../src --no-handle-version --no-handle-help --no-handle-error --string-parser
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 */
28 const char *gengetopt_args_info_purpose = "This program generates a C function that uses getopt_long function\nto parse the command line options, validate them and fill a struct.";
30 const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]...";
32 const char *gengetopt_args_info_description = "";
34 const char *gengetopt_args_info_detailed_help[] = {
35 " -h, --help Print help and exit",
36 " --detailed-help Print help, including all details and hidden \n options, and exit",
37 " -V, --version Print version and exit",
40 " -i, --input=filename input file (default std input)",
41 " -f, --func-name=name name of generated function \n (default=`cmdline_parser')",
42 " -a, --arg-struct-name=name name of generated args info struct \n (default=`gengetopt_args_info')",
43 " -F, --file-name=name name of generated file (default=`cmdline')",
44 " --output-dir=path output directory",
45 " \n if this option is not specified, the files are generated in the current \n directory.\n",
46 " --header-output-dir=path header output directory",
47 " --src-output-dir=path source output directory",
48 " -c, --c-extension=ext extension of c file (default=`c')",
49 " -H, --header-extension=ext extension of header file (default=`h')",
50 " -l, --long-help long usage line in help",
51 " \n The usage line will print all the options, e.g.,\n\n sample1 -iINT|--int-opt=INT [-h|--help]\n",
52 " --default-optional by default, an option is considered optional if \n not specified otherwise",
53 " -u, --unamed-opts[=STRING] accept options without names (e.g., file names) \n (default=`FILES')",
54 "\nThe parser generated is thought to be used to parse the command line arguments. \nHowever, you can also generate parsers for configuration files, or strings that \ncontain the arguments to parse, by using the following two options.\n",
55 " -C, --conf-parser generate a config file parser",
56 " -S, --string-parser generate a string parser (the string contains \n the command line)",
58 "\nAdditional options:",
59 " -G, --include-getopt adds the code for getopt_long in the generated \n C file",
60 " -n, --no-handle-help do not handle --help|-h automatically",
61 " \n If --no-handle-help is specified, the command line option --help|-h will not \n be handled automatically, so the programmer will be able to print some other \n information; then the function for printing the standard help output can be \n used; this function is called <parser-name>_print_help.\n\n Notice that, although the programmer can handle --help|-h manually, the \n parser will return after finding such option: the other command line options, \n if any, will be ignored. In case you want to have full control on --help|-h, \n you should use --ho-help.\n",
62 " --no-help do not add --help|-h automatically",
63 " \n With this option you can disable the automatic addition of options --help|-h. \n The programmer will then be able to add this option in the input file and \n handle it as he sees fit. Notice that --no-help will also disable the \n automatic options --detailed-help and --full-help.\n\n The programmer can still define options with short character h as he wants, \n but he cannot define options help, unless he specifies --no-help (otherwise \n an error will be printed).\n",
64 " -N, --no-handle-version do not handle --version|-V automatically",
65 " --no-version do not add --version|-V automatically",
66 " \n See above the details about --no-handle-help and --no-help, respectively.\n",
67 " -e, --no-handle-error do not exit on errors",
68 " \n With this option, if the generated parser encounters an error (e.g., an \n unknown option) it does not make the main program exit; instead, the parser \n function returns a value different 0, and the main program can print a help \n message.\n",
69 " --show-required[=STRING] in the output of help will specify which \n options are mandatory, by using the optional \n passed string (default=`(mandatory)')",
70 " -g, --gen-version put gengetopt version in the generated file \n (default=on)",
71 " --set-package=STRING set the package name (override package defined \n in the .ggo file)",
72 " --set-version=STRING set the version number (override version \n defined in the .ggo file)",
73 " --show-help show the output of --help instead of generating \n code",
74 " --show-full-help show the output of --full-help (i.e., including \n hidden options) instead of generating code",
75 " --show-detailed-help show the output of --detailed-help (i.e., \n including details and hidden options) instead \n of generating code",
76 " --show-version show the output of --version instead of \n generating code",
77 "\nPlease refer to the info manual for further explanations.",
84 gengetopt_args_info_help[0] = gengetopt_args_info_detailed_help[0];
85 gengetopt_args_info_help[1] = gengetopt_args_info_detailed_help[1];
86 gengetopt_args_info_help[2] = gengetopt_args_info_detailed_help[2];
87 gengetopt_args_info_help[3] = gengetopt_args_info_detailed_help[3];
88 gengetopt_args_info_help[4] = gengetopt_args_info_detailed_help[4];
89 gengetopt_args_info_help[5] = gengetopt_args_info_detailed_help[5];
90 gengetopt_args_info_help[6] = gengetopt_args_info_detailed_help[6];
91 gengetopt_args_info_help[7] = gengetopt_args_info_detailed_help[7];
92 gengetopt_args_info_help[8] = gengetopt_args_info_detailed_help[8];
93 gengetopt_args_info_help[9] = gengetopt_args_info_detailed_help[9];
94 gengetopt_args_info_help[10] = gengetopt_args_info_detailed_help[11];
95 gengetopt_args_info_help[11] = gengetopt_args_info_detailed_help[12];
96 gengetopt_args_info_help[12] = gengetopt_args_info_detailed_help[13];
97 gengetopt_args_info_help[13] = gengetopt_args_info_detailed_help[14];
98 gengetopt_args_info_help[14] = gengetopt_args_info_detailed_help[15];
99 gengetopt_args_info_help[15] = gengetopt_args_info_detailed_help[17];
100 gengetopt_args_info_help[16] = gengetopt_args_info_detailed_help[18];
101 gengetopt_args_info_help[17] = gengetopt_args_info_detailed_help[19];
102 gengetopt_args_info_help[18] = gengetopt_args_info_detailed_help[20];
103 gengetopt_args_info_help[19] = gengetopt_args_info_detailed_help[21];
104 gengetopt_args_info_help[20] = gengetopt_args_info_detailed_help[22];
105 gengetopt_args_info_help[21] = gengetopt_args_info_detailed_help[23];
106 gengetopt_args_info_help[22] = gengetopt_args_info_detailed_help[24];
107 gengetopt_args_info_help[23] = gengetopt_args_info_detailed_help[25];
108 gengetopt_args_info_help[24] = gengetopt_args_info_detailed_help[27];
109 gengetopt_args_info_help[25] = gengetopt_args_info_detailed_help[29];
110 gengetopt_args_info_help[26] = gengetopt_args_info_detailed_help[30];
111 gengetopt_args_info_help[27] = gengetopt_args_info_detailed_help[32];
112 gengetopt_args_info_help[28] = gengetopt_args_info_detailed_help[34];
113 gengetopt_args_info_help[29] = gengetopt_args_info_detailed_help[35];
114 gengetopt_args_info_help[30] = gengetopt_args_info_detailed_help[36];
115 gengetopt_args_info_help[31] = gengetopt_args_info_detailed_help[37];
116 gengetopt_args_info_help[32] = gengetopt_args_info_detailed_help[38];
117 gengetopt_args_info_help[33] = gengetopt_args_info_detailed_help[39];
118 gengetopt_args_info_help[34] = gengetopt_args_info_detailed_help[40];
119 gengetopt_args_info_help[35] = gengetopt_args_info_detailed_help[41];
120 gengetopt_args_info_help[36] = gengetopt_args_info_detailed_help[42];
121 gengetopt_args_info_help[37] = 0;
125 const char *gengetopt_args_info_help[38];
130 } cmdline_parser_arg_type;
133 void clear_given (struct gengetopt_args_info *args_info);
135 void clear_args (struct gengetopt_args_info *args_info);
138 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
139 struct cmdline_parser_params *params, const char *additional_error);
144 struct line_list * next;
147 static struct line_list *cmd_line_list = 0;
148 static struct line_list *cmd_line_list_tmp = 0;
153 /* free the list of a previous call */
156 while (cmd_line_list) {
157 cmd_line_list_tmp = cmd_line_list;
158 cmd_line_list = cmd_line_list->next;
159 free (cmd_line_list_tmp->string_arg);
160 free (cmd_line_list_tmp);
167 gengetopt_strdup (const char *s);
170 void clear_given (struct gengetopt_args_info *args_info)
172 args_info->help_given = 0 ;
173 args_info->detailed_help_given = 0 ;
174 args_info->version_given = 0 ;
175 args_info->input_given = 0 ;
176 args_info->func_name_given = 0 ;
177 args_info->arg_struct_name_given = 0 ;
178 args_info->file_name_given = 0 ;
179 args_info->output_dir_given = 0 ;
180 args_info->header_output_dir_given = 0 ;
181 args_info->src_output_dir_given = 0 ;
182 args_info->c_extension_given = 0 ;
183 args_info->header_extension_given = 0 ;
184 args_info->long_help_given = 0 ;
185 args_info->default_optional_given = 0 ;
186 args_info->unamed_opts_given = 0 ;
187 args_info->conf_parser_given = 0 ;
188 args_info->string_parser_given = 0 ;
189 args_info->include_getopt_given = 0 ;
190 args_info->no_handle_help_given = 0 ;
191 args_info->no_help_given = 0 ;
192 args_info->no_handle_version_given = 0 ;
193 args_info->no_version_given = 0 ;
194 args_info->no_handle_error_given = 0 ;
195 args_info->show_required_given = 0 ;
196 args_info->gen_version_given = 0 ;
197 args_info->set_package_given = 0 ;
198 args_info->set_version_given = 0 ;
199 args_info->show_help_given = 0 ;
200 args_info->show_full_help_given = 0 ;
201 args_info->show_detailed_help_given = 0 ;
202 args_info->show_version_given = 0 ;
206 void clear_args (struct gengetopt_args_info *args_info)
208 FIX_UNUSED (args_info);
209 args_info->input_arg = NULL;
210 args_info->input_orig = NULL;
211 args_info->func_name_arg = gengetopt_strdup ("cmdline_parser");
212 args_info->func_name_orig = NULL;
213 args_info->arg_struct_name_arg = gengetopt_strdup ("gengetopt_args_info");
214 args_info->arg_struct_name_orig = NULL;
215 args_info->file_name_arg = gengetopt_strdup ("cmdline");
216 args_info->file_name_orig = NULL;
217 args_info->output_dir_arg = NULL;
218 args_info->output_dir_orig = NULL;
219 args_info->header_output_dir_arg = NULL;
220 args_info->header_output_dir_orig = NULL;
221 args_info->src_output_dir_arg = NULL;
222 args_info->src_output_dir_orig = NULL;
223 args_info->c_extension_arg = gengetopt_strdup ("c");
224 args_info->c_extension_orig = NULL;
225 args_info->header_extension_arg = gengetopt_strdup ("h");
226 args_info->header_extension_orig = NULL;
227 args_info->unamed_opts_arg = gengetopt_strdup ("FILES");
228 args_info->unamed_opts_orig = NULL;
229 args_info->show_required_arg = gengetopt_strdup ("(mandatory)");
230 args_info->show_required_orig = NULL;
231 args_info->gen_version_flag = 1;
232 args_info->set_package_arg = NULL;
233 args_info->set_package_orig = NULL;
234 args_info->set_version_arg = NULL;
235 args_info->set_version_orig = NULL;
240 void init_args_info(struct gengetopt_args_info *args_info)
244 args_info->help_help = gengetopt_args_info_detailed_help[0] ;
245 args_info->detailed_help_help = gengetopt_args_info_detailed_help[1] ;
246 args_info->version_help = gengetopt_args_info_detailed_help[2] ;
247 args_info->input_help = gengetopt_args_info_detailed_help[5] ;
248 args_info->func_name_help = gengetopt_args_info_detailed_help[6] ;
249 args_info->arg_struct_name_help = gengetopt_args_info_detailed_help[7] ;
250 args_info->file_name_help = gengetopt_args_info_detailed_help[8] ;
251 args_info->output_dir_help = gengetopt_args_info_detailed_help[9] ;
252 args_info->header_output_dir_help = gengetopt_args_info_detailed_help[11] ;
253 args_info->src_output_dir_help = gengetopt_args_info_detailed_help[12] ;
254 args_info->c_extension_help = gengetopt_args_info_detailed_help[13] ;
255 args_info->header_extension_help = gengetopt_args_info_detailed_help[14] ;
256 args_info->long_help_help = gengetopt_args_info_detailed_help[15] ;
257 args_info->default_optional_help = gengetopt_args_info_detailed_help[17] ;
258 args_info->unamed_opts_help = gengetopt_args_info_detailed_help[18] ;
259 args_info->conf_parser_help = gengetopt_args_info_detailed_help[20] ;
260 args_info->string_parser_help = gengetopt_args_info_detailed_help[21] ;
261 args_info->include_getopt_help = gengetopt_args_info_detailed_help[24] ;
262 args_info->no_handle_help_help = gengetopt_args_info_detailed_help[25] ;
263 args_info->no_help_help = gengetopt_args_info_detailed_help[27] ;
264 args_info->no_handle_version_help = gengetopt_args_info_detailed_help[29] ;
265 args_info->no_version_help = gengetopt_args_info_detailed_help[30] ;
266 args_info->no_handle_error_help = gengetopt_args_info_detailed_help[32] ;
267 args_info->show_required_help = gengetopt_args_info_detailed_help[34] ;
268 args_info->gen_version_help = gengetopt_args_info_detailed_help[35] ;
269 args_info->set_package_help = gengetopt_args_info_detailed_help[36] ;
270 args_info->set_version_help = gengetopt_args_info_detailed_help[37] ;
271 args_info->show_help_help = gengetopt_args_info_detailed_help[38] ;
272 args_info->show_full_help_help = gengetopt_args_info_detailed_help[39] ;
273 args_info->show_detailed_help_help = gengetopt_args_info_detailed_help[40] ;
274 args_info->show_version_help = gengetopt_args_info_detailed_help[41] ;
279 cmdline_parser_print_version (void)
282 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
283 CMDLINE_PARSER_VERSION);
286 static void print_help_common(void) {
287 cmdline_parser_print_version ();
289 if (strlen(gengetopt_args_info_purpose) > 0)
290 printf("\n%s\n", gengetopt_args_info_purpose);
292 if (strlen(gengetopt_args_info_usage) > 0)
293 printf("\n%s\n", gengetopt_args_info_usage);
297 if (strlen(gengetopt_args_info_description) > 0)
298 printf("%s\n\n", gengetopt_args_info_description);
302 cmdline_parser_print_help (void)
306 while (gengetopt_args_info_help[i])
307 printf("%s\n", gengetopt_args_info_help[i++]);
311 cmdline_parser_print_detailed_help (void)
315 while (gengetopt_args_info_detailed_help[i])
316 printf("%s\n", gengetopt_args_info_detailed_help[i++]);
320 cmdline_parser_init (struct gengetopt_args_info *args_info)
322 clear_given (args_info);
323 clear_args (args_info);
324 init_args_info (args_info);
328 cmdline_parser_params_init(struct cmdline_parser_params *params)
332 params->override = 0;
333 params->initialize = 1;
334 params->check_required = 1;
335 params->check_ambiguity = 0;
336 params->print_errors = 1;
340 struct cmdline_parser_params *
341 cmdline_parser_params_create(void)
343 struct cmdline_parser_params *params =
344 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
345 cmdline_parser_params_init(params);
350 free_string_field (char **s)
361 cmdline_parser_release (struct gengetopt_args_info *args_info)
364 free_string_field (&(args_info->input_arg));
365 free_string_field (&(args_info->input_orig));
366 free_string_field (&(args_info->func_name_arg));
367 free_string_field (&(args_info->func_name_orig));
368 free_string_field (&(args_info->arg_struct_name_arg));
369 free_string_field (&(args_info->arg_struct_name_orig));
370 free_string_field (&(args_info->file_name_arg));
371 free_string_field (&(args_info->file_name_orig));
372 free_string_field (&(args_info->output_dir_arg));
373 free_string_field (&(args_info->output_dir_orig));
374 free_string_field (&(args_info->header_output_dir_arg));
375 free_string_field (&(args_info->header_output_dir_orig));
376 free_string_field (&(args_info->src_output_dir_arg));
377 free_string_field (&(args_info->src_output_dir_orig));
378 free_string_field (&(args_info->c_extension_arg));
379 free_string_field (&(args_info->c_extension_orig));
380 free_string_field (&(args_info->header_extension_arg));
381 free_string_field (&(args_info->header_extension_orig));
382 free_string_field (&(args_info->unamed_opts_arg));
383 free_string_field (&(args_info->unamed_opts_orig));
384 free_string_field (&(args_info->show_required_arg));
385 free_string_field (&(args_info->show_required_orig));
386 free_string_field (&(args_info->set_package_arg));
387 free_string_field (&(args_info->set_package_orig));
388 free_string_field (&(args_info->set_version_arg));
389 free_string_field (&(args_info->set_version_orig));
393 clear_given (args_info);
398 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
402 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
404 fprintf(outfile, "%s\n", opt);
410 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
416 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
420 if (args_info->help_given)
421 write_into_file(outfile, "help", 0, 0 );
422 if (args_info->detailed_help_given)
423 write_into_file(outfile, "detailed-help", 0, 0 );
424 if (args_info->version_given)
425 write_into_file(outfile, "version", 0, 0 );
426 if (args_info->input_given)
427 write_into_file(outfile, "input", args_info->input_orig, 0);
428 if (args_info->func_name_given)
429 write_into_file(outfile, "func-name", args_info->func_name_orig, 0);
430 if (args_info->arg_struct_name_given)
431 write_into_file(outfile, "arg-struct-name", args_info->arg_struct_name_orig, 0);
432 if (args_info->file_name_given)
433 write_into_file(outfile, "file-name", args_info->file_name_orig, 0);
434 if (args_info->output_dir_given)
435 write_into_file(outfile, "output-dir", args_info->output_dir_orig, 0);
436 if (args_info->header_output_dir_given)
437 write_into_file(outfile, "header-output-dir", args_info->header_output_dir_orig, 0);
438 if (args_info->src_output_dir_given)
439 write_into_file(outfile, "src-output-dir", args_info->src_output_dir_orig, 0);
440 if (args_info->c_extension_given)
441 write_into_file(outfile, "c-extension", args_info->c_extension_orig, 0);
442 if (args_info->header_extension_given)
443 write_into_file(outfile, "header-extension", args_info->header_extension_orig, 0);
444 if (args_info->long_help_given)
445 write_into_file(outfile, "long-help", 0, 0 );
446 if (args_info->default_optional_given)
447 write_into_file(outfile, "default-optional", 0, 0 );
448 if (args_info->unamed_opts_given)
449 write_into_file(outfile, "unamed-opts", args_info->unamed_opts_orig, 0);
450 if (args_info->conf_parser_given)
451 write_into_file(outfile, "conf-parser", 0, 0 );
452 if (args_info->string_parser_given)
453 write_into_file(outfile, "string-parser", 0, 0 );
454 if (args_info->include_getopt_given)
455 write_into_file(outfile, "include-getopt", 0, 0 );
456 if (args_info->no_handle_help_given)
457 write_into_file(outfile, "no-handle-help", 0, 0 );
458 if (args_info->no_help_given)
459 write_into_file(outfile, "no-help", 0, 0 );
460 if (args_info->no_handle_version_given)
461 write_into_file(outfile, "no-handle-version", 0, 0 );
462 if (args_info->no_version_given)
463 write_into_file(outfile, "no-version", 0, 0 );
464 if (args_info->no_handle_error_given)
465 write_into_file(outfile, "no-handle-error", 0, 0 );
466 if (args_info->show_required_given)
467 write_into_file(outfile, "show-required", args_info->show_required_orig, 0);
468 if (args_info->gen_version_given)
469 write_into_file(outfile, "gen-version", 0, 0 );
470 if (args_info->set_package_given)
471 write_into_file(outfile, "set-package", args_info->set_package_orig, 0);
472 if (args_info->set_version_given)
473 write_into_file(outfile, "set-version", args_info->set_version_orig, 0);
474 if (args_info->show_help_given)
475 write_into_file(outfile, "show-help", 0, 0 );
476 if (args_info->show_full_help_given)
477 write_into_file(outfile, "show-full-help", 0, 0 );
478 if (args_info->show_detailed_help_given)
479 write_into_file(outfile, "show-detailed-help", 0, 0 );
480 if (args_info->show_version_given)
481 write_into_file(outfile, "show-version", 0, 0 );
489 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
494 outfile = fopen(filename, "w");
498 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
502 i = cmdline_parser_dump(outfile, args_info);
509 cmdline_parser_free (struct gengetopt_args_info *args_info)
511 cmdline_parser_release (args_info);
514 /** @brief replacement of strdup, which is not standard */
516 gengetopt_strdup (const char *s)
522 result = (char*)malloc(strlen(s) + 1);
523 if (result == (char*)0)
530 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
532 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
536 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
537 struct cmdline_parser_params *params)
540 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
546 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
549 struct cmdline_parser_params params;
551 params.override = override;
552 params.initialize = initialize;
553 params.check_required = check_required;
554 params.check_ambiguity = 0;
555 params.print_errors = 1;
557 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0);
563 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
565 FIX_UNUSED (args_info);
566 FIX_UNUSED (prog_name);
571 static char *package_name = 0;
574 * @brief updates an option
575 * @param field the generic pointer to the field to update
576 * @param orig_field the pointer to the orig field
577 * @param field_given the pointer to the number of occurrence of this option
578 * @param prev_given the pointer to the number of occurrence already seen
579 * @param value the argument for this option (if null no arg was specified)
580 * @param possible_values the possible values for this option (if specified)
581 * @param default_value the default value (in case the option only accepts fixed values)
582 * @param arg_type the type of this option
583 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
584 * @param override @see cmdline_parser_params.override
585 * @param no_free whether to free a possible previous value
586 * @param multiple_option whether this is a multiple option
587 * @param long_opt the corresponding long option
588 * @param short_opt the corresponding short option (or '-' if none)
589 * @param additional_error possible further error specification
592 int update_arg(void *field, char **orig_field,
593 unsigned int *field_given, unsigned int *prev_given,
594 char *value, const char *possible_values[],
595 const char *default_value,
596 cmdline_parser_arg_type arg_type,
597 int check_ambiguity, int override,
598 int no_free, int multiple_option,
599 const char *long_opt, char short_opt,
600 const char *additional_error)
603 const char *val = value;
611 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
613 if (short_opt != '-')
614 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
615 package_name, long_opt, short_opt,
616 (additional_error ? additional_error : ""));
618 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
619 package_name, long_opt,
620 (additional_error ? additional_error : ""));
621 return 1; /* failure */
624 FIX_UNUSED (default_value);
626 if (field_given && *field_given && ! override)
633 val = possible_values[found];
637 *((int *)field) = !*((int *)field);
641 string_field = (char **)field;
642 if (!no_free && *string_field)
643 free (*string_field); /* free previous string */
644 *string_field = gengetopt_strdup (val);
652 /* store the original value */
658 if (value && orig_field) {
663 free (*orig_field); /* free previous string */
664 *orig_field = gengetopt_strdup (value);
674 cmdline_parser_internal (
675 int argc, char **argv, struct gengetopt_args_info *args_info,
676 struct cmdline_parser_params *params, const char *additional_error)
678 int c; /* Character of the parsed option. */
681 struct gengetopt_args_info local_args_info;
688 package_name = argv[0];
690 override = params->override;
691 initialize = params->initialize;
692 check_required = params->check_required;
693 check_ambiguity = params->check_ambiguity;
696 cmdline_parser_init (args_info);
698 cmdline_parser_init (&local_args_info);
702 opterr = params->print_errors;
707 int option_index = 0;
709 static struct option long_options[] = {
710 { "help", 0, NULL, 'h' },
711 { "detailed-help", 0, NULL, 0 },
712 { "version", 0, NULL, 'V' },
713 { "input", 1, NULL, 'i' },
714 { "func-name", 1, NULL, 'f' },
715 { "arg-struct-name", 1, NULL, 'a' },
716 { "file-name", 1, NULL, 'F' },
717 { "output-dir", 1, NULL, 0 },
718 { "header-output-dir", 1, NULL, 0 },
719 { "src-output-dir", 1, NULL, 0 },
720 { "c-extension", 1, NULL, 'c' },
721 { "header-extension", 1, NULL, 'H' },
722 { "long-help", 0, NULL, 'l' },
723 { "default-optional", 0, NULL, 0 },
724 { "unamed-opts", 2, NULL, 'u' },
725 { "conf-parser", 0, NULL, 'C' },
726 { "string-parser", 0, NULL, 'S' },
727 { "include-getopt", 0, NULL, 'G' },
728 { "no-handle-help", 0, NULL, 'n' },
729 { "no-help", 0, NULL, 0 },
730 { "no-handle-version", 0, NULL, 'N' },
731 { "no-version", 0, NULL, 0 },
732 { "no-handle-error", 0, NULL, 'e' },
733 { "show-required", 2, NULL, 0 },
734 { "gen-version", 0, NULL, 'g' },
735 { "set-package", 1, NULL, 0 },
736 { "set-version", 1, NULL, 0 },
737 { "show-help", 0, NULL, 0 },
738 { "show-full-help", 0, NULL, 0 },
739 { "show-detailed-help", 0, NULL, 0 },
740 { "show-version", 0, NULL, 0 },
744 c = getopt_long (argc, argv, "hVi:f:a:F:c:H:lu::CSGnNeg", long_options, &option_index);
746 if (c == -1) break; /* Exit from `while (1)' loop. */
750 case 'h': /* Print help and exit. */
754 0 , &(args_info->help_given),
755 &(local_args_info.help_given), optarg, 0, 0, ARG_NO,
756 check_ambiguity, override, 0, 0,
760 cmdline_parser_free (&local_args_info);
764 case 'V': /* Print version and exit. */
768 0 , &(args_info->version_given),
769 &(local_args_info.version_given), optarg, 0, 0, ARG_NO,
770 check_ambiguity, override, 0, 0,
774 cmdline_parser_free (&local_args_info);
778 case 'i': /* input file (default std input). */
781 if (update_arg( (void *)&(args_info->input_arg),
782 &(args_info->input_orig), &(args_info->input_given),
783 &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
784 check_ambiguity, override, 0, 0,
790 case 'f': /* name of generated function. */
793 if (update_arg( (void *)&(args_info->func_name_arg),
794 &(args_info->func_name_orig), &(args_info->func_name_given),
795 &(local_args_info.func_name_given), optarg, 0, "cmdline_parser", ARG_STRING,
796 check_ambiguity, override, 0, 0,
802 case 'a': /* name of generated args info struct. */
805 if (update_arg( (void *)&(args_info->arg_struct_name_arg),
806 &(args_info->arg_struct_name_orig), &(args_info->arg_struct_name_given),
807 &(local_args_info.arg_struct_name_given), optarg, 0, "gengetopt_args_info", ARG_STRING,
808 check_ambiguity, override, 0, 0,
809 "arg-struct-name", 'a',
814 case 'F': /* name of generated file. */
817 if (update_arg( (void *)&(args_info->file_name_arg),
818 &(args_info->file_name_orig), &(args_info->file_name_given),
819 &(local_args_info.file_name_given), optarg, 0, "cmdline", ARG_STRING,
820 check_ambiguity, override, 0, 0,
826 case 'c': /* extension of c file. */
829 if (update_arg( (void *)&(args_info->c_extension_arg),
830 &(args_info->c_extension_orig), &(args_info->c_extension_given),
831 &(local_args_info.c_extension_given), optarg, 0, "c", ARG_STRING,
832 check_ambiguity, override, 0, 0,
838 case 'H': /* extension of header file. */
841 if (update_arg( (void *)&(args_info->header_extension_arg),
842 &(args_info->header_extension_orig), &(args_info->header_extension_given),
843 &(local_args_info.header_extension_given), optarg, 0, "h", ARG_STRING,
844 check_ambiguity, override, 0, 0,
845 "header-extension", 'H',
850 case 'l': /* long usage line in help. */
854 0 , &(args_info->long_help_given),
855 &(local_args_info.long_help_given), optarg, 0, 0, ARG_NO,
856 check_ambiguity, override, 0, 0,
862 case 'u': /* accept options without names (e.g., file names). */
865 if (update_arg( (void *)&(args_info->unamed_opts_arg),
866 &(args_info->unamed_opts_orig), &(args_info->unamed_opts_given),
867 &(local_args_info.unamed_opts_given), optarg, 0, "FILES", ARG_STRING,
868 check_ambiguity, override, 0, 0,
874 case 'C': /* generate a config file parser. */
878 0 , &(args_info->conf_parser_given),
879 &(local_args_info.conf_parser_given), optarg, 0, 0, ARG_NO,
880 check_ambiguity, override, 0, 0,
886 case 'S': /* generate a string parser (the string contains the command line). */
890 0 , &(args_info->string_parser_given),
891 &(local_args_info.string_parser_given), optarg, 0, 0, ARG_NO,
892 check_ambiguity, override, 0, 0,
893 "string-parser", 'S',
898 case 'G': /* adds the code for getopt_long in the generated C file. */
902 0 , &(args_info->include_getopt_given),
903 &(local_args_info.include_getopt_given), optarg, 0, 0, ARG_NO,
904 check_ambiguity, override, 0, 0,
905 "include-getopt", 'G',
910 case 'n': /* do not handle --help|-h automatically. */
914 0 , &(args_info->no_handle_help_given),
915 &(local_args_info.no_handle_help_given), optarg, 0, 0, ARG_NO,
916 check_ambiguity, override, 0, 0,
917 "no-handle-help", 'n',
922 case 'N': /* do not handle --version|-V automatically. */
926 0 , &(args_info->no_handle_version_given),
927 &(local_args_info.no_handle_version_given), optarg, 0, 0, ARG_NO,
928 check_ambiguity, override, 0, 0,
929 "no-handle-version", 'N',
934 case 'e': /* do not exit on errors. */
938 0 , &(args_info->no_handle_error_given),
939 &(local_args_info.no_handle_error_given), optarg, 0, 0, ARG_NO,
940 check_ambiguity, override, 0, 0,
941 "no-handle-error", 'e',
946 case 'g': /* put gengetopt version in the generated file. */
949 if (update_arg((void *)&(args_info->gen_version_flag), 0, &(args_info->gen_version_given),
950 &(local_args_info.gen_version_given), optarg, 0, 0, ARG_FLAG,
951 check_ambiguity, override, 1, 0, "gen-version", 'g',
957 case 0: /* Long option with no short option */
958 /* Print help, including all details and hidden options, and exit. */
959 if (strcmp (long_options[option_index].name, "detailed-help") == 0)
964 0 , &(args_info->detailed_help_given),
965 &(local_args_info.detailed_help_given), optarg, 0, 0, ARG_NO,
966 check_ambiguity, override, 0, 0,
967 "detailed-help", '-',
970 cmdline_parser_free (&local_args_info);
974 /* output directory. */
975 else if (strcmp (long_options[option_index].name, "output-dir") == 0)
979 if (update_arg( (void *)&(args_info->output_dir_arg),
980 &(args_info->output_dir_orig), &(args_info->output_dir_given),
981 &(local_args_info.output_dir_given), optarg, 0, 0, ARG_STRING,
982 check_ambiguity, override, 0, 0,
988 /* header output directory. */
989 else if (strcmp (long_options[option_index].name, "header-output-dir") == 0)
993 if (update_arg( (void *)&(args_info->header_output_dir_arg),
994 &(args_info->header_output_dir_orig), &(args_info->header_output_dir_given),
995 &(local_args_info.header_output_dir_given), optarg, 0, 0, ARG_STRING,
996 check_ambiguity, override, 0, 0,
997 "header-output-dir", '-',
1002 /* source output directory. */
1003 else if (strcmp (long_options[option_index].name, "src-output-dir") == 0)
1007 if (update_arg( (void *)&(args_info->src_output_dir_arg),
1008 &(args_info->src_output_dir_orig), &(args_info->src_output_dir_given),
1009 &(local_args_info.src_output_dir_given), optarg, 0, 0, ARG_STRING,
1010 check_ambiguity, override, 0, 0,
1011 "src-output-dir", '-',
1016 /* by default, an option is considered optional if not specified otherwise. */
1017 else if (strcmp (long_options[option_index].name, "default-optional") == 0)
1022 0 , &(args_info->default_optional_given),
1023 &(local_args_info.default_optional_given), optarg, 0, 0, ARG_NO,
1024 check_ambiguity, override, 0, 0,
1025 "default-optional", '-',
1030 /* do not add --help|-h automatically. */
1031 else if (strcmp (long_options[option_index].name, "no-help") == 0)
1036 0 , &(args_info->no_help_given),
1037 &(local_args_info.no_help_given), optarg, 0, 0, ARG_NO,
1038 check_ambiguity, override, 0, 0,
1044 /* do not add --version|-V automatically. */
1045 else if (strcmp (long_options[option_index].name, "no-version") == 0)
1050 0 , &(args_info->no_version_given),
1051 &(local_args_info.no_version_given), optarg, 0, 0, ARG_NO,
1052 check_ambiguity, override, 0, 0,
1058 /* in the output of help will specify which options are mandatory, by using the optional passed string. */
1059 else if (strcmp (long_options[option_index].name, "show-required") == 0)
1063 if (update_arg( (void *)&(args_info->show_required_arg),
1064 &(args_info->show_required_orig), &(args_info->show_required_given),
1065 &(local_args_info.show_required_given), optarg, 0, "(mandatory)", ARG_STRING,
1066 check_ambiguity, override, 0, 0,
1067 "show-required", '-',
1072 /* set the package name (override package defined in the .ggo file). */
1073 else if (strcmp (long_options[option_index].name, "set-package") == 0)
1077 if (update_arg( (void *)&(args_info->set_package_arg),
1078 &(args_info->set_package_orig), &(args_info->set_package_given),
1079 &(local_args_info.set_package_given), optarg, 0, 0, ARG_STRING,
1080 check_ambiguity, override, 0, 0,
1086 /* set the version number (override version defined in the .ggo file). */
1087 else if (strcmp (long_options[option_index].name, "set-version") == 0)
1091 if (update_arg( (void *)&(args_info->set_version_arg),
1092 &(args_info->set_version_orig), &(args_info->set_version_given),
1093 &(local_args_info.set_version_given), optarg, 0, 0, ARG_STRING,
1094 check_ambiguity, override, 0, 0,
1100 /* show the output of --help instead of generating code. */
1101 else if (strcmp (long_options[option_index].name, "show-help") == 0)
1106 0 , &(args_info->show_help_given),
1107 &(local_args_info.show_help_given), optarg, 0, 0, ARG_NO,
1108 check_ambiguity, override, 0, 0,
1114 /* show the output of --full-help (i.e., including hidden options) instead of generating code. */
1115 else if (strcmp (long_options[option_index].name, "show-full-help") == 0)
1120 0 , &(args_info->show_full_help_given),
1121 &(local_args_info.show_full_help_given), optarg, 0, 0, ARG_NO,
1122 check_ambiguity, override, 0, 0,
1123 "show-full-help", '-',
1128 /* show the output of --detailed-help (i.e., including details and hidden options) instead of generating code. */
1129 else if (strcmp (long_options[option_index].name, "show-detailed-help") == 0)
1134 0 , &(args_info->show_detailed_help_given),
1135 &(local_args_info.show_detailed_help_given), optarg, 0, 0, ARG_NO,
1136 check_ambiguity, override, 0, 0,
1137 "show-detailed-help", '-',
1142 /* show the output of --version instead of generating code. */
1143 else if (strcmp (long_options[option_index].name, "show-version") == 0)
1148 0 , &(args_info->show_version_given),
1149 &(local_args_info.show_version_given), optarg, 0, 0, ARG_NO,
1150 check_ambiguity, override, 0, 0,
1151 "show-version", '-',
1158 case '?': /* Invalid option. */
1159 /* `getopt_long' already printed an error message. */
1162 default: /* bug: option not considered. */
1163 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1171 cmdline_parser_release (&local_args_info);
1174 return (EXIT_FAILURE);
1180 cmdline_parser_release (&local_args_info);
1181 return (EXIT_FAILURE);
1185 cmdline_parser_create_argv(const char *cmdline_, char ***argv_ptr, const char *prog_name)
1192 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1193 cmd_line_list_tmp->next = cmd_line_list;
1194 cmd_line_list = cmd_line_list_tmp;
1195 cmd_line_list->string_arg = gengetopt_strdup (prog_name);
1200 cmdline = gengetopt_strdup(cmdline_);
1203 while (p && strlen(p))
1205 j = strcspn(p, " \t");
1207 if (j && j < strlen(p))
1211 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1212 cmd_line_list_tmp->next = cmd_line_list;
1213 cmd_line_list = cmd_line_list_tmp;
1214 cmd_line_list->string_arg = gengetopt_strdup (p);
1217 p += strspn(p, " \t");
1221 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1222 cmd_line_list_tmp->next = cmd_line_list;
1223 cmd_line_list = cmd_line_list_tmp;
1224 cmd_line_list->string_arg = gengetopt_strdup (p);
1230 *argv_ptr = (char **) malloc((n + 1) * sizeof(char *));
1231 cmd_line_list_tmp = cmd_line_list;
1232 for (i = (n-1); i >= 0; --i)
1234 (*argv_ptr)[i] = cmd_line_list_tmp->string_arg;
1235 cmd_line_list_tmp = cmd_line_list_tmp->next;
1245 cmdline_parser_string(const char *cmdline, struct gengetopt_args_info *args_info, const char *prog_name)
1247 return cmdline_parser_string2(cmdline, args_info, prog_name, 0, 1, 1);
1251 cmdline_parser_string2(const char *cmdline, struct gengetopt_args_info *args_info, const char *prog_name,
1252 int override, int initialize, int check_required)
1254 struct cmdline_parser_params params;
1256 params.override = override;
1257 params.initialize = initialize;
1258 params.check_required = check_required;
1259 params.check_ambiguity = 0;
1260 params.print_errors = 1;
1262 return cmdline_parser_string_ext(cmdline, args_info, prog_name, ¶ms);
1266 cmdline_parser_string_ext(const char *cmdline, struct gengetopt_args_info *args_info, const char *prog_name,
1267 struct cmdline_parser_params *params)
1269 char **argv_ptr = 0;
1273 argc = cmdline_parser_create_argv(cmdline, &argv_ptr, prog_name);
1276 cmdline_parser_internal (argc, argv_ptr, args_info, params, 0);