1 /* Example of Parsing Long Options with getopt_long.
2    Copyright (C) 1991-2021 Free Software Foundation, Inc.
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
15    along with this program; if not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <getopt.h>
21 
22 /* Flag set by @samp{--verbose}.  */
23 static int verbose_flag;
24 
25 int
main(int argc,char ** argv)26 main (int argc, char **argv)
27 {
28   int c;
29 
30   while (1)
31     {
32       static struct option long_options[] =
33 	{
34 	  /* These options set a flag.  */
35 	  {"verbose", no_argument,       &verbose_flag, 1},
36 	  {"brief",   no_argument,       &verbose_flag, 0},
37 	  /* These options don't set a flag.
38 	     We distinguish them by their indices.  */
39 	  {"add",     no_argument,       0, 'a'},
40 	  {"append",  no_argument,       0, 'b'},
41 	  {"delete",  required_argument, 0, 'd'},
42 	  {"create",  required_argument, 0, 'c'},
43 	  {"file",    required_argument, 0, 'f'},
44 	  {0, 0, 0, 0}
45 	};
46       /* @code{getopt_long} stores the option index here.  */
47       int option_index = 0;
48 
49       c = getopt_long (argc, argv, "abc:d:f:",
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 
84 	case 'f':
85 	  printf ("option -f with value `%s'\n", optarg);
86 	  break;
87 
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 }
114