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
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 HAVE_CONFIG_H
22 1.1 christos # include <config.h>
23 1.1 christos #endif
24 1.1 christos
25 1.1 christos #ifdef _LIBC
26 1.1 christos # include <getopt.h>
27 1.1 christos #else
28 1.1 christos # include "getopt.h"
29 1.1 christos #endif
30 1.1 christos #include "getopt_int.h"
31 1.1 christos
32 1.1 christos #include <stdio.h>
33 1.1 christos
34 1.1 christos /* This needs to come after some library #include
35 1.1 christos to get __GNU_LIBRARY__ defined. */
36 1.1 christos #ifdef __GNU_LIBRARY__
37 1.1 christos #include <stdlib.h>
38 1.1 christos #endif
39 1.1 christos
40 1.1 christos #ifndef NULL
41 1.1 christos #define NULL 0
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos int
45 1.1 christos getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
46 1.1 christos const struct option *long_options, int *opt_index)
47 1.1 christos {
48 1.1 christos return _getopt_internal (argc, (char **) argv, options, long_options,
49 1.1 christos opt_index, 0, 0);
50 1.1 christos }
51 1.1 christos
52 1.1 christos int
53 1.1 christos _getopt_long_r (int argc, char **argv, const char *options,
54 1.1 christos const struct option *long_options, int *opt_index,
55 1.1 christos struct _getopt_data *d)
56 1.1 christos {
57 1.1 christos return _getopt_internal_r (argc, argv, options, long_options, opt_index,
58 1.1 christos 0, 0, d);
59 1.1 christos }
60 1.1 christos
61 1.1 christos /* Like getopt_long, but '-' as well as '--' can indicate a long option.
62 1.1 christos If an option that starts with '-' (not '--') doesn't match a long option,
63 1.1 christos but does match a short option, it is parsed as a short option
64 1.1 christos instead. */
65 1.1 christos
66 1.1 christos int
67 1.1 christos getopt_long_only (int argc, char *__getopt_argv_const *argv,
68 1.1 christos const char *options,
69 1.1 christos const struct option *long_options, int *opt_index)
70 1.1 christos {
71 1.1 christos return _getopt_internal (argc, (char **) argv, options, long_options,
72 1.1 christos opt_index, 1, 0);
73 1.1 christos }
74 1.1 christos
75 1.1 christos int
76 1.1 christos _getopt_long_only_r (int argc, char **argv, const char *options,
77 1.1 christos const struct option *long_options, int *opt_index,
78 1.1 christos struct _getopt_data *d)
79 1.1 christos {
80 1.1 christos return _getopt_internal_r (argc, argv, options, long_options, opt_index,
81 1.1 christos 1, 0, d);
82 1.1 christos }
83 1.1 christos
84 1.1 christos
85 1.1 christos #ifdef TEST
87 1.1 christos
88 1.1 christos #include <stdio.h>
89 1.1 christos
90 1.1 christos int
91 1.1 christos main (int argc, char **argv)
92 1.1 christos {
93 1.1 christos int c;
94 1.1 christos int digit_optind = 0;
95 1.1 christos
96 1.1 christos while (1)
97 1.1 christos {
98 1.1 christos int this_option_optind = optind ? optind : 1;
99 1.1 christos int option_index = 0;
100 1.1 christos static struct option long_options[] =
101 1.1 christos {
102 1.1 christos {"add", 1, 0, 0},
103 1.1 christos {"append", 0, 0, 0},
104 1.1 christos {"delete", 1, 0, 0},
105 1.1 christos {"verbose", 0, 0, 0},
106 1.1 christos {"create", 0, 0, 0},
107 1.1 christos {"file", 1, 0, 0},
108 1.1 christos {0, 0, 0, 0}
109 1.1 christos };
110 1.1 christos
111 1.1 christos c = getopt_long (argc, argv, "abc:d:0123456789",
112 1.1 christos long_options, &option_index);
113 1.1 christos if (c == -1)
114 1.1 christos break;
115 1.1 christos
116 1.1 christos switch (c)
117 1.1 christos {
118 1.1 christos case 0:
119 1.1 christos printf ("option %s", long_options[option_index].name);
120 1.1 christos if (optarg)
121 1.1 christos printf (" with arg %s", optarg);
122 1.1 christos printf ("\n");
123 1.1 christos break;
124 1.1 christos
125 1.1 christos case '0':
126 1.1 christos case '1':
127 1.1 christos case '2':
128 1.1 christos case '3':
129 1.1 christos case '4':
130 1.1 christos case '5':
131 1.1 christos case '6':
132 1.1 christos case '7':
133 1.1 christos case '8':
134 1.1 christos case '9':
135 1.1 christos if (digit_optind != 0 && digit_optind != this_option_optind)
136 1.1 christos printf ("digits occur in two different argv-elements.\n");
137 1.1 christos digit_optind = this_option_optind;
138 1.1 christos printf ("option %c\n", c);
139 1.1 christos break;
140 1.1 christos
141 1.1 christos case 'a':
142 1.1 christos printf ("option a\n");
143 1.1 christos break;
144 1.1 christos
145 1.1 christos case 'b':
146 1.1 christos printf ("option b\n");
147 1.1 christos break;
148 1.1 christos
149 1.1 christos case 'c':
150 1.1 christos printf ("option c with value `%s'\n", optarg);
151 1.1 christos break;
152 1.1 christos
153 1.1 christos case 'd':
154 1.1 christos printf ("option d with value `%s'\n", optarg);
155 1.1 christos break;
156 1.1 christos
157 1.1 christos case '?':
158 1.1 christos break;
159 1.1 christos
160 1.1 christos default:
161 1.1 christos printf ("?? getopt returned character code 0%o ??\n", c);
162 1.1 christos }
163 1.1 christos }
164 1.1 christos
165 1.1 christos if (optind < argc)
166 1.1 christos {
167 1.1 christos printf ("non-option ARGV-elements: ");
168 1.1 christos while (optind < argc)
169 1.1 christos printf ("%s ", argv[optind++]);
170 1.1 christos printf ("\n");
171 1.1 christos }
172 1.1 christos
173 1.1 christos exit (0);
174 1.1 christos }
175
176 #endif /* TEST */
177