parse-options.con commit parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN (baa4adc)
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3#include "cache.h"
   4#include "config.h"
   5#include "commit.h"
   6#include "color.h"
   7#include "utf8.h"
   8
   9#define OPT_SHORT 1
  10#define OPT_UNSET 2
  11
  12int optbug(const struct option *opt, const char *reason)
  13{
  14        if (opt->long_name) {
  15                if (opt->short_name)
  16                        return error("BUG: switch '%c' (--%s) %s",
  17                                     opt->short_name, opt->long_name, reason);
  18                return error("BUG: option '%s' %s", opt->long_name, reason);
  19        }
  20        return error("BUG: switch '%c' %s", opt->short_name, reason);
  21}
  22
  23static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  24                   int flags, const char **arg)
  25{
  26        if (p->opt) {
  27                *arg = p->opt;
  28                p->opt = NULL;
  29        } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
  30                *arg = (const char *)opt->defval;
  31        } else if (p->argc > 1) {
  32                p->argc--;
  33                *arg = *++p->argv;
  34        } else
  35                return error(_("%s requires a value"), optname(opt, flags));
  36        return 0;
  37}
  38
  39static void fix_filename(const char *prefix, const char **file)
  40{
  41        if (!file || !*file || !prefix || is_absolute_path(*file)
  42            || !strcmp("-", *file))
  43                return;
  44        *file = prefix_filename(prefix, *file);
  45}
  46
  47static int opt_command_mode_error(const struct option *opt,
  48                                  const struct option *all_opts,
  49                                  int flags)
  50{
  51        const struct option *that;
  52        struct strbuf that_name = STRBUF_INIT;
  53
  54        /*
  55         * Find the other option that was used to set the variable
  56         * already, and report that this is not compatible with it.
  57         */
  58        for (that = all_opts; that->type != OPTION_END; that++) {
  59                if (that == opt ||
  60                    that->type != OPTION_CMDMODE ||
  61                    that->value != opt->value ||
  62                    that->defval != *(int *)opt->value)
  63                        continue;
  64
  65                if (that->long_name)
  66                        strbuf_addf(&that_name, "--%s", that->long_name);
  67                else
  68                        strbuf_addf(&that_name, "-%c", that->short_name);
  69                error(_("%s is incompatible with %s"),
  70                      optname(opt, flags), that_name.buf);
  71                strbuf_release(&that_name);
  72                return -1;
  73        }
  74        return error(_("%s : incompatible with something else"),
  75                     optname(opt, flags));
  76}
  77
  78static int get_value(struct parse_opt_ctx_t *p,
  79                     const struct option *opt,
  80                     const struct option *all_opts,
  81                     int flags)
  82{
  83        const char *s, *arg;
  84        const int unset = flags & OPT_UNSET;
  85        int err;
  86
  87        if (unset && p->opt)
  88                return error(_("%s takes no value"), optname(opt, flags));
  89        if (unset && (opt->flags & PARSE_OPT_NONEG))
  90                return error(_("%s isn't available"), optname(opt, flags));
  91        if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
  92                return error(_("%s takes no value"), optname(opt, flags));
  93
  94        switch (opt->type) {
  95        case OPTION_LOWLEVEL_CALLBACK:
  96                return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
  97
  98        case OPTION_BIT:
  99                if (unset)
 100                        *(int *)opt->value &= ~opt->defval;
 101                else
 102                        *(int *)opt->value |= opt->defval;
 103                return 0;
 104
 105        case OPTION_NEGBIT:
 106                if (unset)
 107                        *(int *)opt->value |= opt->defval;
 108                else
 109                        *(int *)opt->value &= ~opt->defval;
 110                return 0;
 111
 112        case OPTION_COUNTUP:
 113                if (*(int *)opt->value < 0)
 114                        *(int *)opt->value = 0;
 115                *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
 116                return 0;
 117
 118        case OPTION_SET_INT:
 119                *(int *)opt->value = unset ? 0 : opt->defval;
 120                return 0;
 121
 122        case OPTION_CMDMODE:
 123                /*
 124                 * Giving the same mode option twice, although is unnecessary,
 125                 * is not a grave error, so let it pass.
 126                 */
 127                if (*(int *)opt->value && *(int *)opt->value != opt->defval)
 128                        return opt_command_mode_error(opt, all_opts, flags);
 129                *(int *)opt->value = opt->defval;
 130                return 0;
 131
 132        case OPTION_STRING:
 133                if (unset)
 134                        *(const char **)opt->value = NULL;
 135                else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 136                        *(const char **)opt->value = (const char *)opt->defval;
 137                else
 138                        return get_arg(p, opt, flags, (const char **)opt->value);
 139                return 0;
 140
 141        case OPTION_FILENAME:
 142                err = 0;
 143                if (unset)
 144                        *(const char **)opt->value = NULL;
 145                else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 146                        *(const char **)opt->value = (const char *)opt->defval;
 147                else
 148                        err = get_arg(p, opt, flags, (const char **)opt->value);
 149
 150                if (!err)
 151                        fix_filename(p->prefix, (const char **)opt->value);
 152                return err;
 153
 154        case OPTION_CALLBACK:
 155                if (unset)
 156                        return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
 157                if (opt->flags & PARSE_OPT_NOARG)
 158                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 159                if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 160                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 161                if (get_arg(p, opt, flags, &arg))
 162                        return -1;
 163                return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 164
 165        case OPTION_INTEGER:
 166                if (unset) {
 167                        *(int *)opt->value = 0;
 168                        return 0;
 169                }
 170                if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
 171                        *(int *)opt->value = opt->defval;
 172                        return 0;
 173                }
 174                if (get_arg(p, opt, flags, &arg))
 175                        return -1;
 176                *(int *)opt->value = strtol(arg, (char **)&s, 10);
 177                if (*s)
 178                        return error(_("%s expects a numerical value"),
 179                                     optname(opt, flags));
 180                return 0;
 181
 182        case OPTION_MAGNITUDE:
 183                if (unset) {
 184                        *(unsigned long *)opt->value = 0;
 185                        return 0;
 186                }
 187                if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
 188                        *(unsigned long *)opt->value = opt->defval;
 189                        return 0;
 190                }
 191                if (get_arg(p, opt, flags, &arg))
 192                        return -1;
 193                if (!git_parse_ulong(arg, opt->value))
 194                        return error(_("%s expects a non-negative integer value"
 195                                       " with an optional k/m/g suffix"),
 196                                     optname(opt, flags));
 197                return 0;
 198
 199        default:
 200                BUG("opt->type %d should not happen", opt->type);
 201        }
 202}
 203
 204static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
 205{
 206        const struct option *all_opts = options;
 207        const struct option *numopt = NULL;
 208
 209        for (; options->type != OPTION_END; options++) {
 210                if (options->short_name == *p->opt) {
 211                        p->opt = p->opt[1] ? p->opt + 1 : NULL;
 212                        return get_value(p, options, all_opts, OPT_SHORT);
 213                }
 214
 215                /*
 216                 * Handle the numerical option later, explicit one-digit
 217                 * options take precedence over it.
 218                 */
 219                if (options->type == OPTION_NUMBER)
 220                        numopt = options;
 221        }
 222        if (numopt && isdigit(*p->opt)) {
 223                size_t len = 1;
 224                char *arg;
 225                int rc;
 226
 227                while (isdigit(p->opt[len]))
 228                        len++;
 229                arg = xmemdupz(p->opt, len);
 230                p->opt = p->opt[len] ? p->opt + len : NULL;
 231                rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
 232                free(arg);
 233                return rc;
 234        }
 235        return -2;
 236}
 237
 238static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
 239                          const struct option *options)
 240{
 241        const struct option *all_opts = options;
 242        const char *arg_end = strchrnul(arg, '=');
 243        const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
 244        int abbrev_flags = 0, ambiguous_flags = 0;
 245
 246        for (; options->type != OPTION_END; options++) {
 247                const char *rest, *long_name = options->long_name;
 248                int flags = 0, opt_flags = 0;
 249
 250                if (!long_name)
 251                        continue;
 252
 253again:
 254                if (!skip_prefix(arg, long_name, &rest))
 255                        rest = NULL;
 256                if (options->type == OPTION_ARGUMENT) {
 257                        if (!rest)
 258                                continue;
 259                        if (*rest == '=')
 260                                return error(_("%s takes no value"),
 261                                             optname(options, flags));
 262                        if (*rest)
 263                                continue;
 264                        p->out[p->cpidx++] = arg - 2;
 265                        return 0;
 266                }
 267                if (!rest) {
 268                        /* abbreviated? */
 269                        if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
 270                            !strncmp(long_name, arg, arg_end - arg)) {
 271is_abbreviated:
 272                                if (abbrev_option) {
 273                                        /*
 274                                         * If this is abbreviated, it is
 275                                         * ambiguous. So when there is no
 276                                         * exact match later, we need to
 277                                         * error out.
 278                                         */
 279                                        ambiguous_option = abbrev_option;
 280                                        ambiguous_flags = abbrev_flags;
 281                                }
 282                                if (!(flags & OPT_UNSET) && *arg_end)
 283                                        p->opt = arg_end + 1;
 284                                abbrev_option = options;
 285                                abbrev_flags = flags ^ opt_flags;
 286                                continue;
 287                        }
 288                        /* negation allowed? */
 289                        if (options->flags & PARSE_OPT_NONEG)
 290                                continue;
 291                        /* negated and abbreviated very much? */
 292                        if (starts_with("no-", arg)) {
 293                                flags |= OPT_UNSET;
 294                                goto is_abbreviated;
 295                        }
 296                        /* negated? */
 297                        if (!starts_with(arg, "no-")) {
 298                                if (starts_with(long_name, "no-")) {
 299                                        long_name += 3;
 300                                        opt_flags |= OPT_UNSET;
 301                                        goto again;
 302                                }
 303                                continue;
 304                        }
 305                        flags |= OPT_UNSET;
 306                        if (!skip_prefix(arg + 3, long_name, &rest)) {
 307                                /* abbreviated and negated? */
 308                                if (starts_with(long_name, arg + 3))
 309                                        goto is_abbreviated;
 310                                else
 311                                        continue;
 312                        }
 313                }
 314                if (*rest) {
 315                        if (*rest != '=')
 316                                continue;
 317                        p->opt = rest + 1;
 318                }
 319                return get_value(p, options, all_opts, flags ^ opt_flags);
 320        }
 321
 322        if (ambiguous_option) {
 323                error(_("ambiguous option: %s "
 324                        "(could be --%s%s or --%s%s)"),
 325                        arg,
 326                        (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
 327                        ambiguous_option->long_name,
 328                        (abbrev_flags & OPT_UNSET) ?  "no-" : "",
 329                        abbrev_option->long_name);
 330                return -3;
 331        }
 332        if (abbrev_option)
 333                return get_value(p, abbrev_option, all_opts, abbrev_flags);
 334        return -2;
 335}
 336
 337static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
 338                            const struct option *options)
 339{
 340        const struct option *all_opts = options;
 341
 342        for (; options->type != OPTION_END; options++) {
 343                if (!(options->flags & PARSE_OPT_NODASH))
 344                        continue;
 345                if (options->short_name == arg[0] && arg[1] == '\0')
 346                        return get_value(p, options, all_opts, OPT_SHORT);
 347        }
 348        return -2;
 349}
 350
 351static void check_typos(const char *arg, const struct option *options)
 352{
 353        if (strlen(arg) < 3)
 354                return;
 355
 356        if (starts_with(arg, "no-")) {
 357                error(_("did you mean `--%s` (with two dashes ?)"), arg);
 358                exit(129);
 359        }
 360
 361        for (; options->type != OPTION_END; options++) {
 362                if (!options->long_name)
 363                        continue;
 364                if (starts_with(options->long_name, arg)) {
 365                        error(_("did you mean `--%s` (with two dashes ?)"), arg);
 366                        exit(129);
 367                }
 368        }
 369}
 370
 371static void parse_options_check(const struct option *opts)
 372{
 373        int err = 0;
 374        char short_opts[128];
 375
 376        memset(short_opts, '\0', sizeof(short_opts));
 377        for (; opts->type != OPTION_END; opts++) {
 378                if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
 379                    (opts->flags & PARSE_OPT_OPTARG))
 380                        err |= optbug(opts, "uses incompatible flags "
 381                                        "LASTARG_DEFAULT and OPTARG");
 382                if (opts->short_name) {
 383                        if (0x7F <= opts->short_name)
 384                                err |= optbug(opts, "invalid short name");
 385                        else if (short_opts[opts->short_name]++)
 386                                err |= optbug(opts, "short name already used");
 387                }
 388                if (opts->flags & PARSE_OPT_NODASH &&
 389                    ((opts->flags & PARSE_OPT_OPTARG) ||
 390                     !(opts->flags & PARSE_OPT_NOARG) ||
 391                     !(opts->flags & PARSE_OPT_NONEG) ||
 392                     opts->long_name))
 393                        err |= optbug(opts, "uses feature "
 394                                        "not supported for dashless options");
 395                switch (opts->type) {
 396                case OPTION_COUNTUP:
 397                case OPTION_BIT:
 398                case OPTION_NEGBIT:
 399                case OPTION_SET_INT:
 400                case OPTION_NUMBER:
 401                        if ((opts->flags & PARSE_OPT_OPTARG) ||
 402                            !(opts->flags & PARSE_OPT_NOARG))
 403                                err |= optbug(opts, "should not accept an argument");
 404                default:
 405                        ; /* ok. (usually accepts an argument) */
 406                }
 407                if (opts->argh &&
 408                    strcspn(opts->argh, " _") != strlen(opts->argh))
 409                        err |= optbug(opts, "multi-word argh should use dash to separate words");
 410        }
 411        if (err)
 412                exit(128);
 413}
 414
 415void parse_options_start(struct parse_opt_ctx_t *ctx,
 416                         int argc, const char **argv, const char *prefix,
 417                         const struct option *options, int flags)
 418{
 419        memset(ctx, 0, sizeof(*ctx));
 420        ctx->argc = argc;
 421        ctx->argv = argv;
 422        if (!(flags & PARSE_OPT_ONE_SHOT)) {
 423                ctx->argc--;
 424                ctx->argv++;
 425        }
 426        ctx->total = ctx->argc;
 427        ctx->out   = argv;
 428        ctx->prefix = prefix;
 429        ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 430        ctx->flags = flags;
 431        if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
 432            (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
 433            !(flags & PARSE_OPT_ONE_SHOT))
 434                BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
 435        if ((flags & PARSE_OPT_ONE_SHOT) &&
 436            (flags & PARSE_OPT_KEEP_ARGV0))
 437                BUG("Can't keep argv0 if you don't have it");
 438        parse_options_check(options);
 439}
 440
 441static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
 442{
 443        int printed_dashdash = 0;
 444
 445        for (; opts->type != OPTION_END; opts++) {
 446                int has_unset_form = 0;
 447                const char *name;
 448
 449                if (!opts->long_name)
 450                        continue;
 451                if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
 452                        continue;
 453                if (opts->flags & PARSE_OPT_NONEG)
 454                        continue;
 455
 456                switch (opts->type) {
 457                case OPTION_STRING:
 458                case OPTION_FILENAME:
 459                case OPTION_INTEGER:
 460                case OPTION_MAGNITUDE:
 461                case OPTION_CALLBACK:
 462                case OPTION_BIT:
 463                case OPTION_NEGBIT:
 464                case OPTION_COUNTUP:
 465                case OPTION_SET_INT:
 466                        has_unset_form = 1;
 467                        break;
 468                default:
 469                        break;
 470                }
 471                if (!has_unset_form)
 472                        continue;
 473
 474                if (skip_prefix(opts->long_name, "no-", &name)) {
 475                        if (nr_noopts < 0)
 476                                printf(" --%s", name);
 477                } else if (nr_noopts >= 0) {
 478                        if (nr_noopts && !printed_dashdash) {
 479                                printf(" --");
 480                                printed_dashdash = 1;
 481                        }
 482                        printf(" --no-%s", opts->long_name);
 483                        nr_noopts++;
 484                }
 485        }
 486}
 487
 488static int show_gitcomp(struct parse_opt_ctx_t *ctx,
 489                        const struct option *opts)
 490{
 491        const struct option *original_opts = opts;
 492        int nr_noopts = 0;
 493
 494        for (; opts->type != OPTION_END; opts++) {
 495                const char *suffix = "";
 496
 497                if (!opts->long_name)
 498                        continue;
 499                if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
 500                        continue;
 501
 502                switch (opts->type) {
 503                case OPTION_GROUP:
 504                        continue;
 505                case OPTION_STRING:
 506                case OPTION_FILENAME:
 507                case OPTION_INTEGER:
 508                case OPTION_MAGNITUDE:
 509                case OPTION_CALLBACK:
 510                        if (opts->flags & PARSE_OPT_NOARG)
 511                                break;
 512                        if (opts->flags & PARSE_OPT_OPTARG)
 513                                break;
 514                        if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
 515                                break;
 516                        suffix = "=";
 517                        break;
 518                default:
 519                        break;
 520                }
 521                if (opts->flags & PARSE_OPT_COMP_ARG)
 522                        suffix = "=";
 523                if (starts_with(opts->long_name, "no-"))
 524                        nr_noopts++;
 525                printf(" --%s%s", opts->long_name, suffix);
 526        }
 527        show_negated_gitcomp(original_opts, -1);
 528        show_negated_gitcomp(original_opts, nr_noopts);
 529        fputc('\n', stdout);
 530        return PARSE_OPT_COMPLETE;
 531}
 532
 533static int usage_with_options_internal(struct parse_opt_ctx_t *,
 534                                       const char * const *,
 535                                       const struct option *, int, int);
 536
 537int parse_options_step(struct parse_opt_ctx_t *ctx,
 538                       const struct option *options,
 539                       const char * const usagestr[])
 540{
 541        int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
 542
 543        /* we must reset ->opt, unknown short option leave it dangling */
 544        ctx->opt = NULL;
 545
 546        for (; ctx->argc; ctx->argc--, ctx->argv++) {
 547                const char *arg = ctx->argv[0];
 548
 549                if (ctx->flags & PARSE_OPT_ONE_SHOT &&
 550                    ctx->argc != ctx->total)
 551                        break;
 552
 553                if (*arg != '-' || !arg[1]) {
 554                        if (parse_nodash_opt(ctx, arg, options) == 0)
 555                                continue;
 556                        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 557                                return PARSE_OPT_NON_OPTION;
 558                        ctx->out[ctx->cpidx++] = ctx->argv[0];
 559                        continue;
 560                }
 561
 562                /* lone -h asks for help */
 563                if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
 564                        goto show_usage;
 565
 566                /* lone --git-completion-helper is asked by git-completion.bash */
 567                if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
 568                        return show_gitcomp(ctx, options);
 569
 570                if (arg[1] != '-') {
 571                        ctx->opt = arg + 1;
 572                        switch (parse_short_opt(ctx, options)) {
 573                        case -1:
 574                                return PARSE_OPT_ERROR;
 575                        case -2:
 576                                if (ctx->opt)
 577                                        check_typos(arg + 1, options);
 578                                if (internal_help && *ctx->opt == 'h')
 579                                        goto show_usage;
 580                                goto unknown;
 581                        }
 582                        if (ctx->opt)
 583                                check_typos(arg + 1, options);
 584                        while (ctx->opt) {
 585                                switch (parse_short_opt(ctx, options)) {
 586                                case -1:
 587                                        return PARSE_OPT_ERROR;
 588                                case -2:
 589                                        if (internal_help && *ctx->opt == 'h')
 590                                                goto show_usage;
 591
 592                                        /* fake a short option thing to hide the fact that we may have
 593                                         * started to parse aggregated stuff
 594                                         *
 595                                         * This is leaky, too bad.
 596                                         */
 597                                        ctx->argv[0] = xstrdup(ctx->opt - 1);
 598                                        *(char *)ctx->argv[0] = '-';
 599                                        goto unknown;
 600                                }
 601                        }
 602                        continue;
 603                }
 604
 605                if (!arg[2]) { /* "--" */
 606                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
 607                                ctx->argc--;
 608                                ctx->argv++;
 609                        }
 610                        break;
 611                }
 612
 613                if (internal_help && !strcmp(arg + 2, "help-all"))
 614                        return usage_with_options_internal(ctx, usagestr, options, 1, 0);
 615                if (internal_help && !strcmp(arg + 2, "help"))
 616                        goto show_usage;
 617                switch (parse_long_opt(ctx, arg + 2, options)) {
 618                case -1:
 619                        return PARSE_OPT_ERROR;
 620                case -2:
 621                        goto unknown;
 622                case -3:
 623                        goto show_usage;
 624                }
 625                continue;
 626unknown:
 627                if (ctx->flags & PARSE_OPT_ONE_SHOT)
 628                        break;
 629                if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
 630                        return PARSE_OPT_UNKNOWN;
 631                ctx->out[ctx->cpidx++] = ctx->argv[0];
 632                ctx->opt = NULL;
 633        }
 634        return PARSE_OPT_DONE;
 635
 636 show_usage:
 637        return usage_with_options_internal(ctx, usagestr, options, 0, 0);
 638}
 639
 640int parse_options_end(struct parse_opt_ctx_t *ctx)
 641{
 642        if (ctx->flags & PARSE_OPT_ONE_SHOT)
 643                return ctx->total - ctx->argc;
 644
 645        MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
 646        ctx->out[ctx->cpidx + ctx->argc] = NULL;
 647        return ctx->cpidx + ctx->argc;
 648}
 649
 650int parse_options(int argc, const char **argv, const char *prefix,
 651                  const struct option *options, const char * const usagestr[],
 652                  int flags)
 653{
 654        struct parse_opt_ctx_t ctx;
 655
 656        parse_options_start(&ctx, argc, argv, prefix, options, flags);
 657        switch (parse_options_step(&ctx, options, usagestr)) {
 658        case PARSE_OPT_HELP:
 659        case PARSE_OPT_ERROR:
 660                exit(129);
 661        case PARSE_OPT_COMPLETE:
 662                exit(0);
 663        case PARSE_OPT_NON_OPTION:
 664        case PARSE_OPT_DONE:
 665                break;
 666        default: /* PARSE_OPT_UNKNOWN */
 667                if (ctx.argv[0][1] == '-') {
 668                        error(_("unknown option `%s'"), ctx.argv[0] + 2);
 669                } else if (isascii(*ctx.opt)) {
 670                        error(_("unknown switch `%c'"), *ctx.opt);
 671                } else {
 672                        error(_("unknown non-ascii option in string: `%s'"),
 673                              ctx.argv[0]);
 674                }
 675                usage_with_options(usagestr, options);
 676        }
 677
 678        precompose_argv(argc, argv);
 679        return parse_options_end(&ctx);
 680}
 681
 682static int usage_argh(const struct option *opts, FILE *outfile)
 683{
 684        const char *s;
 685        int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
 686                !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
 687        if (opts->flags & PARSE_OPT_OPTARG)
 688                if (opts->long_name)
 689                        s = literal ? "[=%s]" : "[=<%s>]";
 690                else
 691                        s = literal ? "[%s]" : "[<%s>]";
 692        else
 693                s = literal ? " %s" : " <%s>";
 694        return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
 695}
 696
 697#define USAGE_OPTS_WIDTH 24
 698#define USAGE_GAP         2
 699
 700static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 701                                       const char * const *usagestr,
 702                                       const struct option *opts, int full, int err)
 703{
 704        FILE *outfile = err ? stderr : stdout;
 705        int need_newline;
 706
 707        if (!usagestr)
 708                return PARSE_OPT_HELP;
 709
 710        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 711                fprintf(outfile, "cat <<\\EOF\n");
 712
 713        fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
 714        while (*usagestr && **usagestr)
 715                /*
 716                 * TRANSLATORS: the colon here should align with the
 717                 * one in "usage: %s" translation.
 718                 */
 719                fprintf_ln(outfile, _("   or: %s"), _(*usagestr++));
 720        while (*usagestr) {
 721                if (**usagestr)
 722                        fprintf_ln(outfile, _("    %s"), _(*usagestr));
 723                else
 724                        fputc('\n', outfile);
 725                usagestr++;
 726        }
 727
 728        need_newline = 1;
 729
 730        for (; opts->type != OPTION_END; opts++) {
 731                size_t pos;
 732                int pad;
 733
 734                if (opts->type == OPTION_GROUP) {
 735                        fputc('\n', outfile);
 736                        need_newline = 0;
 737                        if (*opts->help)
 738                                fprintf(outfile, "%s\n", _(opts->help));
 739                        continue;
 740                }
 741                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
 742                        continue;
 743
 744                if (need_newline) {
 745                        fputc('\n', outfile);
 746                        need_newline = 0;
 747                }
 748
 749                pos = fprintf(outfile, "    ");
 750                if (opts->short_name) {
 751                        if (opts->flags & PARSE_OPT_NODASH)
 752                                pos += fprintf(outfile, "%c", opts->short_name);
 753                        else
 754                                pos += fprintf(outfile, "-%c", opts->short_name);
 755                }
 756                if (opts->long_name && opts->short_name)
 757                        pos += fprintf(outfile, ", ");
 758                if (opts->long_name)
 759                        pos += fprintf(outfile, "--%s", opts->long_name);
 760                if (opts->type == OPTION_NUMBER)
 761                        pos += utf8_fprintf(outfile, _("-NUM"));
 762
 763                if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
 764                    !(opts->flags & PARSE_OPT_NOARG))
 765                        pos += usage_argh(opts, outfile);
 766
 767                if (pos <= USAGE_OPTS_WIDTH)
 768                        pad = USAGE_OPTS_WIDTH - pos;
 769                else {
 770                        fputc('\n', outfile);
 771                        pad = USAGE_OPTS_WIDTH;
 772                }
 773                fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
 774        }
 775        fputc('\n', outfile);
 776
 777        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 778                fputs("EOF\n", outfile);
 779
 780        return PARSE_OPT_HELP;
 781}
 782
 783void NORETURN usage_with_options(const char * const *usagestr,
 784                        const struct option *opts)
 785{
 786        usage_with_options_internal(NULL, usagestr, opts, 0, 1);
 787        exit(129);
 788}
 789
 790void NORETURN usage_msg_opt(const char *msg,
 791                   const char * const *usagestr,
 792                   const struct option *options)
 793{
 794        fprintf(stderr, "fatal: %s\n\n", msg);
 795        usage_with_options(usagestr, options);
 796}
 797
 798const char *optname(const struct option *opt, int flags)
 799{
 800        static struct strbuf sb = STRBUF_INIT;
 801
 802        strbuf_reset(&sb);
 803        if (flags & OPT_SHORT)
 804                strbuf_addf(&sb, "switch `%c'", opt->short_name);
 805        else if (flags & OPT_UNSET)
 806                strbuf_addf(&sb, "option `no-%s'", opt->long_name);
 807        else
 808                strbuf_addf(&sb, "option `%s'", opt->long_name);
 809
 810        return sb.buf;
 811}