]>
Commit | Line | Data |
---|---|---|
41cfadd6 | 1 | #include <getopt.h> |
b64cd08a UD |
2 | #include <stdio.h> |
3 | #include <string.h> | |
4 | #include <unistd.h> | |
28f540f4 | 5 | |
41cfadd6 RM |
6 | int |
7 | main (int argc, char **argv) | |
28f540f4 | 8 | { |
41cfadd6 RM |
9 | static const struct option options[] = |
10 | { | |
11 | {"required", required_argument, NULL, 'r'}, | |
12 | {"optional", optional_argument, NULL, 'o'}, | |
364ff81f | 13 | {"none", no_argument, NULL, 'n'}, |
b64cd08a UD |
14 | {"color", no_argument, NULL, 'C'}, |
15 | {"colour", no_argument, NULL, 'C'}, | |
364ff81f | 16 | {NULL, 0, NULL, 0 } |
41cfadd6 | 17 | }; |
364ff81f | 18 | |
28f540f4 RM |
19 | int aflag = 0; |
20 | int bflag = 0; | |
21 | char *cvalue = NULL; | |
b64cd08a UD |
22 | int Cflag = 0; |
23 | int nflag = 0; | |
28f540f4 RM |
24 | int index; |
25 | int c; | |
b64cd08a | 26 | int result = 0; |
28f540f4 | 27 | |
41cfadd6 RM |
28 | while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0) |
29 | switch (c) | |
30 | { | |
31 | case 'a': | |
32 | aflag = 1; | |
33 | break; | |
34 | case 'b': | |
35 | bflag = 1; | |
36 | break; | |
37 | case 'c': | |
38 | cvalue = optarg; | |
39 | break; | |
b64cd08a UD |
40 | case 'C': |
41 | ++Cflag; | |
42 | break; | |
41cfadd6 RM |
43 | case '?': |
44 | fputs ("Unknown option.\n", stderr); | |
45 | return 1; | |
46 | default: | |
47 | fprintf (stderr, "This should never happen!\n"); | |
48 | return 1; | |
49 | ||
50 | case 'r': | |
51 | printf ("--required %s\n", optarg); | |
b64cd08a | 52 | result |= strcmp (optarg, "foobar") != 0; |
41cfadd6 RM |
53 | break; |
54 | case 'o': | |
55 | printf ("--optional %s\n", optarg); | |
b64cd08a | 56 | result |= optarg == NULL || strcmp (optarg, "bazbug") != 0; |
41cfadd6 RM |
57 | break; |
58 | case 'n': | |
59 | puts ("--none"); | |
b64cd08a | 60 | nflag = 1; |
41cfadd6 RM |
61 | break; |
62 | } | |
28f540f4 | 63 | |
b64cd08a UD |
64 | printf ("aflag = %d, bflag = %d, cvalue = %s, Cflags = %d, nflag = %d\n", |
65 | aflag, bflag, cvalue, Cflag, nflag); | |
66 | ||
67 | result |= (aflag != 1 || bflag != 1 || cvalue == NULL | |
68 | || strcmp (cvalue, "foobar") != 0 || Cflag != 3 || nflag != 1); | |
28f540f4 RM |
69 | |
70 | for (index = optind; index < argc; index++) | |
71 | printf ("Non-option argument %s\n", argv[index]); | |
41cfadd6 | 72 | |
b64cd08a UD |
73 | result |= optind + 1 != argc || strcmp (argv[optind], "random") != 0; |
74 | ||
75 | return result; | |
28f540f4 | 76 | } |