1995-09-30 18:10:48 +01:00
|
|
|
#include <getopt.h>
|
2000-09-06 20:07:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1995-02-18 02:27:10 +01:00
|
|
|
|
1995-09-30 18:10:48 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
1995-02-18 02:27:10 +01:00
|
|
|
{
|
1995-09-30 18:10:48 +01:00
|
|
|
static const struct option options[] =
|
|
|
|
{
|
|
|
|
{"required", required_argument, NULL, 'r'},
|
|
|
|
{"optional", optional_argument, NULL, 'o'},
|
1998-08-17 18:48:17 +02:00
|
|
|
{"none", no_argument, NULL, 'n'},
|
2000-09-06 20:07:07 +02:00
|
|
|
{"color", no_argument, NULL, 'C'},
|
|
|
|
{"colour", no_argument, NULL, 'C'},
|
1998-08-17 18:48:17 +02:00
|
|
|
{NULL, 0, NULL, 0 }
|
1995-09-30 18:10:48 +01:00
|
|
|
};
|
1998-08-17 18:48:17 +02:00
|
|
|
|
1995-02-18 02:27:10 +01:00
|
|
|
int aflag = 0;
|
|
|
|
int bflag = 0;
|
|
|
|
char *cvalue = NULL;
|
2000-09-06 20:07:07 +02:00
|
|
|
int Cflag = 0;
|
|
|
|
int nflag = 0;
|
1995-02-18 02:27:10 +01:00
|
|
|
int index;
|
|
|
|
int c;
|
2000-09-06 20:07:07 +02:00
|
|
|
int result = 0;
|
1995-02-18 02:27:10 +01:00
|
|
|
|
1995-09-30 18:10:48 +01:00
|
|
|
while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cvalue = optarg;
|
|
|
|
break;
|
2000-09-06 20:07:07 +02:00
|
|
|
case 'C':
|
|
|
|
++Cflag;
|
|
|
|
break;
|
1995-09-30 18:10:48 +01:00
|
|
|
case '?':
|
|
|
|
fputs ("Unknown option.\n", stderr);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "This should never happen!\n");
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
printf ("--required %s\n", optarg);
|
2000-09-06 20:07:07 +02:00
|
|
|
result |= strcmp (optarg, "foobar") != 0;
|
1995-09-30 18:10:48 +01:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
printf ("--optional %s\n", optarg);
|
2000-09-06 20:07:07 +02:00
|
|
|
result |= optarg == NULL || strcmp (optarg, "bazbug") != 0;
|
1995-09-30 18:10:48 +01:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
puts ("--none");
|
2000-09-06 20:07:07 +02:00
|
|
|
nflag = 1;
|
1995-09-30 18:10:48 +01:00
|
|
|
break;
|
|
|
|
}
|
1995-02-18 02:27:10 +01:00
|
|
|
|
2000-09-06 20:07:07 +02:00
|
|
|
printf ("aflag = %d, bflag = %d, cvalue = %s, Cflags = %d, nflag = %d\n",
|
|
|
|
aflag, bflag, cvalue, Cflag, nflag);
|
|
|
|
|
|
|
|
result |= (aflag != 1 || bflag != 1 || cvalue == NULL
|
|
|
|
|| strcmp (cvalue, "foobar") != 0 || Cflag != 3 || nflag != 1);
|
1995-02-18 02:27:10 +01:00
|
|
|
|
|
|
|
for (index = optind; index < argc; index++)
|
|
|
|
printf ("Non-option argument %s\n", argv[index]);
|
1995-09-30 18:10:48 +01:00
|
|
|
|
2000-09-06 20:07:07 +02:00
|
|
|
result |= optind + 1 != argc || strcmp (argv[optind], "random") != 0;
|
|
|
|
|
|
|
|
return result;
|
1995-02-18 02:27:10 +01:00
|
|
|
}
|