parse-options.con commit parse-options: option to let --git-completion-helper show negative form (2b1c01d)
   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 opterror(opt, "requires a value", 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 message = STRBUF_INIT;
  53        struct strbuf that_name = STRBUF_INIT;
  54
  55        /*
  56         * Find the other option that was used to set the variable
  57         * already, and report that this is not compatible with it.
  58         */
  59        for (that = all_opts; that->type != OPTION_END; that++) {
  60                if (that == opt ||
  61                    that->type != OPTION_CMDMODE ||
  62                    that->value != opt->value ||
  63                    that->defval != *(int *)opt->value)
  64                        continue;
  65
  66                if (that->long_name)
  67                        strbuf_addf(&that_name, "--%s", that->long_name);
  68                else
  69                        strbuf_addf(&that_name, "-%c", that->short_name);
  70                strbuf_addf(&message, ": incompatible with %s", that_name.buf);
  71                strbuf_release(&that_name);
  72                opterror(opt, message.buf, flags);
  73                strbuf_release(&message);
  74                return -1;
  75        }
  76        return opterror(opt, ": incompatible with something else", flags);
  77}
  78
  79static int get_value(struct parse_opt_ctx_t *p,
  80                     const struct option *opt,
  81                     const struct option *all_opts,
  82                     int flags)
  83{
  84        const char *s, *arg;
  85        const int unset = flags & OPT_UNSET;
  86        int err;
  87
  88        if (unset && p->opt)
  89                return opterror(opt, "takes no value", flags);
  90        if (unset && (opt->flags & PARSE_OPT_NONEG))
  91                return opterror(opt, "isn't available", flags);
  92        if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
  93                return opterror(opt, "takes no value", flags);
  94
  95        switch (opt->type) {
  96        case OPTION_LOWLEVEL_CALLBACK:
  97                return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
  98
  99        case OPTION_BIT:
 100                if (unset)
 101                        *(int *)opt->value &= ~opt->defval;
 102                else
 103                        *(int *)opt->value |= opt->defval;
 104                return 0;
 105
 106        case OPTION_NEGBIT:
 107                if (unset)
 108                        *(int *)opt->value |= opt->defval;
 109                else
 110                        *(int *)opt->value &= ~opt->defval;
 111                return 0;
 112
 113        case OPTION_COUNTUP:
 114                if (*(int *)opt->value < 0)
 115                        *(int *)opt->value = 0;
 116                *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
 117                return 0;
 118
 119        case OPTION_SET_INT:
 120                *(int *)opt->value = unset ? 0 : opt->defval;
 121                return 0;
 122
 123        case OPTION_CMDMODE:
 124                /*
 125                 * Giving the same mode option twice, although is unnecessary,
 126                 * is not a grave error, so let it pass.
 127                 */
 128                if (*(int *)opt->value && *(int *)opt->value != opt->defval)
 129                        return opt_command_mode_error(opt, all_opts, flags);
 130                *(int *)opt->value = opt->defval;
 131                return 0;
 132
 133        case OPTION_STRING:
 134                if (unset)
 135                        *(const char **)opt->value = NULL;
 136                else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 137                        *(const char **)opt->value = (const char *)opt->defval;
 138                else
 139                        return get_arg(p, opt, flags, (const char **)opt->value);
 140                return 0;
 141
 142        case OPTION_FILENAME:
 143                err = 0;
 144                if (unset)
 145                        *(const char **)opt->value = NULL;
 146                else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 147                        *(const char **)opt->value = (const char *)opt->defval;
 148                else
 149                        err = get_arg(p, opt, flags, (const char **)opt->value);
 150
 151                if (!err)
 152                        fix_filename(p->prefix, (const char **)opt->value);
 153                return err;
 154
 155        case OPTION_CALLBACK:
 156                if (unset)
 157                        return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
 158                if (opt->flags & PARSE_OPT_NOARG)
 159                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 160                if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
 161                        return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 162                if (get_arg(p, opt, flags, &arg))
 163                        return -1;
 164                return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 165
 166        case OPTION_INTEGER:
 167                if (unset) {
 168                        *(int *)opt->value = 0;
 169                        return 0;
 170                }
 171                if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
 172                        *(int *)opt->value = opt->defval;
 173                        return 0;
 174                }
 175                if (get_arg(p, opt, flags, &arg))
 176                        return -1;
 177                *(int *)opt->value = strtol(arg, (char **)&s, 10);
 178                if (*s)
 179                        return opterror(opt, "expects a numerical value", 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 opterror(opt,
 195                                "expects a non-negative integer value with an optional k/m/g suffix",
 196                                flags);
 197                return 0;
 198
 199        default:
 200                die("should not happen, someone must be hit on the forehead");
 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 opterror(options, "takes no value", flags);
 261                        if (*rest)
 262                                continue;
 263                        p->out[p->cpidx++] = arg - 2;
 264                        return 0;
 265                }
 266                if (!rest) {
 267                        /* abbreviated? */
 268                        if (!strncmp(long_name, arg, arg_end - arg)) {
 269is_abbreviated:
 270                                if (abbrev_option) {
 271                                        /*
 272                                         * If this is abbreviated, it is
 273                                         * ambiguous. So when there is no
 274                                         * exact match later, we need to
 275                                         * error out.
 276                                         */
 277                                        ambiguous_option = abbrev_option;
 278                                        ambiguous_flags = abbrev_flags;
 279                                }
 280                                if (!(flags & OPT_UNSET) && *arg_end)
 281                                        p->opt = arg_end + 1;
 282                                abbrev_option = options;
 283                                abbrev_flags = flags ^ opt_flags;
 284                                continue;
 285                        }
 286                        /* negation allowed? */
 287                        if (options->flags & PARSE_OPT_NONEG)
 288                                continue;
 289                        /* negated and abbreviated very much? */
 290                        if (starts_with("no-", arg)) {
 291                                flags |= OPT_UNSET;
 292                                goto is_abbreviated;
 293                        }
 294                        /* negated? */
 295                        if (!starts_with(arg, "no-")) {
 296                                if (starts_with(long_name, "no-")) {
 297                                        long_name += 3;
 298                                        opt_flags |= OPT_UNSET;
 299                                        goto again;
 300                                }
 301                                continue;
 302                        }
 303                        flags |= OPT_UNSET;
 304                        if (!skip_prefix(arg + 3, long_name, &rest)) {
 305                                /* abbreviated and negated? */
 306                                if (starts_with(long_name, arg + 3))
 307                                        goto is_abbreviated;
 308                                else
 309                                        continue;
 310                        }
 311                }
 312                if (*rest) {
 313                        if (*rest != '=')
 314                                continue;
 315                        p->opt = rest + 1;
 316                }
 317                return get_value(p, options, all_opts, flags ^ opt_flags);
 318        }
 319
 320        if (ambiguous_option) {
 321                error("Ambiguous option: %s "
 322                        "(could be --%s%s or --%s%s)",
 323                        arg,
 324                        (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
 325                        ambiguous_option->long_name,
 326                        (abbrev_flags & OPT_UNSET) ?  "no-" : "",
 327                        abbrev_option->long_name);
 328                return -3;
 329        }
 330        if (abbrev_option)
 331                return get_value(p, abbrev_option, all_opts, abbrev_flags);
 332        return -2;
 333}
 334
 335static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
 336                            const struct option *options)
 337{
 338        const struct option *all_opts = options;
 339
 340        for (; options->type != OPTION_END; options++) {
 341                if (!(options->flags & PARSE_OPT_NODASH))
 342                        continue;
 343                if (options->short_name == arg[0] && arg[1] == '\0')
 344                        return get_value(p, options, all_opts, OPT_SHORT);
 345        }
 346        return -2;
 347}
 348
 349static void check_typos(const char *arg, const struct option *options)
 350{
 351        if (strlen(arg) < 3)
 352                return;
 353
 354        if (starts_with(arg, "no-")) {
 355                error ("did you mean `--%s` (with two dashes ?)", arg);
 356                exit(129);
 357        }
 358
 359        for (; options->type != OPTION_END; options++) {
 360                if (!options->long_name)
 361                        continue;
 362                if (starts_with(options->long_name, arg)) {
 363                        error ("did you mean `--%s` (with two dashes ?)", arg);
 364                        exit(129);
 365                }
 366        }
 367}
 368
 369static void parse_options_check(const struct option *opts)
 370{
 371        int err = 0;
 372        char short_opts[128];
 373
 374        memset(short_opts, '\0', sizeof(short_opts));
 375        for (; opts->type != OPTION_END; opts++) {
 376                if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
 377                    (opts->flags & PARSE_OPT_OPTARG))
 378                        err |= optbug(opts, "uses incompatible flags "
 379                                        "LASTARG_DEFAULT and OPTARG");
 380                if (opts->short_name) {
 381                        if (0x7F <= opts->short_name)
 382                                err |= optbug(opts, "invalid short name");
 383                        else if (short_opts[opts->short_name]++)
 384                                err |= optbug(opts, "short name already used");
 385                }
 386                if (opts->flags & PARSE_OPT_NODASH &&
 387                    ((opts->flags & PARSE_OPT_OPTARG) ||
 388                     !(opts->flags & PARSE_OPT_NOARG) ||
 389                     !(opts->flags & PARSE_OPT_NONEG) ||
 390                     opts->long_name))
 391                        err |= optbug(opts, "uses feature "
 392                                        "not supported for dashless options");
 393                switch (opts->type) {
 394                case OPTION_COUNTUP:
 395                case OPTION_BIT:
 396                case OPTION_NEGBIT:
 397                case OPTION_SET_INT:
 398                case OPTION_NUMBER:
 399                        if ((opts->flags & PARSE_OPT_OPTARG) ||
 400                            !(opts->flags & PARSE_OPT_NOARG))
 401                                err |= optbug(opts, "should not accept an argument");
 402                default:
 403                        ; /* ok. (usually accepts an argument) */
 404                }
 405                if (opts->argh &&
 406                    strcspn(opts->argh, " _") != strlen(opts->argh))
 407                        err |= optbug(opts, "multi-word argh should use dash to separate words");
 408        }
 409        if (err)
 410                exit(128);
 411}
 412
 413void parse_options_start(struct parse_opt_ctx_t *ctx,
 414                         int argc, const char **argv, const char *prefix,
 415                         const struct option *options, int flags)
 416{
 417        memset(ctx, 0, sizeof(*ctx));
 418        ctx->argc = ctx->total = argc - 1;
 419        ctx->argv = argv + 1;
 420        ctx->out  = argv;
 421        ctx->prefix = prefix;
 422        ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 423        ctx->flags = flags;
 424        if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
 425            (flags & PARSE_OPT_STOP_AT_NON_OPTION))
 426                die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
 427        parse_options_check(options);
 428}
 429
 430static int show_gitcomp(struct parse_opt_ctx_t *ctx,
 431                        const struct option *opts)
 432{
 433        for (; opts->type != OPTION_END; opts++) {
 434                const char *suffix = "";
 435                int has_unset_form = 0;
 436
 437                if (!opts->long_name)
 438                        continue;
 439                if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
 440                        continue;
 441
 442                switch (opts->type) {
 443                case OPTION_GROUP:
 444                        continue;
 445                case OPTION_STRING:
 446                case OPTION_FILENAME:
 447                case OPTION_INTEGER:
 448                case OPTION_MAGNITUDE:
 449                case OPTION_CALLBACK:
 450                        has_unset_form = 1;
 451
 452                        if (opts->flags & PARSE_OPT_NOARG)
 453                                break;
 454                        if (opts->flags & PARSE_OPT_OPTARG)
 455                                break;
 456                        if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
 457                                break;
 458                        suffix = "=";
 459                        break;
 460                case OPTION_BIT:
 461                case OPTION_NEGBIT:
 462                case OPTION_COUNTUP:
 463                case OPTION_SET_INT:
 464                        has_unset_form = 1;
 465                        break;
 466                default:
 467                        break;
 468                }
 469                if (opts->flags & PARSE_OPT_COMP_ARG)
 470                        suffix = "=";
 471                printf(" --%s%s", opts->long_name, suffix);
 472
 473                if (has_unset_form && !(opts->flags & PARSE_OPT_NONEG)) {
 474                        const char *name;
 475
 476                        if (skip_prefix(opts->long_name, "no-", &name))
 477                                printf(" --%s", name);
 478                        else
 479                                printf(" --no-%s", opts->long_name);
 480                }
 481        }
 482        fputc('\n', stdout);
 483        exit(0);
 484}
 485
 486static int usage_with_options_internal(struct parse_opt_ctx_t *,
 487                                       const char * const *,
 488                                       const struct option *, int, int);
 489
 490int parse_options_step(struct parse_opt_ctx_t *ctx,
 491                       const struct option *options,
 492                       const char * const usagestr[])
 493{
 494        int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
 495
 496        /* we must reset ->opt, unknown short option leave it dangling */
 497        ctx->opt = NULL;
 498
 499        for (; ctx->argc; ctx->argc--, ctx->argv++) {
 500                const char *arg = ctx->argv[0];
 501
 502                if (*arg != '-' || !arg[1]) {
 503                        if (parse_nodash_opt(ctx, arg, options) == 0)
 504                                continue;
 505                        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 506                                return PARSE_OPT_NON_OPTION;
 507                        ctx->out[ctx->cpidx++] = ctx->argv[0];
 508                        continue;
 509                }
 510
 511                /* lone -h asks for help */
 512                if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
 513                        goto show_usage;
 514
 515                /* lone --git-completion-helper is asked by git-completion.bash */
 516                if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
 517                        return show_gitcomp(ctx, options);
 518
 519                if (arg[1] != '-') {
 520                        ctx->opt = arg + 1;
 521                        switch (parse_short_opt(ctx, options)) {
 522                        case -1:
 523                                return PARSE_OPT_ERROR;
 524                        case -2:
 525                                if (ctx->opt)
 526                                        check_typos(arg + 1, options);
 527                                if (internal_help && *ctx->opt == 'h')
 528                                        goto show_usage;
 529                                goto unknown;
 530                        }
 531                        if (ctx->opt)
 532                                check_typos(arg + 1, options);
 533                        while (ctx->opt) {
 534                                switch (parse_short_opt(ctx, options)) {
 535                                case -1:
 536                                        return PARSE_OPT_ERROR;
 537                                case -2:
 538                                        if (internal_help && *ctx->opt == 'h')
 539                                                goto show_usage;
 540
 541                                        /* fake a short option thing to hide the fact that we may have
 542                                         * started to parse aggregated stuff
 543                                         *
 544                                         * This is leaky, too bad.
 545                                         */
 546                                        ctx->argv[0] = xstrdup(ctx->opt - 1);
 547                                        *(char *)ctx->argv[0] = '-';
 548                                        goto unknown;
 549                                }
 550                        }
 551                        continue;
 552                }
 553
 554                if (!arg[2]) { /* "--" */
 555                        if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
 556                                ctx->argc--;
 557                                ctx->argv++;
 558                        }
 559                        break;
 560                }
 561
 562                if (internal_help && !strcmp(arg + 2, "help-all"))
 563                        return usage_with_options_internal(ctx, usagestr, options, 1, 0);
 564                if (internal_help && !strcmp(arg + 2, "help"))
 565                        goto show_usage;
 566                switch (parse_long_opt(ctx, arg + 2, options)) {
 567                case -1:
 568                        return PARSE_OPT_ERROR;
 569                case -2:
 570                        goto unknown;
 571                case -3:
 572                        goto show_usage;
 573                }
 574                continue;
 575unknown:
 576                if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
 577                        return PARSE_OPT_UNKNOWN;
 578                ctx->out[ctx->cpidx++] = ctx->argv[0];
 579                ctx->opt = NULL;
 580        }
 581        return PARSE_OPT_DONE;
 582
 583 show_usage:
 584        return usage_with_options_internal(ctx, usagestr, options, 0, 0);
 585}
 586
 587int parse_options_end(struct parse_opt_ctx_t *ctx)
 588{
 589        MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
 590        ctx->out[ctx->cpidx + ctx->argc] = NULL;
 591        return ctx->cpidx + ctx->argc;
 592}
 593
 594int parse_options(int argc, const char **argv, const char *prefix,
 595                  const struct option *options, const char * const usagestr[],
 596                  int flags)
 597{
 598        struct parse_opt_ctx_t ctx;
 599
 600        parse_options_start(&ctx, argc, argv, prefix, options, flags);
 601        switch (parse_options_step(&ctx, options, usagestr)) {
 602        case PARSE_OPT_HELP:
 603        case PARSE_OPT_ERROR:
 604                exit(129);
 605        case PARSE_OPT_NON_OPTION:
 606        case PARSE_OPT_DONE:
 607                break;
 608        default: /* PARSE_OPT_UNKNOWN */
 609                if (ctx.argv[0][1] == '-') {
 610                        error("unknown option `%s'", ctx.argv[0] + 2);
 611                } else if (isascii(*ctx.opt)) {
 612                        error("unknown switch `%c'", *ctx.opt);
 613                } else {
 614                        error("unknown non-ascii option in string: `%s'",
 615                              ctx.argv[0]);
 616                }
 617                usage_with_options(usagestr, options);
 618        }
 619
 620        precompose_argv(argc, argv);
 621        return parse_options_end(&ctx);
 622}
 623
 624static int usage_argh(const struct option *opts, FILE *outfile)
 625{
 626        const char *s;
 627        int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
 628        if (opts->flags & PARSE_OPT_OPTARG)
 629                if (opts->long_name)
 630                        s = literal ? "[=%s]" : "[=<%s>]";
 631                else
 632                        s = literal ? "[%s]" : "[<%s>]";
 633        else
 634                s = literal ? " %s" : " <%s>";
 635        return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
 636}
 637
 638#define USAGE_OPTS_WIDTH 24
 639#define USAGE_GAP         2
 640
 641static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 642                                       const char * const *usagestr,
 643                                       const struct option *opts, int full, int err)
 644{
 645        FILE *outfile = err ? stderr : stdout;
 646        int need_newline;
 647
 648        if (!usagestr)
 649                return PARSE_OPT_HELP;
 650
 651        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 652                fprintf(outfile, "cat <<\\EOF\n");
 653
 654        fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
 655        while (*usagestr && **usagestr)
 656                /*
 657                 * TRANSLATORS: the colon here should align with the
 658                 * one in "usage: %s" translation.
 659                 */
 660                fprintf_ln(outfile, _("   or: %s"), _(*usagestr++));
 661        while (*usagestr) {
 662                if (**usagestr)
 663                        fprintf_ln(outfile, _("    %s"), _(*usagestr));
 664                else
 665                        fputc('\n', outfile);
 666                usagestr++;
 667        }
 668
 669        need_newline = 1;
 670
 671        for (; opts->type != OPTION_END; opts++) {
 672                size_t pos;
 673                int pad;
 674
 675                if (opts->type == OPTION_GROUP) {
 676                        fputc('\n', outfile);
 677                        need_newline = 0;
 678                        if (*opts->help)
 679                                fprintf(outfile, "%s\n", _(opts->help));
 680                        continue;
 681                }
 682                if (!full && (opts->flags & PARSE_OPT_HIDDEN))
 683                        continue;
 684
 685                if (need_newline) {
 686                        fputc('\n', outfile);
 687                        need_newline = 0;
 688                }
 689
 690                pos = fprintf(outfile, "    ");
 691                if (opts->short_name) {
 692                        if (opts->flags & PARSE_OPT_NODASH)
 693                                pos += fprintf(outfile, "%c", opts->short_name);
 694                        else
 695                                pos += fprintf(outfile, "-%c", opts->short_name);
 696                }
 697                if (opts->long_name && opts->short_name)
 698                        pos += fprintf(outfile, ", ");
 699                if (opts->long_name)
 700                        pos += fprintf(outfile, "--%s", opts->long_name);
 701                if (opts->type == OPTION_NUMBER)
 702                        pos += utf8_fprintf(outfile, _("-NUM"));
 703
 704                if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
 705                    !(opts->flags & PARSE_OPT_NOARG))
 706                        pos += usage_argh(opts, outfile);
 707
 708                if (pos <= USAGE_OPTS_WIDTH)
 709                        pad = USAGE_OPTS_WIDTH - pos;
 710                else {
 711                        fputc('\n', outfile);
 712                        pad = USAGE_OPTS_WIDTH;
 713                }
 714                fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
 715        }
 716        fputc('\n', outfile);
 717
 718        if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 719                fputs("EOF\n", outfile);
 720
 721        return PARSE_OPT_HELP;
 722}
 723
 724void NORETURN usage_with_options(const char * const *usagestr,
 725                        const struct option *opts)
 726{
 727        usage_with_options_internal(NULL, usagestr, opts, 0, 1);
 728        exit(129);
 729}
 730
 731void NORETURN usage_msg_opt(const char *msg,
 732                   const char * const *usagestr,
 733                   const struct option *options)
 734{
 735        fprintf(stderr, "fatal: %s\n\n", msg);
 736        usage_with_options(usagestr, options);
 737}
 738
 739#undef opterror
 740int opterror(const struct option *opt, const char *reason, int flags)
 741{
 742        if (flags & OPT_SHORT)
 743                return error("switch `%c' %s", opt->short_name, reason);
 744        if (flags & OPT_UNSET)
 745                return error("option `no-%s' %s", opt->long_name, reason);
 746        return error("option `%s' %s", opt->long_name, reason);
 747}