]>
Commit | Line | Data |
---|---|---|
d9a17c07 | 1 | /* Example of Parsing Long Options with getopt_long. |
04277e02 | 2 | Copyright (C) 1991-2019 Free Software Foundation, Inc. |
d9a17c07 RM |
3 | |
4 | This program is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU General Public License | |
6 | as published by the Free Software Foundation; either version 2 | |
7 | of the License, or (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
5a82c748 | 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. |
d9a17c07 RM |
16 | */ |
17 | ||
28f540f4 | 18 | #include <stdio.h> |
46d9215f RM |
19 | #include <stdlib.h> |
20 | #include <getopt.h> | |
28f540f4 RM |
21 | |
22 | /* Flag set by @samp{--verbose}. */ | |
23 | static int verbose_flag; | |
24 | ||
25 | int | |
09c093b5 | 26 | main (int argc, char **argv) |
28f540f4 RM |
27 | { |
28 | int c; | |
29 | ||
30 | while (1) | |
31 | { | |
32 | static struct option long_options[] = | |
33 | { | |
34 | /* These options set a flag. */ | |
9495793d UD |
35 | {"verbose", no_argument, &verbose_flag, 1}, |
36 | {"brief", no_argument, &verbose_flag, 0}, | |
28f540f4 RM |
37 | /* These options don't set a flag. |
38 | We distinguish them by their indices. */ | |
941cee27 | 39 | {"add", no_argument, 0, 'a'}, |
9495793d UD |
40 | {"append", no_argument, 0, 'b'}, |
41 | {"delete", required_argument, 0, 'd'}, | |
941cee27 | 42 | {"create", required_argument, 0, 'c'}, |
9495793d | 43 | {"file", required_argument, 0, 'f'}, |
28f540f4 RM |
44 | {0, 0, 0, 0} |
45 | }; | |
46 | /* @code{getopt_long} stores the option index here. */ | |
47 | int option_index = 0; | |
48 | ||
9495793d | 49 | c = getopt_long (argc, argv, "abc:d:f:", |
28f540f4 RM |
50 | long_options, &option_index); |
51 | ||
52 | /* Detect the end of the options. */ | |
53 | if (c == -1) | |
54 | break; | |
55 | ||
56 | switch (c) | |
57 | { | |
58 | case 0: | |
59 | /* If this option set a flag, do nothing else now. */ | |
60 | if (long_options[option_index].flag != 0) | |
61 | break; | |
62 | printf ("option %s", long_options[option_index].name); | |
63 | if (optarg) | |
64 | printf (" with arg %s", optarg); | |
65 | printf ("\n"); | |
66 | break; | |
67 | ||
68 | case 'a': | |
69 | puts ("option -a\n"); | |
70 | break; | |
71 | ||
72 | case 'b': | |
73 | puts ("option -b\n"); | |
74 | break; | |
75 | ||
76 | case 'c': | |
77 | printf ("option -c with value `%s'\n", optarg); | |
78 | break; | |
79 | ||
80 | case 'd': | |
81 | printf ("option -d with value `%s'\n", optarg); | |
82 | break; | |
83 | ||
9495793d UD |
84 | case 'f': |
85 | printf ("option -f with value `%s'\n", optarg); | |
86 | break; | |
87 | ||
28f540f4 RM |
88 | case '?': |
89 | /* @code{getopt_long} already printed an error message. */ | |
90 | break; | |
91 | ||
92 | default: | |
93 | abort (); | |
94 | } | |
95 | } | |
96 | ||
97 | /* Instead of reporting @samp{--verbose} | |
98 | and @samp{--brief} as they are encountered, | |
99 | we report the final status resulting from them. */ | |
100 | if (verbose_flag) | |
101 | puts ("verbose flag is set"); | |
102 | ||
103 | /* Print any remaining command line arguments (not options). */ | |
104 | if (optind < argc) | |
105 | { | |
106 | printf ("non-option ARGV-elements: "); | |
107 | while (optind < argc) | |
108 | printf ("%s ", argv[optind++]); | |
109 | putchar ('\n'); | |
110 | } | |
111 | ||
112 | exit (0); | |
113 | } |