Code

parseopt: add OPT_NEGBIT
[git.git] / parse-options.c
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
6 #define OPT_SHORT 1
7 #define OPT_UNSET 2
9 static int opterror(const struct option *opt, const char *reason, int flags)
10 {
11         if (flags & OPT_SHORT)
12                 return error("switch `%c' %s", opt->short_name, reason);
13         if (flags & OPT_UNSET)
14                 return error("option `no-%s' %s", opt->long_name, reason);
15         return error("option `%s' %s", opt->long_name, reason);
16 }
18 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19                    int flags, const char **arg)
20 {
21         if (p->opt) {
22                 *arg = p->opt;
23                 p->opt = NULL;
24         } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
25                 *arg = (const char *)opt->defval;
26         } else if (p->argc > 1) {
27                 p->argc--;
28                 *arg = *++p->argv;
29         } else
30                 return opterror(opt, "requires a value", flags);
31         return 0;
32 }
34 static int get_value(struct parse_opt_ctx_t *p,
35                      const struct option *opt, int flags)
36 {
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_NEGBIT:
54                 case OPTION_SET_INT:
55                 case OPTION_SET_PTR:
56                         return opterror(opt, "takes no value", flags);
57                 default:
58                         break;
59                 }
60         }
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_NEGBIT:
71                 if (unset)
72                         *(int *)opt->value |= opt->defval;
73                 else
74                         *(int *)opt->value &= ~opt->defval;
75                 return 0;
77         case OPTION_BOOLEAN:
78                 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
79                 return 0;
81         case OPTION_SET_INT:
82                 *(int *)opt->value = unset ? 0 : opt->defval;
83                 return 0;
85         case OPTION_SET_PTR:
86                 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
87                 return 0;
89         case OPTION_STRING:
90                 if (unset)
91                         *(const char **)opt->value = NULL;
92                 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
93                         *(const char **)opt->value = (const char *)opt->defval;
94                 else
95                         return get_arg(p, opt, flags, (const char **)opt->value);
96                 return 0;
98         case OPTION_CALLBACK:
99                 if (unset)
100                         return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
101                 if (opt->flags & PARSE_OPT_NOARG)
102                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
103                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
104                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
105                 if (get_arg(p, opt, flags, &arg))
106                         return -1;
107                 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
109         case OPTION_INTEGER:
110                 if (unset) {
111                         *(int *)opt->value = 0;
112                         return 0;
113                 }
114                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
115                         *(int *)opt->value = opt->defval;
116                         return 0;
117                 }
118                 if (get_arg(p, opt, flags, &arg))
119                         return -1;
120                 *(int *)opt->value = strtol(arg, (char **)&s, 10);
121                 if (*s)
122                         return opterror(opt, "expects a numerical value", flags);
123                 return 0;
125         default:
126                 die("should not happen, someone must be hit on the forehead");
127         }
130 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
132         for (; options->type != OPTION_END; options++) {
133                 if (options->short_name == *p->opt) {
134                         p->opt = p->opt[1] ? p->opt + 1 : NULL;
135                         return get_value(p, options, OPT_SHORT);
136                 }
137         }
138         return -2;
141 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
142                           const struct option *options)
144         const char *arg_end = strchr(arg, '=');
145         const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
146         int abbrev_flags = 0, ambiguous_flags = 0;
148         if (!arg_end)
149                 arg_end = arg + strlen(arg);
151         for (; options->type != OPTION_END; options++) {
152                 const char *rest;
153                 int flags = 0;
155                 if (!options->long_name)
156                         continue;
158                 rest = skip_prefix(arg, options->long_name);
159                 if (options->type == OPTION_ARGUMENT) {
160                         if (!rest)
161                                 continue;
162                         if (*rest == '=')
163                                 return opterror(options, "takes no value", flags);
164                         if (*rest)
165                                 continue;
166                         p->out[p->cpidx++] = arg - 2;
167                         return 0;
168                 }
169                 if (!rest) {
170                         /* abbreviated? */
171                         if (!strncmp(options->long_name, arg, arg_end - arg)) {
172 is_abbreviated:
173                                 if (abbrev_option) {
174                                         /*
175                                          * If this is abbreviated, it is
176                                          * ambiguous. So when there is no
177                                          * exact match later, we need to
178                                          * error out.
179                                          */
180                                         ambiguous_option = abbrev_option;
181                                         ambiguous_flags = abbrev_flags;
182                                 }
183                                 if (!(flags & OPT_UNSET) && *arg_end)
184                                         p->opt = arg_end + 1;
185                                 abbrev_option = options;
186                                 abbrev_flags = flags;
187                                 continue;
188                         }
189                         /* negated and abbreviated very much? */
190                         if (!prefixcmp("no-", arg)) {
191                                 flags |= OPT_UNSET;
192                                 goto is_abbreviated;
193                         }
194                         /* negated? */
195                         if (strncmp(arg, "no-", 3))
196                                 continue;
197                         flags |= OPT_UNSET;
198                         rest = skip_prefix(arg + 3, options->long_name);
199                         /* abbreviated and negated? */
200                         if (!rest && !prefixcmp(options->long_name, arg + 3))
201                                 goto is_abbreviated;
202                         if (!rest)
203                                 continue;
204                 }
205                 if (*rest) {
206                         if (*rest != '=')
207                                 continue;
208                         p->opt = rest + 1;
209                 }
210                 return get_value(p, options, flags);
211         }
213         if (ambiguous_option)
214                 return error("Ambiguous option: %s "
215                         "(could be --%s%s or --%s%s)",
216                         arg,
217                         (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
218                         ambiguous_option->long_name,
219                         (abbrev_flags & OPT_UNSET) ?  "no-" : "",
220                         abbrev_option->long_name);
221         if (abbrev_option)
222                 return get_value(p, abbrev_option, abbrev_flags);
223         return -2;
226 static void check_typos(const char *arg, const struct option *options)
228         if (strlen(arg) < 3)
229                 return;
231         if (!prefixcmp(arg, "no-")) {
232                 error ("did you mean `--%s` (with two dashes ?)", arg);
233                 exit(129);
234         }
236         for (; options->type != OPTION_END; options++) {
237                 if (!options->long_name)
238                         continue;
239                 if (!prefixcmp(options->long_name, arg)) {
240                         error ("did you mean `--%s` (with two dashes ?)", arg);
241                         exit(129);
242                 }
243         }
246 void parse_options_start(struct parse_opt_ctx_t *ctx,
247                          int argc, const char **argv, int flags)
249         memset(ctx, 0, sizeof(*ctx));
250         ctx->argc = argc - 1;
251         ctx->argv = argv + 1;
252         ctx->out  = argv;
253         ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
254         ctx->flags = flags;
255         if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
256             (flags & PARSE_OPT_STOP_AT_NON_OPTION))
257                 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
260 static int usage_with_options_internal(const char * const *,
261                                        const struct option *, int);
263 int parse_options_step(struct parse_opt_ctx_t *ctx,
264                        const struct option *options,
265                        const char * const usagestr[])
267         int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
269         /* we must reset ->opt, unknown short option leave it dangling */
270         ctx->opt = NULL;
272         for (; ctx->argc; ctx->argc--, ctx->argv++) {
273                 const char *arg = ctx->argv[0];
275                 if (*arg != '-' || !arg[1]) {
276                         if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
277                                 break;
278                         ctx->out[ctx->cpidx++] = ctx->argv[0];
279                         continue;
280                 }
282                 if (arg[1] != '-') {
283                         ctx->opt = arg + 1;
284                         if (internal_help && *ctx->opt == 'h')
285                                 return parse_options_usage(usagestr, options);
286                         switch (parse_short_opt(ctx, options)) {
287                         case -1:
288                                 return parse_options_usage(usagestr, options);
289                         case -2:
290                                 goto unknown;
291                         }
292                         if (ctx->opt)
293                                 check_typos(arg + 1, options);
294                         while (ctx->opt) {
295                                 if (internal_help && *ctx->opt == 'h')
296                                         return parse_options_usage(usagestr, options);
297                                 switch (parse_short_opt(ctx, options)) {
298                                 case -1:
299                                         return parse_options_usage(usagestr, options);
300                                 case -2:
301                                         /* fake a short option thing to hide the fact that we may have
302                                          * started to parse aggregated stuff
303                                          *
304                                          * This is leaky, too bad.
305                                          */
306                                         ctx->argv[0] = xstrdup(ctx->opt - 1);
307                                         *(char *)ctx->argv[0] = '-';
308                                         goto unknown;
309                                 }
310                         }
311                         continue;
312                 }
314                 if (!arg[2]) { /* "--" */
315                         if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
316                                 ctx->argc--;
317                                 ctx->argv++;
318                         }
319                         break;
320                 }
322                 if (internal_help && !strcmp(arg + 2, "help-all"))
323                         return usage_with_options_internal(usagestr, options, 1);
324                 if (internal_help && !strcmp(arg + 2, "help"))
325                         return parse_options_usage(usagestr, options);
326                 switch (parse_long_opt(ctx, arg + 2, options)) {
327                 case -1:
328                         return parse_options_usage(usagestr, options);
329                 case -2:
330                         goto unknown;
331                 }
332                 continue;
333 unknown:
334                 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
335                         return PARSE_OPT_UNKNOWN;
336                 ctx->out[ctx->cpidx++] = ctx->argv[0];
337                 ctx->opt = NULL;
338         }
339         return PARSE_OPT_DONE;
342 int parse_options_end(struct parse_opt_ctx_t *ctx)
344         memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
345         ctx->out[ctx->cpidx + ctx->argc] = NULL;
346         return ctx->cpidx + ctx->argc;
349 int parse_options(int argc, const char **argv, const struct option *options,
350                   const char * const usagestr[], int flags)
352         struct parse_opt_ctx_t ctx;
354         parse_options_start(&ctx, argc, argv, flags);
355         switch (parse_options_step(&ctx, options, usagestr)) {
356         case PARSE_OPT_HELP:
357                 exit(129);
358         case PARSE_OPT_DONE:
359                 break;
360         default: /* PARSE_OPT_UNKNOWN */
361                 if (ctx.argv[0][1] == '-') {
362                         error("unknown option `%s'", ctx.argv[0] + 2);
363                 } else {
364                         error("unknown switch `%c'", *ctx.opt);
365                 }
366                 usage_with_options(usagestr, options);
367         }
369         return parse_options_end(&ctx);
372 #define USAGE_OPTS_WIDTH 24
373 #define USAGE_GAP         2
375 int usage_with_options_internal(const char * const *usagestr,
376                                 const struct option *opts, int full)
378         if (!usagestr)
379                 return PARSE_OPT_HELP;
381         fprintf(stderr, "usage: %s\n", *usagestr++);
382         while (*usagestr && **usagestr)
383                 fprintf(stderr, "   or: %s\n", *usagestr++);
384         while (*usagestr) {
385                 fprintf(stderr, "%s%s\n",
386                                 **usagestr ? "    " : "",
387                                 *usagestr);
388                 usagestr++;
389         }
391         if (opts->type != OPTION_GROUP)
392                 fputc('\n', stderr);
394         for (; opts->type != OPTION_END; opts++) {
395                 size_t pos;
396                 int pad;
398                 if (opts->type == OPTION_GROUP) {
399                         fputc('\n', stderr);
400                         if (*opts->help)
401                                 fprintf(stderr, "%s\n", opts->help);
402                         continue;
403                 }
404                 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
405                         continue;
407                 pos = fprintf(stderr, "    ");
408                 if (opts->short_name)
409                         pos += fprintf(stderr, "-%c", opts->short_name);
410                 if (opts->long_name && opts->short_name)
411                         pos += fprintf(stderr, ", ");
412                 if (opts->long_name)
413                         pos += fprintf(stderr, "--%s", opts->long_name);
415                 switch (opts->type) {
416                 case OPTION_ARGUMENT:
417                         break;
418                 case OPTION_INTEGER:
419                         if (opts->flags & PARSE_OPT_OPTARG)
420                                 if (opts->long_name)
421                                         pos += fprintf(stderr, "[=<n>]");
422                                 else
423                                         pos += fprintf(stderr, "[<n>]");
424                         else
425                                 pos += fprintf(stderr, " <n>");
426                         break;
427                 case OPTION_CALLBACK:
428                         if (opts->flags & PARSE_OPT_NOARG)
429                                 break;
430                         /* FALLTHROUGH */
431                 case OPTION_STRING:
432                         if (opts->argh) {
433                                 if (opts->flags & PARSE_OPT_OPTARG)
434                                         if (opts->long_name)
435                                                 pos += fprintf(stderr, "[=<%s>]", opts->argh);
436                                         else
437                                                 pos += fprintf(stderr, "[<%s>]", opts->argh);
438                                 else
439                                         pos += fprintf(stderr, " <%s>", opts->argh);
440                         } else {
441                                 if (opts->flags & PARSE_OPT_OPTARG)
442                                         if (opts->long_name)
443                                                 pos += fprintf(stderr, "[=...]");
444                                         else
445                                                 pos += fprintf(stderr, "[...]");
446                                 else
447                                         pos += fprintf(stderr, " ...");
448                         }
449                         break;
450                 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
451                         break;
452                 }
454                 if (pos <= USAGE_OPTS_WIDTH)
455                         pad = USAGE_OPTS_WIDTH - pos;
456                 else {
457                         fputc('\n', stderr);
458                         pad = USAGE_OPTS_WIDTH;
459                 }
460                 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
461         }
462         fputc('\n', stderr);
464         return PARSE_OPT_HELP;
467 void usage_with_options(const char * const *usagestr,
468                         const struct option *opts)
470         usage_with_options_internal(usagestr, opts, 0);
471         exit(129);
474 int parse_options_usage(const char * const *usagestr,
475                         const struct option *opts)
477         return usage_with_options_internal(usagestr, opts, 0);
481 /*----- some often used options -----*/
482 #include "cache.h"
484 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
486         int v;
488         if (!arg) {
489                 v = unset ? 0 : DEFAULT_ABBREV;
490         } else {
491                 v = strtol(arg, (char **)&arg, 10);
492                 if (*arg)
493                         return opterror(opt, "expects a numerical value", 0);
494                 if (v && v < MINIMUM_ABBREV)
495                         v = MINIMUM_ABBREV;
496                 else if (v > 40)
497                         v = 40;
498         }
499         *(int *)(opt->value) = v;
500         return 0;
503 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
504                              int unset)
506         *(unsigned long *)(opt->value) = approxidate(arg);
507         return 0;
510 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
511                            int unset)
513         int *target = opt->value;
515         if (unset)
516                 /* --no-quiet, --no-verbose */
517                 *target = 0;
518         else if (opt->short_name == 'v') {
519                 if (*target >= 0)
520                         (*target)++;
521                 else
522                         *target = 1;
523         } else {
524                 if (*target <= 0)
525                         (*target)--;
526                 else
527                         *target = -1;
528         }
529         return 0;
532 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
534         unsigned char sha1[20];
535         struct commit *commit;
537         if (!arg)
538                 return -1;
539         if (get_sha1(arg, sha1))
540                 return error("malformed object name %s", arg);
541         commit = lookup_commit_reference(sha1);
542         if (!commit)
543                 return error("no such commit %s", arg);
544         commit_list_insert(commit, opt->value);
545         return 0;
548 /*
549  * This should really be OPTION_FILENAME type as a part of
550  * parse_options that take prefix to do this while parsing.
551  */
552 extern const char *parse_options_fix_filename(const char *prefix, const char *file)
554         if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file))
555                 return file;
556         return prefix_filename(prefix, strlen(prefix), file);