|
Packit |
6c4009 |
/* Example of Parsing Long Options with getopt_long.
|
|
Packit |
6c4009 |
Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
This program is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU General Public License
|
|
Packit |
6c4009 |
as published by the Free Software Foundation; either version 2
|
|
Packit |
6c4009 |
of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
6c4009 |
GNU General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU General Public License
|
|
Packit |
6c4009 |
along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <getopt.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Flag set by @samp{--verbose}. */
|
|
Packit |
6c4009 |
static int verbose_flag;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (int argc, char **argv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int c;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
static struct option long_options[] =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* These options set a flag. */
|
|
Packit |
6c4009 |
{"verbose", no_argument, &verbose_flag, 1},
|
|
Packit |
6c4009 |
{"brief", no_argument, &verbose_flag, 0},
|
|
Packit |
6c4009 |
/* These options don't set a flag.
|
|
Packit |
6c4009 |
We distinguish them by their indices. */
|
|
Packit |
6c4009 |
{"add", no_argument, 0, 'a'},
|
|
Packit |
6c4009 |
{"append", no_argument, 0, 'b'},
|
|
Packit |
6c4009 |
{"delete", required_argument, 0, 'd'},
|
|
Packit |
6c4009 |
{"create", required_argument, 0, 'c'},
|
|
Packit |
6c4009 |
{"file", required_argument, 0, 'f'},
|
|
Packit |
6c4009 |
{0, 0, 0, 0}
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
/* @code{getopt_long} stores the option index here. */
|
|
Packit |
6c4009 |
int option_index = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
c = getopt_long (argc, argv, "abc:d:f:",
|
|
Packit |
6c4009 |
long_options, &option_index);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Detect the end of the options. */
|
|
Packit |
6c4009 |
if (c == -1)
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
switch (c)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
/* If this option set a flag, do nothing else now. */
|
|
Packit |
6c4009 |
if (long_options[option_index].flag != 0)
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
printf ("option %s", long_options[option_index].name);
|
|
Packit |
6c4009 |
if (optarg)
|
|
Packit |
6c4009 |
printf (" with arg %s", optarg);
|
|
Packit |
6c4009 |
printf ("\n");
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 'a':
|
|
Packit |
6c4009 |
puts ("option -a\n");
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 'b':
|
|
Packit |
6c4009 |
puts ("option -b\n");
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 'c':
|
|
Packit |
6c4009 |
printf ("option -c with value `%s'\n", optarg);
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 'd':
|
|
Packit |
6c4009 |
printf ("option -d with value `%s'\n", optarg);
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case 'f':
|
|
Packit |
6c4009 |
printf ("option -f with value `%s'\n", optarg);
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case '?':
|
|
Packit |
6c4009 |
/* @code{getopt_long} already printed an error message. */
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
abort ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Instead of reporting @samp{--verbose}
|
|
Packit |
6c4009 |
and @samp{--brief} as they are encountered,
|
|
Packit |
6c4009 |
we report the final status resulting from them. */
|
|
Packit |
6c4009 |
if (verbose_flag)
|
|
Packit |
6c4009 |
puts ("verbose flag is set");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Print any remaining command line arguments (not options). */
|
|
Packit |
6c4009 |
if (optind < argc)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("non-option ARGV-elements: ");
|
|
Packit |
6c4009 |
while (optind < argc)
|
|
Packit |
6c4009 |
printf ("%s ", argv[optind++]);
|
|
Packit |
6c4009 |
putchar ('\n');
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
exit (0);
|
|
Packit |
6c4009 |
}
|