getopt1.c revision 1.1 1 1.1 christos /* getopt_long and getopt_long_only entry points for GNU getopt.
2 1.1 christos Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004,2006
3 1.1 christos Free Software Foundation, Inc.
4 1.1 christos This file is part of the GNU C Library.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 2, or (at your option)
9 1.1 christos any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License along
17 1.1 christos with this program; if not, write to the Free Software Foundation,
18 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 1.1 christos
20 1.1 christos #ifdef _LIBC
22 1.1 christos # include <getopt.h>
23 1.1 christos #else
24 1.1 christos # include <config.h>
25 1.1 christos # include "getopt.h"
26 1.1 christos #endif
27 1.1 christos #include "getopt_int.h"
28 1.1 christos
29 1.1 christos #include <stdio.h>
30 1.1 christos
31 1.1 christos /* This needs to come after some library #include
32 1.1 christos to get __GNU_LIBRARY__ defined. */
33 1.1 christos #ifdef __GNU_LIBRARY__
34 1.1 christos #include <stdlib.h>
35 1.1 christos #endif
36 1.1 christos
37 1.1 christos #ifndef NULL
38 1.1 christos #define NULL 0
39 1.1 christos #endif
40 1.1 christos
41 1.1 christos int
42 1.1 christos getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
43 1.1 christos const struct option *long_options, int *opt_index)
44 1.1 christos {
45 1.1 christos return _getopt_internal (argc, (char **) argv, options, long_options,
46 1.1 christos opt_index, 0, 0);
47 1.1 christos }
48 1.1 christos
49 1.1 christos int
50 1.1 christos _getopt_long_r (int argc, char **argv, const char *options,
51 1.1 christos const struct option *long_options, int *opt_index,
52 1.1 christos struct _getopt_data *d)
53 1.1 christos {
54 1.1 christos return _getopt_internal_r (argc, argv, options, long_options, opt_index,
55 1.1 christos 0, 0, d);
56 1.1 christos }
57 1.1 christos
58 1.1 christos /* Like getopt_long, but '-' as well as '--' can indicate a long option.
59 1.1 christos If an option that starts with '-' (not '--') doesn't match a long option,
60 1.1 christos but does match a short option, it is parsed as a short option
61 1.1 christos instead. */
62 1.1 christos
63 1.1 christos int
64 1.1 christos getopt_long_only (int argc, char *__getopt_argv_const *argv,
65 1.1 christos const char *options,
66 1.1 christos const struct option *long_options, int *opt_index)
67 1.1 christos {
68 1.1 christos return _getopt_internal (argc, (char **) argv, options, long_options,
69 1.1 christos opt_index, 1, 0);
70 1.1 christos }
71 1.1 christos
72 1.1 christos int
73 1.1 christos _getopt_long_only_r (int argc, char **argv, const char *options,
74 1.1 christos const struct option *long_options, int *opt_index,
75 1.1 christos struct _getopt_data *d)
76 1.1 christos {
77 1.1 christos return _getopt_internal_r (argc, argv, options, long_options, opt_index,
78 1.1 christos 1, 0, d);
79 1.1 christos }
80 1.1 christos
81 1.1 christos
82 1.1 christos #ifdef TEST
84 1.1 christos
85 1.1 christos #include <stdio.h>
86 1.1 christos
87 1.1 christos int
88 1.1 christos main (int argc, char **argv)
89 1.1 christos {
90 1.1 christos int c;
91 1.1 christos int digit_optind = 0;
92 1.1 christos
93 1.1 christos while (1)
94 1.1 christos {
95 1.1 christos int this_option_optind = optind ? optind : 1;
96 1.1 christos int option_index = 0;
97 1.1 christos static struct option long_options[] =
98 1.1 christos {
99 1.1 christos {"add", 1, 0, 0},
100 1.1 christos {"append", 0, 0, 0},
101 1.1 christos {"delete", 1, 0, 0},
102 1.1 christos {"verbose", 0, 0, 0},
103 1.1 christos {"create", 0, 0, 0},
104 1.1 christos {"file", 1, 0, 0},
105 1.1 christos {0, 0, 0, 0}
106 1.1 christos };
107 1.1 christos
108 1.1 christos c = getopt_long (argc, argv, "abc:d:0123456789",
109 1.1 christos long_options, &option_index);
110 1.1 christos if (c == -1)
111 1.1 christos break;
112 1.1 christos
113 1.1 christos switch (c)
114 1.1 christos {
115 1.1 christos case 0:
116 1.1 christos printf ("option %s", long_options[option_index].name);
117 1.1 christos if (optarg)
118 1.1 christos printf (" with arg %s", optarg);
119 1.1 christos printf ("\n");
120 1.1 christos break;
121 1.1 christos
122 1.1 christos case '0':
123 1.1 christos case '1':
124 1.1 christos case '2':
125 1.1 christos case '3':
126 1.1 christos case '4':
127 1.1 christos case '5':
128 1.1 christos case '6':
129 1.1 christos case '7':
130 1.1 christos case '8':
131 1.1 christos case '9':
132 1.1 christos if (digit_optind != 0 && digit_optind != this_option_optind)
133 1.1 christos printf ("digits occur in two different argv-elements.\n");
134 1.1 christos digit_optind = this_option_optind;
135 1.1 christos printf ("option %c\n", c);
136 1.1 christos break;
137 1.1 christos
138 1.1 christos case 'a':
139 1.1 christos printf ("option a\n");
140 1.1 christos break;
141 1.1 christos
142 1.1 christos case 'b':
143 1.1 christos printf ("option b\n");
144 1.1 christos break;
145 1.1 christos
146 1.1 christos case 'c':
147 1.1 christos printf ("option c with value `%s'\n", optarg);
148 1.1 christos break;
149 1.1 christos
150 1.1 christos case 'd':
151 1.1 christos printf ("option d with value `%s'\n", optarg);
152 1.1 christos break;
153 1.1 christos
154 1.1 christos case '?':
155 1.1 christos break;
156 1.1 christos
157 1.1 christos default:
158 1.1 christos printf ("?? getopt returned character code 0%o ??\n", c);
159 1.1 christos }
160 1.1 christos }
161 1.1 christos
162 1.1 christos if (optind < argc)
163 1.1 christos {
164 1.1 christos printf ("non-option ARGV-elements: ");
165 1.1 christos while (optind < argc)
166 1.1 christos printf ("%s ", argv[optind++]);
167 1.1 christos printf ("\n");
168 1.1 christos }
169 1.1 christos
170 1.1 christos exit (0);
171 1.1 christos }
172
173 #endif /* TEST */
174