Update.
[glibc.git] / posix / tstgetopt.c
blob40cf3d9c82a8a1469b4eb1ac6e43826f21c2d77b
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <getopt.h>
5 int
6 main (int argc, char **argv)
8 static const struct option options[] =
10 {"required", required_argument, NULL, 'r'},
11 {"optional", optional_argument, NULL, 'o'},
12 {"none", no_argument, NULL, 'n'},
13 {NULL, 0, NULL, 0 }
16 int aflag = 0;
17 int bflag = 0;
18 char *cvalue = NULL;
19 int index;
20 int c;
22 while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
23 switch (c)
25 case 'a':
26 aflag = 1;
27 break;
28 case 'b':
29 bflag = 1;
30 break;
31 case 'c':
32 cvalue = optarg;
33 break;
34 case '?':
35 fputs ("Unknown option.\n", stderr);
36 return 1;
37 default:
38 fprintf (stderr, "This should never happen!\n");
39 return 1;
41 case 'r':
42 printf ("--required %s\n", optarg);
43 break;
44 case 'o':
45 printf ("--optional %s\n", optarg);
46 break;
47 case 'n':
48 puts ("--none");
49 break;
52 printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);
54 for (index = optind; index < argc; index++)
55 printf ("Non-option argument %s\n", argv[index]);
57 return 0;