5 /* Flag set by @samp{--verbose}. */
6 static int verbose_flag
;
17 static struct option long_options
[] =
19 /* These options set a flag. */
20 {"verbose", no_argument
, &verbose_flag
, 1},
21 {"brief", no_argument
, &verbose_flag
, 0},
22 /* These options don't set a flag.
23 We distinguish them by their indices. */
24 {"add", no_argument
, 0, 'a'},
25 {"append", no_argument
, 0, 'b'},
26 {"delete", required_argument
, 0, 'd'},
27 {"create", required_argument
, 0, 'c'},
28 {"file", required_argument
, 0, 'f'},
31 /* @code{getopt_long} stores the option index here. */
34 c
= getopt_long (argc
, argv
, "abc:d:f:",
35 long_options
, &option_index
);
37 /* Detect the end of the options. */
44 /* If this option set a flag, do nothing else now. */
45 if (long_options
[option_index
].flag
!= 0)
47 printf ("option %s", long_options
[option_index
].name
);
49 printf (" with arg %s", optarg
);
62 printf ("option -c with value `%s'\n", optarg
);
66 printf ("option -d with value `%s'\n", optarg
);
70 printf ("option -f with value `%s'\n", optarg
);
74 /* @code{getopt_long} already printed an error message. */
82 /* Instead of reporting @samp{--verbose}
83 and @samp{--brief} as they are encountered,
84 we report the final status resulting from them. */
86 puts ("verbose flag is set");
88 /* Print any remaining command line arguments (not options). */
91 printf ("non-option ARGV-elements: ");
93 printf ("%s ", argv
[optind
++]);