Merge branch 'js/upload-pack'
[git/git.git] / parse-options.c
CommitLineData
4a59fd13
PH
1#include "git-compat-util.h"
2#include "parse-options.h"
4a59fd13
PH
3
4#define OPT_SHORT 1
5#define OPT_UNSET 2
6
7struct optparse_t {
8 const char **argv;
9 int argc;
10 const char *opt;
11};
12
13static inline const char *get_arg(struct optparse_t *p)
14{
15 if (p->opt) {
16 const char *res = p->opt;
17 p->opt = NULL;
18 return res;
19 }
20 p->argc--;
21 return *++p->argv;
22}
23
24static inline const char *skip_prefix(const char *str, const char *prefix)
25{
26 size_t len = strlen(prefix);
27 return strncmp(str, prefix, len) ? NULL : str + len;
28}
29
30static int opterror(const struct option *opt, const char *reason, int flags)
31{
32 if (flags & OPT_SHORT)
33 return error("switch `%c' %s", opt->short_name, reason);
34 if (flags & OPT_UNSET)
35 return error("option `no-%s' %s", opt->long_name, reason);
36 return error("option `%s' %s", opt->long_name, reason);
37}
38
39static int get_value(struct optparse_t *p,
40 const struct option *opt, int flags)
41{
ffe659f9
PH
42 const char *s, *arg;
43 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
4a59fd13
PH
44
45 if (p->opt && (flags & OPT_UNSET))
46 return opterror(opt, "takes no value", flags);
47
48 switch (opt->type) {
49 case OPTION_BOOLEAN:
50 if (!(flags & OPT_SHORT) && p->opt)
51 return opterror(opt, "takes no value", flags);
52 if (flags & OPT_UNSET)
53 *(int *)opt->value = 0;
54 else
55 (*(int *)opt->value)++;
56 return 0;
57
58 case OPTION_STRING:
59 if (flags & OPT_UNSET) {
60 *(const char **)opt->value = (const char *)NULL;
61 return 0;
62 }
ffe659f9
PH
63 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
64 *(const char **)opt->value = (const char *)opt->defval;
65 return 0;
66 }
67 if (!arg)
4a59fd13
PH
68 return opterror(opt, "requires a value", flags);
69 *(const char **)opt->value = get_arg(p);
70 return 0;
71
ffe659f9
PH
72 case OPTION_CALLBACK:
73 if (flags & OPT_UNSET)
74 return (*opt->callback)(opt, NULL, 1);
f481e22a
PH
75 if (opt->flags & PARSE_OPT_NOARG) {
76 if (p->opt && !(flags & OPT_SHORT))
77 return opterror(opt, "takes no value", flags);
78 return (*opt->callback)(opt, NULL, 0);
79 }
ffe659f9
PH
80 if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
81 return (*opt->callback)(opt, NULL, 0);
82 if (!arg)
83 return opterror(opt, "requires a value", flags);
84 return (*opt->callback)(opt, get_arg(p), 0);
85
4a59fd13
PH
86 case OPTION_INTEGER:
87 if (flags & OPT_UNSET) {
88 *(int *)opt->value = 0;
89 return 0;
90 }
ffe659f9
PH
91 if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
92 *(int *)opt->value = opt->defval;
93 return 0;
94 }
95 if (!arg)
4a59fd13
PH
96 return opterror(opt, "requires a value", flags);
97 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
98 if (*s)
99 return opterror(opt, "expects a numerical value", flags);
100 return 0;
101
102 default:
103 die("should not happen, someone must be hit on the forehead");
104 }
105}
106
107static int parse_short_opt(struct optparse_t *p, const struct option *options)
108{
109 for (; options->type != OPTION_END; options++) {
110 if (options->short_name == *p->opt) {
111 p->opt = p->opt[1] ? p->opt + 1 : NULL;
112 return get_value(p, options, OPT_SHORT);
113 }
114 }
115 return error("unknown switch `%c'", *p->opt);
116}
117
118static int parse_long_opt(struct optparse_t *p, const char *arg,
119 const struct option *options)
120{
7f275b91 121 const char *arg_end = strchr(arg, '=');
243e0614
JS
122 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
123 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
124
125 if (!arg_end)
126 arg_end = arg + strlen(arg);
127
4a59fd13
PH
128 for (; options->type != OPTION_END; options++) {
129 const char *rest;
130 int flags = 0;
131
132 if (!options->long_name)
133 continue;
134
135 rest = skip_prefix(arg, options->long_name);
136 if (!rest) {
7f275b91
JS
137 /* abbreviated? */
138 if (!strncmp(options->long_name, arg, arg_end - arg)) {
139is_abbreviated:
243e0614
JS
140 if (abbrev_option) {
141 /*
142 * If this is abbreviated, it is
143 * ambiguous. So when there is no
144 * exact match later, we need to
145 * error out.
146 */
147 ambiguous_option = abbrev_option;
148 ambiguous_flags = abbrev_flags;
149 }
7f275b91
JS
150 if (!(flags & OPT_UNSET) && *arg_end)
151 p->opt = arg_end + 1;
152 abbrev_option = options;
153 abbrev_flags = flags;
154 continue;
155 }
156 /* negated and abbreviated very much? */
157 if (!prefixcmp("no-", arg)) {
158 flags |= OPT_UNSET;
159 goto is_abbreviated;
160 }
161 /* negated? */
4a59fd13
PH
162 if (strncmp(arg, "no-", 3))
163 continue;
164 flags |= OPT_UNSET;
165 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
166 /* abbreviated and negated? */
167 if (!rest && !prefixcmp(options->long_name, arg + 3))
168 goto is_abbreviated;
4a59fd13
PH
169 if (!rest)
170 continue;
171 }
172 if (*rest) {
173 if (*rest != '=')
174 continue;
175 p->opt = rest + 1;
176 }
177 return get_value(p, options, flags);
178 }
243e0614
JS
179
180 if (ambiguous_option)
181 return error("Ambiguous option: %s "
182 "(could be --%s%s or --%s%s)",
183 arg,
184 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
185 ambiguous_option->long_name,
186 (abbrev_flags & OPT_UNSET) ? "no-" : "",
187 abbrev_option->long_name);
7f275b91
JS
188 if (abbrev_option)
189 return get_value(p, abbrev_option, abbrev_flags);
4a59fd13
PH
190 return error("unknown option `%s'", arg);
191}
192
193int parse_options(int argc, const char **argv, const struct option *options,
d7a38c54 194 const char * const usagestr[], int flags)
4a59fd13
PH
195{
196 struct optparse_t args = { argv + 1, argc - 1, NULL };
197 int j = 0;
198
199 for (; args.argc; args.argc--, args.argv++) {
200 const char *arg = args.argv[0];
201
202 if (*arg != '-' || !arg[1]) {
203 argv[j++] = args.argv[0];
204 continue;
205 }
206
207 if (arg[1] != '-') {
208 args.opt = arg + 1;
209 do {
210 if (*args.opt == 'h')
d7a38c54 211 usage_with_options(usagestr, options);
4a59fd13 212 if (parse_short_opt(&args, options) < 0)
d7a38c54 213 usage_with_options(usagestr, options);
4a59fd13
PH
214 } while (args.opt);
215 continue;
216 }
217
218 if (!arg[2]) { /* "--" */
219 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
220 args.argc--;
221 args.argv++;
222 }
223 break;
224 }
225
226 if (!strcmp(arg + 2, "help"))
d7a38c54 227 usage_with_options(usagestr, options);
4a59fd13 228 if (parse_long_opt(&args, arg + 2, options))
d7a38c54 229 usage_with_options(usagestr, options);
4a59fd13
PH
230 }
231
232 memmove(argv + j, args.argv, args.argc * sizeof(*argv));
233 argv[j + args.argc] = NULL;
234 return j + args.argc;
235}
d7a38c54
PH
236
237#define USAGE_OPTS_WIDTH 24
238#define USAGE_GAP 2
239
240void usage_with_options(const char * const *usagestr,
241 const struct option *opts)
242{
f389c808
AR
243 fprintf(stderr, "usage: %s\n", *usagestr++);
244 while (*usagestr && **usagestr)
245 fprintf(stderr, " or: %s\n", *usagestr++);
246 while (*usagestr)
247 fprintf(stderr, " %s\n", *usagestr++);
d7a38c54
PH
248
249 if (opts->type != OPTION_GROUP)
f389c808 250 fputc('\n', stderr);
d7a38c54
PH
251
252 for (; opts->type != OPTION_END; opts++) {
253 size_t pos;
254 int pad;
255
256 if (opts->type == OPTION_GROUP) {
f389c808 257 fputc('\n', stderr);
d7a38c54 258 if (*opts->help)
f389c808 259 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
260 continue;
261 }
262
f389c808 263 pos = fprintf(stderr, " ");
d7a38c54 264 if (opts->short_name)
f389c808 265 pos += fprintf(stderr, "-%c", opts->short_name);
d7a38c54 266 if (opts->long_name && opts->short_name)
f389c808 267 pos += fprintf(stderr, ", ");
d7a38c54 268 if (opts->long_name)
f389c808 269 pos += fprintf(stderr, "--%s", opts->long_name);
d7a38c54
PH
270
271 switch (opts->type) {
272 case OPTION_INTEGER:
ffe659f9
PH
273 if (opts->flags & PARSE_OPT_OPTARG)
274 pos += fprintf(stderr, " [<n>]");
275 else
276 pos += fprintf(stderr, " <n>");
d7a38c54 277 break;
ffe659f9 278 case OPTION_CALLBACK:
f481e22a
PH
279 if (opts->flags & PARSE_OPT_NOARG)
280 break;
281 /* FALLTHROUGH */
282 case OPTION_STRING:
ffe659f9
PH
283 if (opts->argh) {
284 if (opts->flags & PARSE_OPT_OPTARG)
285 pos += fprintf(stderr, " [<%s>]", opts->argh);
286 else
287 pos += fprintf(stderr, " <%s>", opts->argh);
288 } else {
289 if (opts->flags & PARSE_OPT_OPTARG)
290 pos += fprintf(stderr, " [...]");
291 else
292 pos += fprintf(stderr, " ...");
293 }
d7a38c54
PH
294 break;
295 default:
296 break;
297 }
298
f389c808
AR
299 if (pos <= USAGE_OPTS_WIDTH)
300 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 301 else {
f389c808 302 fputc('\n', stderr);
d7a38c54
PH
303 pad = USAGE_OPTS_WIDTH;
304 }
f389c808 305 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 306 }
f389c808
AR
307 fputc('\n', stderr);
308
309 exit(129);
d7a38c54 310}
0ce865b1
PH
311
312/*----- some often used options -----*/
313#include "cache.h"
314int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
315{
316 int v;
317
318 if (!arg) {
319 v = unset ? 0 : DEFAULT_ABBREV;
320 } else {
321 v = strtol(arg, (char **)&arg, 10);
322 if (*arg)
323 return opterror(opt, "expects a numerical value", 0);
324 if (v && v < MINIMUM_ABBREV)
325 v = MINIMUM_ABBREV;
326 else if (v > 40)
327 v = 40;
328 }
329 *(int *)(opt->value) = v;
330 return 0;
331}