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