]>
Commit | Line | Data |
---|---|---|
d9a17c07 | 1 | /* Example of Parsing Arguments with getopt. |
2b778ceb | 2 | Copyright (C) 1991-2021 Free Software Foundation, Inc. |
d9a17c07 RM |
3 | |
4 | This program is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU General Public License | |
6 | as published by the Free Software Foundation; either version 2 | |
7 | of the License, or (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
5a82c748 | 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. |
d9a17c07 RM |
16 | */ |
17 | ||
28f540f4 | 18 | /*@group*/ |
b7129dfb | 19 | #include <ctype.h> |
28f540f4 | 20 | #include <stdio.h> |
b7129dfb UD |
21 | #include <stdlib.h> |
22 | #include <unistd.h> | |
28f540f4 | 23 | |
b7129dfb | 24 | int |
28f540f4 RM |
25 | main (int argc, char **argv) |
26 | { | |
27 | int aflag = 0; | |
28 | int bflag = 0; | |
29 | char *cvalue = NULL; | |
30 | int index; | |
31 | int c; | |
32 | ||
33 | opterr = 0; | |
34 | /*@end group*/ | |
35 | ||
36 | /*@group*/ | |
37 | while ((c = getopt (argc, argv, "abc:")) != -1) | |
38 | switch (c) | |
39 | { | |
40 | case 'a': | |
41 | aflag = 1; | |
42 | break; | |
43 | case 'b': | |
44 | bflag = 1; | |
45 | break; | |
46 | case 'c': | |
47 | cvalue = optarg; | |
48 | break; | |
49 | case '?': | |
b7129dfb UD |
50 | if (optopt == 'c') |
51 | fprintf (stderr, "Option -%c requires an argument.\n", optopt); | |
52 | else if (isprint (optopt)) | |
28f540f4 RM |
53 | fprintf (stderr, "Unknown option `-%c'.\n", optopt); |
54 | else | |
55 | fprintf (stderr, | |
56 | "Unknown option character `\\x%x'.\n", | |
57 | optopt); | |
58 | return 1; | |
59 | default: | |
60 | abort (); | |
61 | } | |
62 | /*@end group*/ | |
63 | ||
64 | /*@group*/ | |
838e5ffe UD |
65 | printf ("aflag = %d, bflag = %d, cvalue = %s\n", |
66 | aflag, bflag, cvalue); | |
28f540f4 RM |
67 | |
68 | for (index = optind; index < argc; index++) | |
69 | printf ("Non-option argument %s\n", argv[index]); | |
70 | return 0; | |
71 | } | |
72 | /*@end group*/ |