Update to 2.1.x development version
[glibc/history.git] / posix / tstgetopt.c
blob2258b20770a953e9de08684a6b9c765155b39947
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'}
15 int aflag = 0;
16 int bflag = 0;
17 char *cvalue = NULL;
18 int index;
19 int c;
21 while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
22 switch (c)
24 case 'a':
25 aflag = 1;
26 break;
27 case 'b':
28 bflag = 1;
29 break;
30 case 'c':
31 cvalue = optarg;
32 break;
33 case '?':
34 fputs ("Unknown option.\n", stderr);
35 return 1;
36 default:
37 fprintf (stderr, "This should never happen!\n");
38 return 1;
40 case 'r':
41 printf ("--required %s\n", optarg);
42 break;
43 case 'o':
44 printf ("--optional %s\n", optarg);
45 break;
46 case 'n':
47 puts ("--none");
48 break;
51 printf ("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);
53 for (index = optind; index < argc; index++)
54 printf ("Non-option argument %s\n", argv[index]);
56 return 0;