parse-opt: have parse_options_{start,end}.
[git/dscho.git] / parse-options.c
blob9964877edfc1d7b4e5344e04c152f1274699fe64
1 #include "git-compat-util.h"
2 #include "parse-options.h"
4 #define OPT_SHORT 1
5 #define OPT_UNSET 2
7 static inline const char *get_arg(struct parse_opt_ctx_t *p)
9 if (p->opt) {
10 const char *res = p->opt;
11 p->opt = NULL;
12 return res;
14 p->argc--;
15 return *++p->argv;
18 static inline const char *skip_prefix(const char *str, const char *prefix)
20 size_t len = strlen(prefix);
21 return strncmp(str, prefix, len) ? NULL : str + len;
24 static int opterror(const struct option *opt, const char *reason, int flags)
26 if (flags & OPT_SHORT)
27 return error("switch `%c' %s", opt->short_name, reason);
28 if (flags & OPT_UNSET)
29 return error("option `no-%s' %s", opt->long_name, reason);
30 return error("option `%s' %s", opt->long_name, reason);
33 static int get_value(struct parse_opt_ctx_t *p,
34 const struct option *opt, int flags)
36 const char *s, *arg;
37 const int unset = flags & OPT_UNSET;
39 if (unset && p->opt)
40 return opterror(opt, "takes no value", flags);
41 if (unset && (opt->flags & PARSE_OPT_NONEG))
42 return opterror(opt, "isn't available", flags);
44 if (!(flags & OPT_SHORT) && p->opt) {
45 switch (opt->type) {
46 case OPTION_CALLBACK:
47 if (!(opt->flags & PARSE_OPT_NOARG))
48 break;
49 /* FALLTHROUGH */
50 case OPTION_BOOLEAN:
51 case OPTION_BIT:
52 case OPTION_SET_INT:
53 case OPTION_SET_PTR:
54 return opterror(opt, "takes no value", flags);
55 default:
56 break;
60 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
61 switch (opt->type) {
62 case OPTION_BIT:
63 if (unset)
64 *(int *)opt->value &= ~opt->defval;
65 else
66 *(int *)opt->value |= opt->defval;
67 return 0;
69 case OPTION_BOOLEAN:
70 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
71 return 0;
73 case OPTION_SET_INT:
74 *(int *)opt->value = unset ? 0 : opt->defval;
75 return 0;
77 case OPTION_SET_PTR:
78 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
79 return 0;
81 case OPTION_STRING:
82 if (unset) {
83 *(const char **)opt->value = NULL;
84 return 0;
86 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
87 *(const char **)opt->value = (const char *)opt->defval;
88 return 0;
90 if (!arg)
91 return opterror(opt, "requires a value", flags);
92 *(const char **)opt->value = get_arg(p);
93 return 0;
95 case OPTION_CALLBACK:
96 if (unset)
97 return (*opt->callback)(opt, NULL, 1);
98 if (opt->flags & PARSE_OPT_NOARG)
99 return (*opt->callback)(opt, NULL, 0);
100 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
101 return (*opt->callback)(opt, NULL, 0);
102 if (!arg)
103 return opterror(opt, "requires a value", flags);
104 return (*opt->callback)(opt, get_arg(p), 0);
106 case OPTION_INTEGER:
107 if (unset) {
108 *(int *)opt->value = 0;
109 return 0;
111 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
112 *(int *)opt->value = opt->defval;
113 return 0;
115 if (!arg)
116 return opterror(opt, "requires a value", flags);
117 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
118 if (*s)
119 return opterror(opt, "expects a numerical value", flags);
120 return 0;
122 default:
123 die("should not happen, someone must be hit on the forehead");
127 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
129 for (; options->type != OPTION_END; options++) {
130 if (options->short_name == *p->opt) {
131 p->opt = p->opt[1] ? p->opt + 1 : NULL;
132 return get_value(p, options, OPT_SHORT);
135 return error("unknown switch `%c'", *p->opt);
138 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
139 const struct option *options)
141 const char *arg_end = strchr(arg, '=');
142 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
143 int abbrev_flags = 0, ambiguous_flags = 0;
145 if (!arg_end)
146 arg_end = arg + strlen(arg);
148 for (; options->type != OPTION_END; options++) {
149 const char *rest;
150 int flags = 0;
152 if (!options->long_name)
153 continue;
155 rest = skip_prefix(arg, options->long_name);
156 if (options->type == OPTION_ARGUMENT) {
157 if (!rest)
158 continue;
159 if (*rest == '=')
160 return opterror(options, "takes no value", flags);
161 if (*rest)
162 continue;
163 p->out[p->cpidx++] = arg - 2;
164 return 0;
166 if (!rest) {
167 /* abbreviated? */
168 if (!strncmp(options->long_name, arg, arg_end - arg)) {
169 is_abbreviated:
170 if (abbrev_option) {
172 * If this is abbreviated, it is
173 * ambiguous. So when there is no
174 * exact match later, we need to
175 * error out.
177 ambiguous_option = abbrev_option;
178 ambiguous_flags = abbrev_flags;
180 if (!(flags & OPT_UNSET) && *arg_end)
181 p->opt = arg_end + 1;
182 abbrev_option = options;
183 abbrev_flags = flags;
184 continue;
186 /* negated and abbreviated very much? */
187 if (!prefixcmp("no-", arg)) {
188 flags |= OPT_UNSET;
189 goto is_abbreviated;
191 /* negated? */
192 if (strncmp(arg, "no-", 3))
193 continue;
194 flags |= OPT_UNSET;
195 rest = skip_prefix(arg + 3, options->long_name);
196 /* abbreviated and negated? */
197 if (!rest && !prefixcmp(options->long_name, arg + 3))
198 goto is_abbreviated;
199 if (!rest)
200 continue;
202 if (*rest) {
203 if (*rest != '=')
204 continue;
205 p->opt = rest + 1;
207 return get_value(p, options, flags);
210 if (ambiguous_option)
211 return error("Ambiguous option: %s "
212 "(could be --%s%s or --%s%s)",
213 arg,
214 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
215 ambiguous_option->long_name,
216 (abbrev_flags & OPT_UNSET) ? "no-" : "",
217 abbrev_option->long_name);
218 if (abbrev_option)
219 return get_value(p, abbrev_option, abbrev_flags);
220 return error("unknown option `%s'", arg);
223 void check_typos(const char *arg, const struct option *options)
225 if (strlen(arg) < 3)
226 return;
228 if (!prefixcmp(arg, "no-")) {
229 error ("did you mean `--%s` (with two dashes ?)", arg);
230 exit(129);
233 for (; options->type != OPTION_END; options++) {
234 if (!options->long_name)
235 continue;
236 if (!prefixcmp(options->long_name, arg)) {
237 error ("did you mean `--%s` (with two dashes ?)", arg);
238 exit(129);
243 void parse_options_start(struct parse_opt_ctx_t *ctx,
244 int argc, const char **argv, int flags)
246 memset(ctx, 0, sizeof(*ctx));
247 ctx->argc = argc - 1;
248 ctx->argv = argv + 1;
249 ctx->out = argv;
250 ctx->flags = flags;
253 int parse_options_end(struct parse_opt_ctx_t *ctx)
255 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
256 ctx->out[ctx->cpidx + ctx->argc] = NULL;
257 return ctx->cpidx + ctx->argc;
260 static NORETURN void usage_with_options_internal(const char * const *,
261 const struct option *, int);
263 int parse_options(int argc, const char **argv, const struct option *options,
264 const char * const usagestr[], int flags)
266 struct parse_opt_ctx_t ctx;
268 parse_options_start(&ctx, argc, argv, flags);
269 for (; ctx.argc; ctx.argc--, ctx.argv++) {
270 const char *arg = ctx.argv[0];
272 if (*arg != '-' || !arg[1]) {
273 if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
274 break;
275 ctx.out[ctx.cpidx++] = ctx.argv[0];
276 continue;
279 if (arg[1] != '-') {
280 ctx.opt = arg + 1;
281 if (*ctx.opt == 'h')
282 usage_with_options(usagestr, options);
283 if (parse_short_opt(&ctx, options) < 0)
284 usage_with_options(usagestr, options);
285 if (ctx.opt)
286 check_typos(arg + 1, options);
287 while (ctx.opt) {
288 if (*ctx.opt == 'h')
289 usage_with_options(usagestr, options);
290 if (parse_short_opt(&ctx, options) < 0)
291 usage_with_options(usagestr, options);
293 continue;
296 if (!arg[2]) { /* "--" */
297 if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
298 ctx.argc--;
299 ctx.argv++;
301 break;
304 if (!strcmp(arg + 2, "help-all"))
305 usage_with_options_internal(usagestr, options, 1);
306 if (!strcmp(arg + 2, "help"))
307 usage_with_options(usagestr, options);
308 if (parse_long_opt(&ctx, arg + 2, options))
309 usage_with_options(usagestr, options);
312 return parse_options_end(&ctx);
315 #define USAGE_OPTS_WIDTH 24
316 #define USAGE_GAP 2
318 void usage_with_options_internal(const char * const *usagestr,
319 const struct option *opts, int full)
321 fprintf(stderr, "usage: %s\n", *usagestr++);
322 while (*usagestr && **usagestr)
323 fprintf(stderr, " or: %s\n", *usagestr++);
324 while (*usagestr) {
325 fprintf(stderr, "%s%s\n",
326 **usagestr ? " " : "",
327 *usagestr);
328 usagestr++;
331 if (opts->type != OPTION_GROUP)
332 fputc('\n', stderr);
334 for (; opts->type != OPTION_END; opts++) {
335 size_t pos;
336 int pad;
338 if (opts->type == OPTION_GROUP) {
339 fputc('\n', stderr);
340 if (*opts->help)
341 fprintf(stderr, "%s\n", opts->help);
342 continue;
344 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
345 continue;
347 pos = fprintf(stderr, " ");
348 if (opts->short_name)
349 pos += fprintf(stderr, "-%c", opts->short_name);
350 if (opts->long_name && opts->short_name)
351 pos += fprintf(stderr, ", ");
352 if (opts->long_name)
353 pos += fprintf(stderr, "--%s", opts->long_name);
355 switch (opts->type) {
356 case OPTION_ARGUMENT:
357 break;
358 case OPTION_INTEGER:
359 if (opts->flags & PARSE_OPT_OPTARG)
360 if (opts->long_name)
361 pos += fprintf(stderr, "[=<n>]");
362 else
363 pos += fprintf(stderr, "[<n>]");
364 else
365 pos += fprintf(stderr, " <n>");
366 break;
367 case OPTION_CALLBACK:
368 if (opts->flags & PARSE_OPT_NOARG)
369 break;
370 /* FALLTHROUGH */
371 case OPTION_STRING:
372 if (opts->argh) {
373 if (opts->flags & PARSE_OPT_OPTARG)
374 if (opts->long_name)
375 pos += fprintf(stderr, "[=<%s>]", opts->argh);
376 else
377 pos += fprintf(stderr, "[<%s>]", opts->argh);
378 else
379 pos += fprintf(stderr, " <%s>", opts->argh);
380 } else {
381 if (opts->flags & PARSE_OPT_OPTARG)
382 if (opts->long_name)
383 pos += fprintf(stderr, "[=...]");
384 else
385 pos += fprintf(stderr, "[...]");
386 else
387 pos += fprintf(stderr, " ...");
389 break;
390 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
391 break;
394 if (pos <= USAGE_OPTS_WIDTH)
395 pad = USAGE_OPTS_WIDTH - pos;
396 else {
397 fputc('\n', stderr);
398 pad = USAGE_OPTS_WIDTH;
400 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
402 fputc('\n', stderr);
404 exit(129);
407 void usage_with_options(const char * const *usagestr,
408 const struct option *opts)
410 usage_with_options_internal(usagestr, opts, 0);
413 /*----- some often used options -----*/
414 #include "cache.h"
416 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
418 int v;
420 if (!arg) {
421 v = unset ? 0 : DEFAULT_ABBREV;
422 } else {
423 v = strtol(arg, (char **)&arg, 10);
424 if (*arg)
425 return opterror(opt, "expects a numerical value", 0);
426 if (v && v < MINIMUM_ABBREV)
427 v = MINIMUM_ABBREV;
428 else if (v > 40)
429 v = 40;
431 *(int *)(opt->value) = v;
432 return 0;
435 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
436 int unset)
438 *(unsigned long *)(opt->value) = approxidate(arg);
439 return 0;