git-commit-tree: make it usable from other builtins
[git/gtp.git] / parse-options.c
blobbbc3ca4a9ffc0c3be829b074cadb2b2a82e43f02
1 #include "git-compat-util.h"
2 #include "parse-options.h"
4 #define OPT_SHORT 1
5 #define OPT_UNSET 2
7 struct optparse_t {
8 const char **argv;
9 const char **out;
10 int argc, cpidx;
11 const char *opt;
14 static inline const char *get_arg(struct optparse_t *p)
16 if (p->opt) {
17 const char *res = p->opt;
18 p->opt = NULL;
19 return res;
21 p->argc--;
22 return *++p->argv;
25 static int opterror(const struct option *opt, const char *reason, int flags)
27 if (flags & OPT_SHORT)
28 return error("switch `%c' %s", opt->short_name, reason);
29 if (flags & OPT_UNSET)
30 return error("option `no-%s' %s", opt->long_name, reason);
31 return error("option `%s' %s", opt->long_name, reason);
34 static int get_value(struct optparse_t *p,
35 const struct option *opt, int flags)
37 const char *s, *arg;
38 const int unset = flags & OPT_UNSET;
40 if (unset && p->opt)
41 return opterror(opt, "takes no value", flags);
42 if (unset && (opt->flags & PARSE_OPT_NONEG))
43 return opterror(opt, "isn't available", flags);
45 if (!(flags & OPT_SHORT) && p->opt) {
46 switch (opt->type) {
47 case OPTION_CALLBACK:
48 if (!(opt->flags & PARSE_OPT_NOARG))
49 break;
50 /* FALLTHROUGH */
51 case OPTION_BOOLEAN:
52 case OPTION_BIT:
53 case OPTION_SET_INT:
54 case OPTION_SET_PTR:
55 return opterror(opt, "takes no value", flags);
56 default:
57 break;
61 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
62 switch (opt->type) {
63 case OPTION_BIT:
64 if (unset)
65 *(int *)opt->value &= ~opt->defval;
66 else
67 *(int *)opt->value |= opt->defval;
68 return 0;
70 case OPTION_BOOLEAN:
71 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
72 return 0;
74 case OPTION_SET_INT:
75 *(int *)opt->value = unset ? 0 : opt->defval;
76 return 0;
78 case OPTION_SET_PTR:
79 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
80 return 0;
82 case OPTION_STRING:
83 if (unset) {
84 *(const char **)opt->value = NULL;
85 return 0;
87 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
88 *(const char **)opt->value = (const char *)opt->defval;
89 return 0;
91 if (!arg)
92 return opterror(opt, "requires a value", flags);
93 *(const char **)opt->value = get_arg(p);
94 return 0;
96 case OPTION_CALLBACK:
97 if (unset)
98 return (*opt->callback)(opt, NULL, 1);
99 if (opt->flags & PARSE_OPT_NOARG)
100 return (*opt->callback)(opt, NULL, 0);
101 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
102 return (*opt->callback)(opt, NULL, 0);
103 if (!arg)
104 return opterror(opt, "requires a value", flags);
105 return (*opt->callback)(opt, get_arg(p), 0);
107 case OPTION_INTEGER:
108 if (unset) {
109 *(int *)opt->value = 0;
110 return 0;
112 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
113 *(int *)opt->value = opt->defval;
114 return 0;
116 if (!arg)
117 return opterror(opt, "requires a value", flags);
118 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
119 if (*s)
120 return opterror(opt, "expects a numerical value", flags);
121 return 0;
123 default:
124 die("should not happen, someone must be hit on the forehead");
128 static int parse_short_opt(struct optparse_t *p, const struct option *options)
130 for (; options->type != OPTION_END; options++) {
131 if (options->short_name == *p->opt) {
132 p->opt = p->opt[1] ? p->opt + 1 : NULL;
133 return get_value(p, options, OPT_SHORT);
136 return error("unknown switch `%c'", *p->opt);
139 static int parse_long_opt(struct optparse_t *p, const char *arg,
140 const struct option *options)
142 const char *arg_end = strchr(arg, '=');
143 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
144 int abbrev_flags = 0, ambiguous_flags = 0;
146 if (!arg_end)
147 arg_end = arg + strlen(arg);
149 for (; options->type != OPTION_END; options++) {
150 const char *rest;
151 int flags = 0;
153 if (!options->long_name)
154 continue;
156 rest = skip_prefix(arg, options->long_name);
157 if (options->type == OPTION_ARGUMENT) {
158 if (!rest)
159 continue;
160 if (*rest == '=')
161 return opterror(options, "takes no value", flags);
162 if (*rest)
163 continue;
164 p->out[p->cpidx++] = arg - 2;
165 return 0;
167 if (!rest) {
168 /* abbreviated? */
169 if (!strncmp(options->long_name, arg, arg_end - arg)) {
170 is_abbreviated:
171 if (abbrev_option) {
173 * If this is abbreviated, it is
174 * ambiguous. So when there is no
175 * exact match later, we need to
176 * error out.
178 ambiguous_option = abbrev_option;
179 ambiguous_flags = abbrev_flags;
181 if (!(flags & OPT_UNSET) && *arg_end)
182 p->opt = arg_end + 1;
183 abbrev_option = options;
184 abbrev_flags = flags;
185 continue;
187 /* negated and abbreviated very much? */
188 if (!prefixcmp("no-", arg)) {
189 flags |= OPT_UNSET;
190 goto is_abbreviated;
192 /* negated? */
193 if (strncmp(arg, "no-", 3))
194 continue;
195 flags |= OPT_UNSET;
196 rest = skip_prefix(arg + 3, options->long_name);
197 /* abbreviated and negated? */
198 if (!rest && !prefixcmp(options->long_name, arg + 3))
199 goto is_abbreviated;
200 if (!rest)
201 continue;
203 if (*rest) {
204 if (*rest != '=')
205 continue;
206 p->opt = rest + 1;
208 return get_value(p, options, flags);
211 if (ambiguous_option)
212 return error("Ambiguous option: %s "
213 "(could be --%s%s or --%s%s)",
214 arg,
215 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
216 ambiguous_option->long_name,
217 (abbrev_flags & OPT_UNSET) ? "no-" : "",
218 abbrev_option->long_name);
219 if (abbrev_option)
220 return get_value(p, abbrev_option, abbrev_flags);
221 return error("unknown option `%s'", arg);
224 void check_typos(const char *arg, const struct option *options)
226 if (strlen(arg) < 3)
227 return;
229 if (!prefixcmp(arg, "no-")) {
230 error ("did you mean `--%s` (with two dashes ?)", arg);
231 exit(129);
234 for (; options->type != OPTION_END; options++) {
235 if (!options->long_name)
236 continue;
237 if (!prefixcmp(options->long_name, arg)) {
238 error ("did you mean `--%s` (with two dashes ?)", arg);
239 exit(129);
244 static NORETURN void usage_with_options_internal(const char * const *,
245 const struct option *, int);
247 int parse_options(int argc, const char **argv, const struct option *options,
248 const char * const usagestr[], int flags)
250 struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
252 for (; args.argc; args.argc--, args.argv++) {
253 const char *arg = args.argv[0];
255 if (*arg != '-' || !arg[1]) {
256 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
257 break;
258 args.out[args.cpidx++] = args.argv[0];
259 continue;
262 if (arg[1] != '-') {
263 args.opt = arg + 1;
264 if (*args.opt == 'h')
265 usage_with_options(usagestr, options);
266 if (parse_short_opt(&args, options) < 0)
267 usage_with_options(usagestr, options);
268 if (args.opt)
269 check_typos(arg + 1, options);
270 while (args.opt) {
271 if (*args.opt == 'h')
272 usage_with_options(usagestr, options);
273 if (parse_short_opt(&args, options) < 0)
274 usage_with_options(usagestr, options);
276 continue;
279 if (!arg[2]) { /* "--" */
280 if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
281 args.argc--;
282 args.argv++;
284 break;
287 if (!strcmp(arg + 2, "help-all"))
288 usage_with_options_internal(usagestr, options, 1);
289 if (!strcmp(arg + 2, "help"))
290 usage_with_options(usagestr, options);
291 if (parse_long_opt(&args, arg + 2, options))
292 usage_with_options(usagestr, options);
295 memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
296 args.out[args.cpidx + args.argc] = NULL;
297 return args.cpidx + args.argc;
300 #define USAGE_OPTS_WIDTH 24
301 #define USAGE_GAP 2
303 void usage_with_options_internal(const char * const *usagestr,
304 const struct option *opts, int full)
306 fprintf(stderr, "usage: %s\n", *usagestr++);
307 while (*usagestr && **usagestr)
308 fprintf(stderr, " or: %s\n", *usagestr++);
309 while (*usagestr) {
310 fprintf(stderr, "%s%s\n",
311 **usagestr ? " " : "",
312 *usagestr);
313 usagestr++;
316 if (opts->type != OPTION_GROUP)
317 fputc('\n', stderr);
319 for (; opts->type != OPTION_END; opts++) {
320 size_t pos;
321 int pad;
323 if (opts->type == OPTION_GROUP) {
324 fputc('\n', stderr);
325 if (*opts->help)
326 fprintf(stderr, "%s\n", opts->help);
327 continue;
329 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
330 continue;
332 pos = fprintf(stderr, " ");
333 if (opts->short_name)
334 pos += fprintf(stderr, "-%c", opts->short_name);
335 if (opts->long_name && opts->short_name)
336 pos += fprintf(stderr, ", ");
337 if (opts->long_name)
338 pos += fprintf(stderr, "--%s", opts->long_name);
340 switch (opts->type) {
341 case OPTION_ARGUMENT:
342 break;
343 case OPTION_INTEGER:
344 if (opts->flags & PARSE_OPT_OPTARG)
345 if (opts->long_name)
346 pos += fprintf(stderr, "[=<n>]");
347 else
348 pos += fprintf(stderr, "[<n>]");
349 else
350 pos += fprintf(stderr, " <n>");
351 break;
352 case OPTION_CALLBACK:
353 if (opts->flags & PARSE_OPT_NOARG)
354 break;
355 /* FALLTHROUGH */
356 case OPTION_STRING:
357 if (opts->argh) {
358 if (opts->flags & PARSE_OPT_OPTARG)
359 if (opts->long_name)
360 pos += fprintf(stderr, "[=<%s>]", opts->argh);
361 else
362 pos += fprintf(stderr, "[<%s>]", opts->argh);
363 else
364 pos += fprintf(stderr, " <%s>", opts->argh);
365 } else {
366 if (opts->flags & PARSE_OPT_OPTARG)
367 if (opts->long_name)
368 pos += fprintf(stderr, "[=...]");
369 else
370 pos += fprintf(stderr, "[...]");
371 else
372 pos += fprintf(stderr, " ...");
374 break;
375 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
376 break;
379 if (pos <= USAGE_OPTS_WIDTH)
380 pad = USAGE_OPTS_WIDTH - pos;
381 else {
382 fputc('\n', stderr);
383 pad = USAGE_OPTS_WIDTH;
385 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
387 fputc('\n', stderr);
389 exit(129);
392 void usage_with_options(const char * const *usagestr,
393 const struct option *opts)
395 usage_with_options_internal(usagestr, opts, 0);
398 /*----- some often used options -----*/
399 #include "cache.h"
401 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
403 int v;
405 if (!arg) {
406 v = unset ? 0 : DEFAULT_ABBREV;
407 } else {
408 v = strtol(arg, (char **)&arg, 10);
409 if (*arg)
410 return opterror(opt, "expects a numerical value", 0);
411 if (v && v < MINIMUM_ABBREV)
412 v = MINIMUM_ABBREV;
413 else if (v > 40)
414 v = 40;
416 *(int *)(opt->value) = v;
417 return 0;
420 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
421 int unset)
423 *(unsigned long *)(opt->value) = approxidate(arg);
424 return 0;