658
658
char * str_opt_arg; /**< @brief A string option, for a filename. */
659
659
char * str_opt_orig; /**< @brief A string option, for a filename original value given at command line. */
660
660
const char *str_opt_help; /**< @brief A string option, for a filename help description. */
661
int my_opt_arg; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break. */
662
char * my_opt_orig; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break original value given at command line. */
663
const char *my_opt_help; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break help description. */
661
int my_opt_arg; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I
662
require a line break. */
663
char * my_opt_orig; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I
664
require a line break original value given at command line. */
665
const char *my_opt_help; /**< @brief Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I
666
require a line break help description. */
664
667
int int_opt_arg; /**< @brief A int option. */
665
668
char * int_opt_orig; /**< @brief A int option original value given at command line. */
666
669
const char *int_opt_help; /**< @brief A int option help description. */
939
942
And here's how these functions can be used inside the main program:
945
/* we try to use gengetopt generated file in a C++ program */
946
/* we don't use autoconf and automake vars */
951
#include "cmdline1.h"
957
main (int argc, char **argv)
959
gengetopt_args_info args_info;
961
cout << "This one is from a C++ program" << endl ;
962
cout << "Try to launch me with some options" << endl ;
963
cout << "(type sample1 --help for the complete list)" << endl ;
964
cout << "For example: ./sample1 *.* --funct-opt" << endl ;
966
/* let's call our cmdline parser */
967
if (cmdline_parser (argc, argv, &args_info) != 0)
970
cout << "Here are the options you passed..." << endl;
972
for ( unsigned i = 0 ; i < args_info.inputs_num ; ++i )
973
cout << "file: " << args_info.inputs[i] << endl ;
975
if ( args_info.funct_opt_given )
976
cout << "You chose --funct-opt or -F." << endl ;
978
if ( args_info.str_opt_given )
979
cout << "You inserted " << args_info.str_opt_arg << " for " <<
980
"--str-opt option." << endl ;
982
if ( args_info.int_opt_given )
983
cout << "This is the integer you input: " <<
984
args_info.int_opt_arg << "." << endl;
986
if (args_info.flag_opt_given)
987
cout << "The flag option was given!" << endl;
989
cout << "The flag is " << ( args_info.flag_opt_flag ? "on" : "off" ) <<
992
if (args_info.enum_opt_given) {
993
cout << "enum-opt value: " << args_info.enum_opt_arg << endl;
994
cout << "enum-opt (original specified) value: "
995
<< args_info.enum_opt_orig << endl;
998
if (args_info.secret_given)
999
cout << "Secret option was specified: " << args_info.secret_arg
1002
cout << args_info.def_opt_arg << "! ";
1004
cout << "Have a nice day! :-)" << endl ;
1006
cmdline_parser_free (&args_info); /* release allocated memory */
941
1011
Now you can compile `main1.cc' and the `cmdline1.c' generated by
942
1012
gengetopt and link all together to obtain `sample1' executable:
1567
1637
in that case, a C `enum' type is also generated with name
1568
1638
`enum_<option>'; the values of such C enum will be generated according
1569
1639
this pattern: `<option>_arg_<value>', where `value' is the value
1570
specified in the input file, and the starting value is always 0. For
1571
instance, if we specify in the input file the following option
1640
specified in the input file, and the starting value is always 0. An
1641
additional value is generated to represent the null/empty value, with
1642
the pattern `<option>__NULL' (note the double underscore) with integer
1643
value -1. For instance, if we specify in the input file the following
1573
1646
option "myopt" ... ... values="FOO","180","BAR" enum ...
1575
1648
then the following C enum will be generated:
1577
enum enum_myopt { myopt_arg_FOO = 0, myopt_arg_180, myopt_arg_BAR };
1651
myopt__NULL = -1, myopt_arg_FOO = 0, myopt_arg_180, myopt_arg_BAR };
1579
1653
If you use the symbols `+' and `-', these will be translated into
1580
1654
`PLUS_' and `MINUS_', respectively, in the the C enum. Thus, if we
1672
1747
For instance, let us consider the file `test_modes_cmd.ggo':
1749
package "test_modes"
1752
section "some non mode options"
1754
option "no-mode" N "a generic option not beloging to any mode" optional
1755
option "no-mode2" - "another generic option not beloging to any mode" string optional
1757
section "some modes just for testing"
1761
defmode "mode1" modedesc="any option of this mode is in contrast with any \
1762
option of the other mode\nNotice that this description is quite long so \
1763
it may spawn many lines... \
1764
fortunately gengetopt will wrap it for you :-)"
1766
modeoption "opta" a "string a" multiple mode="mode1" optional
1767
modeoption "optA" A "string A" string argoptional mode="mode1" required
1768
modeoption "optAmul" M "string M"
1769
argoptional string mode="mode1" multiple optional
1770
modeoption "optb" b "string b" mode="mode1" optional
1772
modeoption "optc" - "string c" mode="mode 2" optional
1773
modeoption "optd" d "string d" mode="mode 2" required
1775
modeoption "mopt" m "option of my mode" int optional mode="my mode" optional
1674
1777
Now, we use the program `test_modes' (that uses the generated parser
1675
1778
for the input file above) to demonstrate how the parser generated by
1676
1779
gengetopt perform checks on mode options.
1905
2008
For instance here's a program that uses this feature (this is the
1906
2009
test `test_conf_parser'):
2011
/* test_conf_parser.c test */
2013
/* test all kinds of options and the conf file parser */
2018
#include "test_conf_parser_cmd.h"
2020
static struct my_args_info args_info;
2023
main (int argc, char **argv)
2028
struct test_conf_parser_cmd_parser_params *params;
2030
/* initialize the parameters structure */
2031
params = test_conf_parser_cmd_parser_params_create();
2033
/* call the command line parser */
2034
if (test_conf_parser_cmd_parser (argc, argv, &args_info) != 0) {
2040
override command line options,
2041
but do not initialize args_info, check for required options.
2042
NOTICE: we must NOT skip the 0 assignment to initialize,
2043
since its default value is 1 and override defaults to 0
2044
while check_required is already set to its default value, 1
2046
params->initialize = 0;
2047
params->override = 1;
2049
/* call the config file parser */
2050
if (test_conf_parser_cmd_parser_config_file
2051
(args_info.conf_file_arg, &args_info, params) != 0)
2057
printf ("value of required: %s\n", args_info.required_arg);
2058
printf ("value of string: %s\n", args_info.string_arg);
2059
printf ("value of no-short_given: %d\n", args_info.no_short_given);
2060
printf ("value of int: %d\n", args_info.int_arg);
2061
printf ("value of float: %f\n", args_info.float_arg);
2063
printf ("value of multi-string_given: %d\n", args_info.multi_string_given);
2064
for (i = 0; i < args_info.multi_string_given; i++)
2065
printf (" value of multi-string: %s\n", args_info.multi_string_arg [i]);
2067
printf ("value of multi-string-def_given: %d\n",
2068
args_info.multi_string_def_given);
2069
for (i = 0; i < args_info.multi_string_def_given; ++i)
2070
printf (" value of multi-string-def: %s\n",
2071
args_info.multi_string_def_arg [i]);
2072
if (!args_info.multi_string_def_given && args_info.multi_string_def_arg [0])
2073
printf ("default value of multi-string-def: %s\n",
2074
args_info.multi_string_def_arg [0]);
2076
printf ("value of opta: %s\n", args_info.opta_arg);
2078
printf ("noarg given %d times\n", args_info.noarg_given);
2079
printf ("noarg_noshort given %d times\n", args_info.noarg_noshort_given);
2081
printf ("opt-arg given: %d\n", args_info.opt_arg_given);
2082
printf ("opt-arg value: %s\n", (args_info.opt_arg_arg ? args_info.opt_arg_arg : "not given"));
2084
if (args_info.file_save_given) {
2085
if (test_conf_parser_cmd_parser_file_save (args_info.file_save_arg, &args_info) == EXIT_FAILURE)
2088
printf ("saved configuration file %s\n", args_info.file_save_arg);
2092
/* deallocate structures */
2093
test_conf_parser_cmd_parser_free (&args_info);
1908
2099
So if we use the following config file
1910
2101
# required option
2213
2430
These last two files are processed with gengetopt using the
2214
2431
`--string-parser'. Let's put everything together in this main file:
2436
#include "test_main_cmdline_cmd.h"
2437
#include "test_first_cmdline_cmd.h"
2438
#include "test_second_cmdline_cmd.h"
2441
main(int argc, char **argv)
2443
struct gengetopt_args_info main_args_info;
2444
struct test_first_cmdline_cmd_struct first_args_info;
2445
struct test_second_cmdline_cmd_struct second_args_info;
2449
if (test_main_cmdline_cmd_parser (argc, argv, &main_args_info) != 0) {
2454
for (j = 0; j < main_args_info.second_cmd_given; ++j) {
2455
printf("second cmdline: %s\n", main_args_info.second_cmd_arg[j]);
2456
if (test_second_cmdline_cmd_parser_string
2457
(main_args_info.second_cmd_arg[j], &second_args_info, argv[0]) == 0) {
2458
if (second_args_info.option_a_given)
2459
printf(" --option-a: %s\n", second_args_info.option_a_arg);
2460
if (second_args_info.option_b_given)
2461
printf(" --option-b: %s\n", second_args_info.option_b_arg);
2462
for (i = 0; i < second_args_info.my_multi_given; ++i)
2463
printf(" --my-multi: %s\n", second_args_info.my_multi_arg[i]);
2465
test_second_cmdline_cmd_parser_free (&second_args_info);
2469
for (j = 0; j < main_args_info.first_cmd_given; ++j) {
2470
printf("first cmdline: %s\n", main_args_info.first_cmd_arg[j]);
2471
if (test_first_cmdline_cmd_parser_string
2472
(main_args_info.first_cmd_arg[j], &first_args_info, argv[0]) == 0) {
2473
if (first_args_info.option_a_given)
2474
printf(" --option-a: %d\n", first_args_info.option_a_arg);
2475
for (i = 0; i < first_args_info.multi_given; ++i)
2476
printf(" --multi: %s\n", first_args_info.multi_arg[i]);
2478
test_first_cmdline_cmd_parser_free (&first_args_info);
2482
test_main_cmdline_cmd_parser_free (&main_args_info);
2216
2487
Notice that in the `for' loops we always free the elements of the
2217
2488
argument structures in order to avoid memory leaks.
2734
Node: Installation5917
2735
Ref: Installation-Footnote-110744
2736
Ref: Installation-Footnote-210860
2737
Ref: Installation-Footnote-310907
2738
Ref: Installation-Footnote-410954
2739
Ref: Installation-Footnote-511000
2740
Node: Basic Usage11045
2743
Ref: addedoptions19795
2744
Ref: showrequired44404
2745
Ref: helpoutput44654
2746
Ref: Basic Usage-Footnote-150310
2747
Ref: Basic Usage-Footnote-250338
2748
Ref: Basic Usage-Footnote-350366
2749
Ref: Basic Usage-Footnote-450393
2750
Ref: Basic Usage-Footnote-550421
2751
Ref: Basic Usage-Footnote-650474
2752
Ref: Basic Usage-Footnote-750502
2753
Ref: Basic Usage-Footnote-850530
2754
Ref: Basic Usage-Footnote-950558
2755
Ref: Basic Usage-Footnote-1050586
2756
Ref: Basic Usage-Footnote-1150703
2757
Ref: Basic Usage-Footnote-1250732
2758
Ref: Basic Usage-Footnote-1350853
2759
Ref: Basic Usage-Footnote-1450956
2760
Ref: Basic Usage-Footnote-1550985
2761
Ref: Basic Usage-Footnote-1651014
2762
Ref: Basic Usage-Footnote-1751043
2763
Ref: Basic Usage-Footnote-1851097
2764
Ref: Basic Usage-Footnote-1951166
2765
Ref: Basic Usage-Footnote-2051286
2766
Ref: Basic Usage-Footnote-2151351
2767
Ref: Basic Usage-Footnote-2251435
2768
Ref: Basic Usage-Footnote-2351494
2769
Node: Invoking gengetopt51553
2770
Ref: Invoking gengetopt-Footnote-161480
2771
Ref: Invoking gengetopt-Footnote-261508
2772
Ref: Invoking gengetopt-Footnote-361538
2773
Node: Terminology61566
2774
Node: Options with enumerated values66456
2775
Node: Group options69058
2776
Node: Mode options71112
2777
Node: Parser function additional parameters75371
2778
Ref: Parser function additional parameters-Footnote-178486
2779
Ref: Parser function additional parameters-Footnote-278514
2780
Ref: Parser function additional parameters-Footnote-378549
2781
Node: Configuration files79306
2782
Ref: Configuration files-Footnote-186574
2783
Ref: Configuration files-Footnote-286765
2784
Node: Multiple Options86931
2785
Node: String Parsers and Multiple Parsers89932
2786
Ref: String Parsers and Multiple Parsers-Footnote-195601
2787
Node: No getopt_long95774
2788
Node: Include the getopt_long code into the parser96508
2789
Node: Use automake/autoconf97777
2790
Node: Use Gnulib98649
2791
Ref: Use Gnulib-Footnote-1102780
2792
Node: Use getopt_long sources102825
2794
Node: Getopt and subsequent calls105784
2795
Ref: Getopt and subsequent calls-Footnote-1107711
2796
Node: Mailing Lists107957
3005
Node: Installation5919
3006
Ref: Installation-Footnote-110746
3007
Ref: Installation-Footnote-210862
3008
Ref: Installation-Footnote-310909
3009
Ref: Installation-Footnote-410956
3010
Ref: Installation-Footnote-511002
3011
Node: Basic Usage11047
3014
Ref: addedoptions19797
3015
Ref: showrequired46479
3016
Ref: helpoutput46729
3017
Ref: Basic Usage-Footnote-152385
3018
Ref: Basic Usage-Footnote-252413
3019
Ref: Basic Usage-Footnote-352441
3020
Ref: Basic Usage-Footnote-452468
3021
Ref: Basic Usage-Footnote-552496
3022
Ref: Basic Usage-Footnote-652549
3023
Ref: Basic Usage-Footnote-752577
3024
Ref: Basic Usage-Footnote-852605
3025
Ref: Basic Usage-Footnote-952633
3026
Ref: Basic Usage-Footnote-1052661
3027
Ref: Basic Usage-Footnote-1152778
3028
Ref: Basic Usage-Footnote-1252807
3029
Ref: Basic Usage-Footnote-1352928
3030
Ref: Basic Usage-Footnote-1453031
3031
Ref: Basic Usage-Footnote-1553060
3032
Ref: Basic Usage-Footnote-1653089
3033
Ref: Basic Usage-Footnote-1753118
3034
Ref: Basic Usage-Footnote-1853172
3035
Ref: Basic Usage-Footnote-1953241
3036
Ref: Basic Usage-Footnote-2053361
3037
Ref: Basic Usage-Footnote-2153426
3038
Ref: Basic Usage-Footnote-2253510
3039
Ref: Basic Usage-Footnote-2353569
3040
Node: Invoking gengetopt53628
3041
Ref: Invoking gengetopt-Footnote-163555
3042
Ref: Invoking gengetopt-Footnote-263583
3043
Ref: Invoking gengetopt-Footnote-363613
3044
Node: Terminology63641
3045
Node: Options with enumerated values68531
3046
Node: Group options71352
3047
Node: Mode options73406
3048
Node: Parser function additional parameters78775
3049
Ref: Parser function additional parameters-Footnote-181890
3050
Ref: Parser function additional parameters-Footnote-281918
3051
Ref: Parser function additional parameters-Footnote-381953
3052
Node: Configuration files82710
3053
Ref: Configuration files-Footnote-193085
3054
Ref: Configuration files-Footnote-293276
3055
Node: Multiple Options93442
3056
Node: String Parsers and Multiple Parsers97049
3057
Ref: String Parsers and Multiple Parsers-Footnote-1104749
3058
Node: No getopt_long104922
3059
Node: Include the getopt_long code into the parser105656
3060
Node: Use automake/autoconf106925
3061
Node: Use Gnulib107797
3062
Ref: Use Gnulib-Footnote-1111928
3063
Node: Use getopt_long sources111973
3065
Node: Getopt and subsequent calls114956
3066
Ref: Getopt and subsequent calls-Footnote-1116883
3067
Node: Mailing Lists117129