Using getopt to parse arguments
I have prepared a small sample program that accepts the same command
line arguments as spice. I intend to integrate this into main.c
taking the place of the current command line argument handling.
To show you how it looks like, I have put it in an attachement for
your viewing pleasure. If there are platforms that fail to compile
this program, I would like to know. In that case, I will have to
include an implementation that is BSD license compatible.
Regards,
--
Arno
#include <stdio.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <getopt.h>
int
main (int argc, char **argv)
{
int c;
int digit_optind = 0;
/* spice [ -n ] [ -t term ] [ -r rawfile] [ -b ] [ -i ] [input
file ... ] */
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"batch", 0, 0, 'b'},
{"interactive", 0, 0, 'i'},
{"noinit", 0, 0, 'n'},
{"rawfile", 1, 0, 'r'},
{"server", 0, 0, 's'},
{"terminal", 1, 0, 't'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "binr:st:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'b':
printf ("enabling batch mode processing\n");
break;
case 'i':
printf ("enabling interactive mode\n");
break;
case 'n':
printf ("not loading ~/.spiceinit file\n");
break;
case 'r':
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 's':
printf ("enabling server mode\n");
break;
case 't':
printf ("setting terminal to");
if (optarg)
printf (" %s", optarg);
printf ("\n");
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}
PGP signature
Partial thread listing: