Git fork
at reftables-rust 1539 lines 42 kB view raw
1#include "git-compat-util.h" 2#include "parse-options.h" 3#include "abspath.h" 4#include "parse.h" 5#include "gettext.h" 6#include "strbuf.h" 7#include "string-list.h" 8#include "utf8.h" 9 10static int disallow_abbreviated_options; 11 12enum opt_parsed { 13 OPT_LONG = 0, 14 OPT_SHORT = 1<<0, 15 OPT_UNSET = 1<<1, 16}; 17 18static void optbug(const struct option *opt, const char *reason) 19{ 20 if (opt->long_name && opt->short_name) 21 bug("switch '%c' (--%s) %s", opt->short_name, 22 opt->long_name, reason); 23 else if (opt->long_name) 24 bug("option '%s' %s", opt->long_name, reason); 25 else 26 bug("switch '%c' %s", opt->short_name, reason); 27} 28 29static const char *optname(const struct option *opt, enum opt_parsed flags) 30{ 31 static struct strbuf sb = STRBUF_INIT; 32 33 strbuf_reset(&sb); 34 if (flags & OPT_SHORT) 35 strbuf_addf(&sb, "switch `%c'", opt->short_name); 36 else if (flags & OPT_UNSET) 37 strbuf_addf(&sb, "option `no-%s'", opt->long_name); 38 else if (flags == OPT_LONG) 39 strbuf_addf(&sb, "option `%s'", opt->long_name); 40 else 41 BUG("optname() got unknown flags %d", flags); 42 43 return sb.buf; 44} 45 46static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, 47 const struct option *opt, 48 enum opt_parsed flags, const char **arg) 49{ 50 if (p->opt) { 51 *arg = p->opt; 52 p->opt = NULL; 53 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { 54 *arg = (const char *)opt->defval; 55 } else if (p->argc > 1) { 56 p->argc--; 57 *arg = *++p->argv; 58 } else 59 return error(_("%s requires a value"), optname(opt, flags)); 60 return 0; 61} 62 63static char *fix_filename(const char *prefix, const char *file) 64{ 65 if (!file || !*file) 66 return NULL; 67 else 68 return prefix_filename_except_for_dash(prefix, file); 69} 70 71static int do_get_int_value(const void *value, size_t precision, intmax_t *ret) 72{ 73 switch (precision) { 74 case sizeof(int8_t): 75 *ret = *(int8_t *)value; 76 return 0; 77 case sizeof(int16_t): 78 *ret = *(int16_t *)value; 79 return 0; 80 case sizeof(int32_t): 81 *ret = *(int32_t *)value; 82 return 0; 83 case sizeof(int64_t): 84 *ret = *(int64_t *)value; 85 return 0; 86 default: 87 return -1; 88 } 89} 90 91static intmax_t get_int_value(const struct option *opt, enum opt_parsed flags) 92{ 93 intmax_t ret; 94 if (do_get_int_value(opt->value, opt->precision, &ret)) 95 BUG("invalid precision for option %s", optname(opt, flags)); 96 return ret; 97} 98 99static enum parse_opt_result set_int_value(const struct option *opt, 100 enum opt_parsed flags, 101 intmax_t value) 102{ 103 switch (opt->precision) { 104 case sizeof(int8_t): 105 *(int8_t *)opt->value = value; 106 return 0; 107 case sizeof(int16_t): 108 *(int16_t *)opt->value = value; 109 return 0; 110 case sizeof(int32_t): 111 *(int32_t *)opt->value = value; 112 return 0; 113 case sizeof(int64_t): 114 *(int64_t *)opt->value = value; 115 return 0; 116 default: 117 BUG("invalid precision for option %s", optname(opt, flags)); 118 } 119} 120 121static int signed_int_fits(intmax_t value, size_t precision) 122{ 123 size_t bits = precision * CHAR_BIT; 124 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits); 125 intmax_t lower_bound = -upper_bound - 1; 126 return lower_bound <= value && value <= upper_bound; 127} 128 129static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, 130 const struct option *opt, 131 enum opt_parsed flags, 132 const char **argp) 133{ 134 const char *arg; 135 const int unset = flags & OPT_UNSET; 136 137 if (unset && p->opt) 138 return error(_("%s takes no value"), optname(opt, flags)); 139 if (unset && (opt->flags & PARSE_OPT_NONEG)) 140 return error(_("%s isn't available"), optname(opt, flags)); 141 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) 142 return error(_("%s takes no value"), optname(opt, flags)); 143 144 switch (opt->type) { 145 case OPTION_LOWLEVEL_CALLBACK: 146 return opt->ll_callback(p, opt, NULL, unset); 147 148 case OPTION_BIT: 149 { 150 intmax_t value = get_int_value(opt, flags); 151 if (unset) 152 value &= ~opt->defval; 153 else 154 value |= opt->defval; 155 return set_int_value(opt, flags, value); 156 } 157 158 case OPTION_NEGBIT: 159 { 160 intmax_t value = get_int_value(opt, flags); 161 if (unset) 162 value |= opt->defval; 163 else 164 value &= ~opt->defval; 165 return set_int_value(opt, flags, value); 166 } 167 168 case OPTION_BITOP: 169 { 170 intmax_t value = get_int_value(opt, flags); 171 if (unset) 172 BUG("BITOP can't have unset form"); 173 value &= ~opt->extra; 174 value |= opt->defval; 175 return set_int_value(opt, flags, value); 176 } 177 178 case OPTION_COUNTUP: 179 { 180 size_t bits = CHAR_BIT * opt->precision; 181 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits); 182 intmax_t value = get_int_value(opt, flags); 183 184 if (value < 0) 185 value = 0; 186 if (unset) 187 value = 0; 188 else if (value < upper_bound) 189 value++; 190 else 191 return error(_("value for %s exceeds %"PRIdMAX), 192 optname(opt, flags), upper_bound); 193 return set_int_value(opt, flags, value); 194 } 195 196 case OPTION_SET_INT: 197 return set_int_value(opt, flags, unset ? 0 : opt->defval); 198 199 case OPTION_STRING: 200 if (unset) 201 *(const char **)opt->value = NULL; 202 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) 203 *(const char **)opt->value = (const char *)opt->defval; 204 else 205 return get_arg(p, opt, flags, (const char **)opt->value); 206 return 0; 207 208 case OPTION_FILENAME: 209 { 210 const char *value; 211 int is_optional; 212 213 if (unset) 214 value = NULL; 215 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) 216 value = (char *)opt->defval; 217 else { 218 int err = get_arg(p, opt, flags, &value); 219 if (err) 220 return err; 221 } 222 if (!value) 223 return 0; 224 225 is_optional = skip_prefix(value, ":(optional)", &value); 226 if (!value) 227 is_optional = 0; 228 value = fix_filename(p->prefix, value); 229 if (is_optional && is_empty_or_missing_file(value)) { 230 free((char *)value); 231 } else { 232 FREE_AND_NULL(*(char **)opt->value); 233 *(const char **)opt->value = value; 234 } 235 return 0; 236 } 237 case OPTION_CALLBACK: 238 { 239 const char *p_arg = NULL; 240 int p_unset; 241 242 if (unset) 243 p_unset = 1; 244 else if (opt->flags & PARSE_OPT_NOARG) 245 p_unset = 0; 246 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) 247 p_unset = 0; 248 else if (get_arg(p, opt, flags, &arg)) 249 return -1; 250 else { 251 p_unset = 0; 252 p_arg = arg; 253 } 254 if (opt->flags & PARSE_OPT_CMDMODE) 255 *argp = p_arg; 256 if (opt->callback) 257 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0; 258 else 259 return (*opt->ll_callback)(p, opt, p_arg, p_unset); 260 } 261 case OPTION_INTEGER: 262 { 263 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - CHAR_BIT * opt->precision); 264 intmax_t lower_bound = -upper_bound - 1; 265 intmax_t value; 266 267 if (unset) { 268 value = 0; 269 } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { 270 value = opt->defval; 271 } else if (get_arg(p, opt, flags, &arg)) { 272 return -1; 273 } else if (!*arg) { 274 return error(_("%s expects a numerical value"), 275 optname(opt, flags)); 276 } else if (!git_parse_signed(arg, &value, upper_bound)) { 277 if (errno == ERANGE) 278 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"), 279 arg, optname(opt, flags), lower_bound, upper_bound); 280 281 return error(_("%s expects an integer value with an optional k/m/g suffix"), 282 optname(opt, flags)); 283 } 284 285 if (value < lower_bound) 286 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"), 287 arg, optname(opt, flags), (intmax_t)lower_bound, (intmax_t)upper_bound); 288 289 return set_int_value(opt, flags, value); 290 } 291 case OPTION_UNSIGNED: 292 { 293 uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision); 294 uintmax_t value; 295 296 if (unset) { 297 value = 0; 298 } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { 299 value = opt->defval; 300 } else if (get_arg(p, opt, flags, &arg)) { 301 return -1; 302 } else if (!*arg) { 303 return error(_("%s expects a numerical value"), 304 optname(opt, flags)); 305 } else if (!git_parse_unsigned(arg, &value, upper_bound)) { 306 if (errno == ERANGE) 307 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"), 308 arg, optname(opt, flags), (uintmax_t) 0, upper_bound); 309 310 return error(_("%s expects a non-negative integer value" 311 " with an optional k/m/g suffix"), 312 optname(opt, flags)); 313 } 314 315 switch (opt->precision) { 316 case 1: 317 *(uint8_t *)opt->value = value; 318 return 0; 319 case 2: 320 *(uint16_t *)opt->value = value; 321 return 0; 322 case 4: 323 *(uint32_t *)opt->value = value; 324 return 0; 325 case 8: 326 *(uint64_t *)opt->value = value; 327 return 0; 328 default: 329 BUG("invalid precision for option %s", 330 optname(opt, flags)); 331 } 332 } 333 334 default: 335 BUG("opt->type %d should not happen", opt->type); 336 } 337} 338 339struct parse_opt_cmdmode_list { 340 intmax_t value; 341 void *value_ptr; 342 size_t precision; 343 const struct option *opt; 344 const char *arg; 345 enum opt_parsed flags; 346 struct parse_opt_cmdmode_list *next; 347}; 348 349static void build_cmdmode_list(struct parse_opt_ctx_t *ctx, 350 const struct option *opts) 351{ 352 ctx->cmdmode_list = NULL; 353 354 for (; opts->type != OPTION_END; opts++) { 355 struct parse_opt_cmdmode_list *elem = ctx->cmdmode_list; 356 void *value_ptr = opts->value; 357 358 if (!(opts->flags & PARSE_OPT_CMDMODE) || !value_ptr) 359 continue; 360 361 while (elem && elem->value_ptr != value_ptr) 362 elem = elem->next; 363 if (elem) 364 continue; 365 366 CALLOC_ARRAY(elem, 1); 367 elem->value_ptr = value_ptr; 368 elem->precision = opts->precision; 369 if (do_get_int_value(value_ptr, opts->precision, &elem->value)) 370 optbug(opts, "has invalid precision"); 371 elem->next = ctx->cmdmode_list; 372 ctx->cmdmode_list = elem; 373 } 374 BUG_if_bug("invalid 'struct option'"); 375} 376 377static char *optnamearg(const struct option *opt, const char *arg, 378 enum opt_parsed flags) 379{ 380 if (flags & OPT_SHORT) 381 return xstrfmt("-%c%s", opt->short_name, arg ? arg : ""); 382 return xstrfmt("--%s%s%s%s", flags & OPT_UNSET ? "no-" : "", 383 opt->long_name, arg ? "=" : "", arg ? arg : ""); 384} 385 386static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, 387 const struct option *opt, 388 enum opt_parsed flags) 389{ 390 const char *arg = NULL; 391 enum parse_opt_result result = do_get_value(p, opt, flags, &arg); 392 struct parse_opt_cmdmode_list *elem = p->cmdmode_list; 393 char *opt_name, *other_opt_name; 394 395 for (; elem; elem = elem->next) { 396 intmax_t new_value; 397 398 if (do_get_int_value(elem->value_ptr, elem->precision, 399 &new_value)) 400 BUG("impossible: invalid precision"); 401 402 if (new_value == elem->value) 403 continue; 404 405 if (elem->opt && 406 (elem->opt->flags | opt->flags) & PARSE_OPT_CMDMODE) 407 break; 408 409 elem->opt = opt; 410 elem->arg = arg; 411 elem->flags = flags; 412 elem->value = new_value; 413 } 414 415 if (result || !elem) 416 return result; 417 418 opt_name = optnamearg(opt, arg, flags); 419 other_opt_name = optnamearg(elem->opt, elem->arg, elem->flags); 420 error(_("options '%s' and '%s' cannot be used together"), 421 opt_name, other_opt_name); 422 free(opt_name); 423 free(other_opt_name); 424 return -1; 425} 426 427static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p, 428 const struct option *options) 429{ 430 const struct option *numopt = NULL; 431 432 for (; options->type != OPTION_END; options++) { 433 if (options->short_name == *p->opt) { 434 p->opt = p->opt[1] ? p->opt + 1 : NULL; 435 return get_value(p, options, OPT_SHORT); 436 } 437 438 /* 439 * Handle the numerical option later, explicit one-digit 440 * options take precedence over it. 441 */ 442 if (options->type == OPTION_NUMBER) 443 numopt = options; 444 } 445 if (numopt && isdigit(*p->opt)) { 446 size_t len = 1; 447 char *arg; 448 int rc; 449 450 while (isdigit(p->opt[len])) 451 len++; 452 arg = xmemdupz(p->opt, len); 453 p->opt = p->opt[len] ? p->opt + len : NULL; 454 if (numopt->callback) 455 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; 456 else 457 rc = (*numopt->ll_callback)(p, numopt, arg, 0); 458 free(arg); 459 return rc; 460 } 461 return PARSE_OPT_UNKNOWN; 462} 463 464static int has_string(const char *it, const char **array) 465{ 466 while (*array) 467 if (!strcmp(it, *(array++))) 468 return 1; 469 return 0; 470} 471 472static int is_alias(struct parse_opt_ctx_t *ctx, 473 const struct option *one_opt, 474 const struct option *another_opt) 475{ 476 const char **group; 477 478 if (!ctx->alias_groups) 479 return 0; 480 481 if (!one_opt->long_name || !another_opt->long_name) 482 return 0; 483 484 for (group = ctx->alias_groups; *group; group += 3) { 485 /* it and other are from the same family? */ 486 if (has_string(one_opt->long_name, group) && 487 has_string(another_opt->long_name, group)) 488 return 1; 489 } 490 return 0; 491} 492 493struct parsed_option { 494 const struct option *option; 495 enum opt_parsed flags; 496}; 497 498static void register_abbrev(struct parse_opt_ctx_t *p, 499 const struct option *option, enum opt_parsed flags, 500 struct parsed_option *abbrev, 501 struct parsed_option *ambiguous) 502{ 503 if (p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) 504 return; 505 if (abbrev->option && 506 !(abbrev->flags == flags && is_alias(p, abbrev->option, option))) { 507 /* 508 * If this is abbreviated, it is 509 * ambiguous. So when there is no 510 * exact match later, we need to 511 * error out. 512 */ 513 ambiguous->option = abbrev->option; 514 ambiguous->flags = abbrev->flags; 515 } 516 abbrev->option = option; 517 abbrev->flags = flags; 518} 519 520static enum parse_opt_result parse_long_opt( 521 struct parse_opt_ctx_t *p, const char *arg, 522 const struct option *options) 523{ 524 const char *arg_end = strchrnul(arg, '='); 525 const char *arg_start = arg; 526 enum opt_parsed flags = OPT_LONG; 527 int arg_starts_with_no_no = 0; 528 struct parsed_option abbrev = { .option = NULL, .flags = OPT_LONG }; 529 struct parsed_option ambiguous = { .option = NULL, .flags = OPT_LONG }; 530 531 if (skip_prefix(arg_start, "no-", &arg_start)) { 532 if (skip_prefix(arg_start, "no-", &arg_start)) 533 arg_starts_with_no_no = 1; 534 else 535 flags |= OPT_UNSET; 536 } 537 538 for (; options->type != OPTION_END; options++) { 539 const char *rest, *long_name = options->long_name; 540 enum opt_parsed opt_flags = OPT_LONG; 541 int allow_unset = !(options->flags & PARSE_OPT_NONEG); 542 543 if (options->type == OPTION_SUBCOMMAND) 544 continue; 545 if (!long_name) 546 continue; 547 548 if (skip_prefix(long_name, "no-", &long_name)) 549 opt_flags |= OPT_UNSET; 550 else if (arg_starts_with_no_no) 551 continue; 552 553 if (((flags ^ opt_flags) & OPT_UNSET) && !allow_unset) 554 continue; 555 556 if (skip_prefix(arg_start, long_name, &rest)) { 557 if (*rest == '=') 558 p->opt = rest + 1; 559 else if (*rest) 560 continue; 561 return get_value(p, options, flags ^ opt_flags); 562 } 563 564 /* abbreviated? */ 565 if (!strncmp(long_name, arg_start, arg_end - arg_start)) 566 register_abbrev(p, options, flags ^ opt_flags, 567 &abbrev, &ambiguous); 568 569 /* negated and abbreviated very much? */ 570 if (allow_unset && starts_with("no-", arg)) 571 register_abbrev(p, options, OPT_UNSET ^ opt_flags, 572 &abbrev, &ambiguous); 573 } 574 575 if (disallow_abbreviated_options && (ambiguous.option || abbrev.option)) 576 die("disallowed abbreviated or ambiguous option '%.*s'", 577 (int)(arg_end - arg), arg); 578 579 if (ambiguous.option) { 580 error(_("ambiguous option: %s " 581 "(could be --%s%s or --%s%s)"), 582 arg, 583 (ambiguous.flags & OPT_UNSET) ? "no-" : "", 584 ambiguous.option->long_name, 585 (abbrev.flags & OPT_UNSET) ? "no-" : "", 586 abbrev.option->long_name); 587 return PARSE_OPT_HELP; 588 } 589 if (abbrev.option) { 590 if (*arg_end) 591 p->opt = arg_end + 1; 592 return get_value(p, abbrev.option, abbrev.flags); 593 } 594 return PARSE_OPT_UNKNOWN; 595} 596 597static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p, 598 const char *arg, 599 const struct option *options) 600{ 601 for (; options->type != OPTION_END; options++) { 602 if (!(options->flags & PARSE_OPT_NODASH)) 603 continue; 604 if (options->short_name == arg[0] && arg[1] == '\0') 605 return get_value(p, options, OPT_SHORT); 606 } 607 return PARSE_OPT_ERROR; 608} 609 610static enum parse_opt_result parse_subcommand(const char *arg, 611 const struct option *options) 612{ 613 for (; options->type != OPTION_END; options++) 614 if (options->type == OPTION_SUBCOMMAND && 615 !strcmp(options->long_name, arg)) { 616 *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn; 617 return PARSE_OPT_SUBCOMMAND; 618 } 619 620 return PARSE_OPT_UNKNOWN; 621} 622 623static void check_typos(const char *arg, const struct option *options) 624{ 625 if (strlen(arg) < 3) 626 return; 627 628 if (starts_with(arg, "no-")) { 629 error(_("did you mean `--%s` (with two dashes)?"), arg); 630 exit(129); 631 } 632 633 for (; options->type != OPTION_END; options++) { 634 if (!options->long_name) 635 continue; 636 if (starts_with(options->long_name, arg)) { 637 error(_("did you mean `--%s` (with two dashes)?"), arg); 638 exit(129); 639 } 640 } 641} 642 643static void parse_options_check(const struct option *opts) 644{ 645 char short_opts[128]; 646 void *subcommand_value = NULL; 647 648 memset(short_opts, '\0', sizeof(short_opts)); 649 for (; opts->type != OPTION_END; opts++) { 650 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && 651 (opts->flags & PARSE_OPT_OPTARG)) 652 optbug(opts, "uses incompatible flags " 653 "LASTARG_DEFAULT and OPTARG"); 654 if (opts->short_name) { 655 if (0x7F <= opts->short_name) 656 optbug(opts, "invalid short name"); 657 else if (short_opts[opts->short_name]++) 658 optbug(opts, "short name already used"); 659 } 660 if (opts->flags & PARSE_OPT_NODASH && 661 ((opts->flags & PARSE_OPT_OPTARG) || 662 !(opts->flags & PARSE_OPT_NOARG) || 663 !(opts->flags & PARSE_OPT_NONEG) || 664 opts->long_name)) 665 optbug(opts, "uses feature " 666 "not supported for dashless options"); 667 if (opts->type == OPTION_SET_INT && !opts->defval && 668 opts->long_name && !(opts->flags & PARSE_OPT_NONEG)) 669 optbug(opts, "OPTION_SET_INT 0 should not be negatable"); 670 switch (opts->type) { 671 case OPTION_SET_INT: 672 case OPTION_BIT: 673 case OPTION_NEGBIT: 674 case OPTION_BITOP: 675 case OPTION_COUNTUP: 676 if (!signed_int_fits(opts->defval, opts->precision)) 677 optbug(opts, "has invalid defval"); 678 /* fallthru */ 679 case OPTION_NUMBER: 680 if ((opts->flags & PARSE_OPT_OPTARG) || 681 !(opts->flags & PARSE_OPT_NOARG)) 682 optbug(opts, "should not accept an argument"); 683 break; 684 case OPTION_CALLBACK: 685 if (!opts->callback && !opts->ll_callback) 686 optbug(opts, "OPTION_CALLBACK needs one callback"); 687 else if (opts->callback && opts->ll_callback) 688 optbug(opts, "OPTION_CALLBACK can't have two callbacks"); 689 break; 690 case OPTION_LOWLEVEL_CALLBACK: 691 if (!opts->ll_callback) 692 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback"); 693 if (opts->callback) 694 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback"); 695 break; 696 case OPTION_ALIAS: 697 optbug(opts, "OPT_ALIAS() should not remain at this point. " 698 "Are you using parse_options_step() directly?\n" 699 "That case is not supported yet."); 700 break; 701 case OPTION_SUBCOMMAND: 702 if (!opts->value || !opts->subcommand_fn) 703 optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function"); 704 if (!subcommand_value) 705 subcommand_value = opts->value; 706 else if (subcommand_value != opts->value) 707 optbug(opts, "all OPTION_SUBCOMMANDs need the same value"); 708 break; 709 default: 710 ; /* ok. (usually accepts an argument) */ 711 } 712 if (opts->argh && 713 strcspn(opts->argh, " _") != strlen(opts->argh)) 714 optbug(opts, "multi-word argh should use dash to separate words"); 715 } 716 BUG_if_bug("invalid 'struct option'"); 717} 718 719static int has_subcommands(const struct option *options) 720{ 721 for (; options->type != OPTION_END; options++) 722 if (options->type == OPTION_SUBCOMMAND) 723 return 1; 724 return 0; 725} 726 727static void parse_options_start_1(struct parse_opt_ctx_t *ctx, 728 int argc, const char **argv, const char *prefix, 729 const struct option *options, 730 enum parse_opt_flags flags) 731{ 732 ctx->argc = argc; 733 ctx->argv = argv; 734 if (!(flags & PARSE_OPT_ONE_SHOT)) { 735 ctx->argc--; 736 ctx->argv++; 737 } 738 ctx->total = ctx->argc; 739 ctx->out = argv; 740 ctx->prefix = prefix; 741 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); 742 ctx->flags = flags; 743 ctx->has_subcommands = has_subcommands(options); 744 if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) 745 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands"); 746 if (ctx->has_subcommands) { 747 if (flags & PARSE_OPT_STOP_AT_NON_OPTION) 748 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION"); 749 if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) { 750 if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT) 751 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL"); 752 if (flags & PARSE_OPT_KEEP_DASHDASH) 753 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL"); 754 } 755 } 756 if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) && 757 (flags & PARSE_OPT_STOP_AT_NON_OPTION) && 758 !(flags & PARSE_OPT_ONE_SHOT)) 759 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); 760 if ((flags & PARSE_OPT_ONE_SHOT) && 761 (flags & PARSE_OPT_KEEP_ARGV0)) 762 BUG("Can't keep argv0 if you don't have it"); 763 parse_options_check(options); 764 build_cmdmode_list(ctx, options); 765} 766 767void parse_options_start(struct parse_opt_ctx_t *ctx, 768 int argc, const char **argv, const char *prefix, 769 const struct option *options, 770 enum parse_opt_flags flags) 771{ 772 memset(ctx, 0, sizeof(*ctx)); 773 parse_options_start_1(ctx, argc, argv, prefix, options, flags); 774} 775 776static void show_negated_gitcomp(const struct option *opts, int show_all, 777 int nr_noopts) 778{ 779 int printed_dashdash = 0; 780 781 for (; opts->type != OPTION_END; opts++) { 782 int has_unset_form = 0; 783 const char *name; 784 785 if (!opts->long_name) 786 continue; 787 if (!show_all && 788 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))) 789 continue; 790 if (opts->flags & PARSE_OPT_NONEG) 791 continue; 792 793 switch (opts->type) { 794 case OPTION_STRING: 795 case OPTION_FILENAME: 796 case OPTION_INTEGER: 797 case OPTION_UNSIGNED: 798 case OPTION_CALLBACK: 799 case OPTION_BIT: 800 case OPTION_NEGBIT: 801 case OPTION_COUNTUP: 802 case OPTION_SET_INT: 803 has_unset_form = 1; 804 break; 805 default: 806 break; 807 } 808 if (!has_unset_form) 809 continue; 810 811 if (skip_prefix(opts->long_name, "no-", &name)) { 812 if (nr_noopts < 0) 813 printf(" --%s", name); 814 } else if (nr_noopts >= 0) { 815 if (nr_noopts && !printed_dashdash) { 816 printf(" --"); 817 printed_dashdash = 1; 818 } 819 printf(" --no-%s", opts->long_name); 820 nr_noopts++; 821 } 822 } 823} 824 825static int show_gitcomp(const struct option *opts, int show_all) 826{ 827 const struct option *original_opts = opts; 828 int nr_noopts = 0; 829 830 for (; opts->type != OPTION_END; opts++) { 831 const char *prefix = "--"; 832 const char *suffix = ""; 833 834 if (!opts->long_name) 835 continue; 836 if (!show_all && 837 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS))) 838 continue; 839 840 switch (opts->type) { 841 case OPTION_SUBCOMMAND: 842 prefix = ""; 843 break; 844 case OPTION_GROUP: 845 continue; 846 case OPTION_STRING: 847 case OPTION_FILENAME: 848 case OPTION_INTEGER: 849 case OPTION_UNSIGNED: 850 case OPTION_CALLBACK: 851 if (opts->flags & PARSE_OPT_NOARG) 852 break; 853 if (opts->flags & PARSE_OPT_OPTARG) 854 break; 855 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT) 856 break; 857 suffix = "="; 858 break; 859 default: 860 break; 861 } 862 if (opts->flags & PARSE_OPT_COMP_ARG) 863 suffix = "="; 864 if (starts_with(opts->long_name, "no-")) 865 nr_noopts++; 866 printf("%s%s%s%s", opts == original_opts ? "" : " ", 867 prefix, opts->long_name, suffix); 868 } 869 show_negated_gitcomp(original_opts, show_all, -1); 870 show_negated_gitcomp(original_opts, show_all, nr_noopts); 871 fputc('\n', stdout); 872 return PARSE_OPT_COMPLETE; 873} 874 875/* 876 * Scan and may produce a new option[] array, which should be used 877 * instead of the original 'options'. 878 * 879 * Right now this is only used to preprocess and substitute 880 * OPTION_ALIAS. 881 * 882 * The returned options should be freed using free_preprocessed_options. 883 */ 884static struct option *preprocess_options(struct parse_opt_ctx_t *ctx, 885 const struct option *options) 886{ 887 struct option *newopt; 888 int i, nr, alias; 889 int nr_aliases = 0; 890 891 for (nr = 0; options[nr].type != OPTION_END; nr++) { 892 if (options[nr].type == OPTION_ALIAS) 893 nr_aliases++; 894 } 895 896 if (!nr_aliases) 897 return NULL; 898 899 DUP_ARRAY(newopt, options, nr + 1); 900 901 /* each alias has two string pointers and NULL */ 902 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); 903 904 for (alias = 0, i = 0; i < nr; i++) { 905 int short_name; 906 const char *long_name; 907 const char *source; 908 struct strbuf help = STRBUF_INIT; 909 int j; 910 911 if (newopt[i].type != OPTION_ALIAS) 912 continue; 913 914 short_name = newopt[i].short_name; 915 long_name = newopt[i].long_name; 916 source = newopt[i].value; 917 918 if (!long_name) 919 BUG("An alias must have long option name"); 920 strbuf_addf(&help, _("alias of --%s"), source); 921 922 for (j = 0; j < nr; j++) { 923 const char *name = options[j].long_name; 924 925 if (!name || strcmp(name, source)) 926 continue; 927 928 if (options[j].type == OPTION_ALIAS) 929 BUG("No please. Nested aliases are not supported."); 930 931 memcpy(newopt + i, options + j, sizeof(*newopt)); 932 newopt[i].short_name = short_name; 933 newopt[i].long_name = long_name; 934 newopt[i].help = strbuf_detach(&help, NULL); 935 newopt[i].flags |= PARSE_OPT_FROM_ALIAS; 936 break; 937 } 938 939 if (j == nr) 940 BUG("could not find source option '%s' of alias '%s'", 941 source, newopt[i].long_name); 942 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name; 943 ctx->alias_groups[alias * 3 + 1] = options[j].long_name; 944 ctx->alias_groups[alias * 3 + 2] = NULL; 945 alias++; 946 } 947 948 return newopt; 949} 950 951static void free_preprocessed_options(struct option *options) 952{ 953 int i; 954 955 if (!options) 956 return; 957 958 for (i = 0; options[i].type != OPTION_END; i++) { 959 if (options[i].flags & PARSE_OPT_FROM_ALIAS) 960 free((void *)options[i].help); 961 } 962 free(options); 963} 964 965#define USAGE_NORMAL 0 966#define USAGE_FULL 1 967#define USAGE_TO_STDOUT 0 968#define USAGE_TO_STDERR 1 969 970static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *, 971 const char * const *, 972 const struct option *, 973 int full_usage, 974 int usage_to_stderr); 975 976enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, 977 const struct option *options, 978 const char * const usagestr[]) 979{ 980 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); 981 982 /* we must reset ->opt, unknown short option leave it dangling */ 983 ctx->opt = NULL; 984 985 for (; ctx->argc; ctx->argc--, ctx->argv++) { 986 const char *arg = ctx->argv[0]; 987 988 if (ctx->flags & PARSE_OPT_ONE_SHOT && 989 ctx->argc != ctx->total) 990 break; 991 992 if (*arg != '-' || !arg[1]) { 993 if (parse_nodash_opt(ctx, arg, options) == 0) 994 continue; 995 if (!ctx->has_subcommands) { 996 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) 997 return PARSE_OPT_NON_OPTION; 998 ctx->out[ctx->cpidx++] = ctx->argv[0]; 999 continue; 1000 } 1001 switch (parse_subcommand(arg, options)) { 1002 case PARSE_OPT_SUBCOMMAND: 1003 return PARSE_OPT_SUBCOMMAND; 1004 case PARSE_OPT_UNKNOWN: 1005 if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) 1006 /* 1007 * arg is neither a short or long 1008 * option nor a subcommand. Since 1009 * this command has a default 1010 * operation mode, we have to treat 1011 * this arg and all remaining args 1012 * as args meant to that default 1013 * operation mode. 1014 * So we are done parsing. 1015 */ 1016 return PARSE_OPT_DONE; 1017 error(_("unknown subcommand: `%s'"), arg); 1018 usage_with_options(usagestr, options); 1019 case PARSE_OPT_COMPLETE: 1020 case PARSE_OPT_HELP: 1021 case PARSE_OPT_ERROR: 1022 case PARSE_OPT_DONE: 1023 case PARSE_OPT_NON_OPTION: 1024 /* Impossible. */ 1025 BUG("parse_subcommand() cannot return these"); 1026 } 1027 } 1028 1029 /* lone -h asks for help */ 1030 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h")) 1031 goto show_usage; 1032 1033 /* 1034 * lone --git-completion-helper and --git-completion-helper-all 1035 * are asked by git-completion.bash 1036 */ 1037 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper")) 1038 return show_gitcomp(options, 0); 1039 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all")) 1040 return show_gitcomp(options, 1); 1041 1042 if (arg[1] != '-') { 1043 ctx->opt = arg + 1; 1044 switch (parse_short_opt(ctx, options)) { 1045 case PARSE_OPT_ERROR: 1046 return PARSE_OPT_ERROR; 1047 case PARSE_OPT_UNKNOWN: 1048 if (ctx->opt) 1049 check_typos(arg + 1, options); 1050 if (internal_help && *ctx->opt == 'h') 1051 goto show_usage; 1052 goto unknown; 1053 case PARSE_OPT_NON_OPTION: 1054 case PARSE_OPT_SUBCOMMAND: 1055 case PARSE_OPT_HELP: 1056 case PARSE_OPT_COMPLETE: 1057 BUG("parse_short_opt() cannot return these"); 1058 case PARSE_OPT_DONE: 1059 break; 1060 } 1061 if (ctx->opt) 1062 check_typos(arg + 1, options); 1063 while (ctx->opt) { 1064 switch (parse_short_opt(ctx, options)) { 1065 case PARSE_OPT_ERROR: 1066 return PARSE_OPT_ERROR; 1067 case PARSE_OPT_UNKNOWN: 1068 if (internal_help && *ctx->opt == 'h') 1069 goto show_usage; 1070 1071 /* fake a short option thing to hide the fact that we may have 1072 * started to parse aggregated stuff 1073 * 1074 * This is leaky, too bad. 1075 */ 1076 ctx->argv[0] = xstrdup(ctx->opt - 1); 1077 *(char *)ctx->argv[0] = '-'; 1078 goto unknown; 1079 case PARSE_OPT_NON_OPTION: 1080 case PARSE_OPT_SUBCOMMAND: 1081 case PARSE_OPT_COMPLETE: 1082 case PARSE_OPT_HELP: 1083 BUG("parse_short_opt() cannot return these"); 1084 case PARSE_OPT_DONE: 1085 break; 1086 } 1087 } 1088 continue; 1089 } 1090 1091 if (!arg[2] /* "--" */) { 1092 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { 1093 ctx->argc--; 1094 ctx->argv++; 1095 } 1096 break; 1097 } else if (!strcmp(arg + 2, "end-of-options")) { 1098 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) { 1099 ctx->argc--; 1100 ctx->argv++; 1101 } 1102 break; 1103 } 1104 1105 if (internal_help && !strcmp(arg + 2, "help-all")) 1106 return usage_with_options_internal(ctx, usagestr, options, 1107 USAGE_FULL, USAGE_TO_STDOUT); 1108 if (internal_help && !strcmp(arg + 2, "help")) 1109 goto show_usage; 1110 switch (parse_long_opt(ctx, arg + 2, options)) { 1111 case PARSE_OPT_ERROR: 1112 return PARSE_OPT_ERROR; 1113 case PARSE_OPT_UNKNOWN: 1114 goto unknown; 1115 case PARSE_OPT_HELP: 1116 goto show_usage; 1117 case PARSE_OPT_NON_OPTION: 1118 case PARSE_OPT_SUBCOMMAND: 1119 case PARSE_OPT_COMPLETE: 1120 BUG("parse_long_opt() cannot return these"); 1121 case PARSE_OPT_DONE: 1122 break; 1123 } 1124 continue; 1125unknown: 1126 if (ctx->flags & PARSE_OPT_ONE_SHOT) 1127 break; 1128 if (ctx->has_subcommands && 1129 (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) && 1130 (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) { 1131 /* 1132 * Found an unknown option given to a command with 1133 * subcommands that has a default operation mode: 1134 * we treat this option and all remaining args as 1135 * arguments meant to that default operation mode. 1136 * So we are done parsing. 1137 */ 1138 return PARSE_OPT_DONE; 1139 } 1140 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) 1141 return PARSE_OPT_UNKNOWN; 1142 ctx->out[ctx->cpidx++] = ctx->argv[0]; 1143 ctx->opt = NULL; 1144 } 1145 return PARSE_OPT_DONE; 1146 1147 show_usage: 1148 return usage_with_options_internal(ctx, usagestr, options, 1149 USAGE_NORMAL, USAGE_TO_STDOUT); 1150} 1151 1152int parse_options_end(struct parse_opt_ctx_t *ctx) 1153{ 1154 if (ctx->flags & PARSE_OPT_ONE_SHOT) 1155 return ctx->total - ctx->argc; 1156 1157 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc); 1158 ctx->out[ctx->cpidx + ctx->argc] = NULL; 1159 return ctx->cpidx + ctx->argc; 1160} 1161 1162int parse_options(int argc, const char **argv, 1163 const char *prefix, 1164 const struct option *options, 1165 const char * const usagestr[], 1166 enum parse_opt_flags flags) 1167{ 1168 struct parse_opt_ctx_t ctx; 1169 struct option *real_options; 1170 1171 disallow_abbreviated_options = 1172 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0); 1173 1174 memset(&ctx, 0, sizeof(ctx)); 1175 real_options = preprocess_options(&ctx, options); 1176 if (real_options) 1177 options = real_options; 1178 parse_options_start_1(&ctx, argc, argv, prefix, options, flags); 1179 switch (parse_options_step(&ctx, options, usagestr)) { 1180 case PARSE_OPT_HELP: 1181 case PARSE_OPT_ERROR: 1182 exit(129); 1183 case PARSE_OPT_COMPLETE: 1184 exit(0); 1185 case PARSE_OPT_NON_OPTION: 1186 case PARSE_OPT_SUBCOMMAND: 1187 break; 1188 case PARSE_OPT_DONE: 1189 if (ctx.has_subcommands && 1190 !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) { 1191 error(_("need a subcommand")); 1192 usage_with_options(usagestr, options); 1193 } 1194 break; 1195 case PARSE_OPT_UNKNOWN: 1196 if (ctx.argv[0][1] == '-') { 1197 error(_("unknown option `%s'"), ctx.argv[0] + 2); 1198 } else if (isascii(*ctx.opt)) { 1199 error(_("unknown switch `%c'"), *ctx.opt); 1200 } else { 1201 error(_("unknown non-ascii option in string: `%s'"), 1202 ctx.argv[0]); 1203 } 1204 usage_with_options(usagestr, options); 1205 } 1206 1207 precompose_argv_prefix(argc, argv, NULL); 1208 free_preprocessed_options(real_options); 1209 free(ctx.alias_groups); 1210 for (struct parse_opt_cmdmode_list *elem = ctx.cmdmode_list; elem;) { 1211 struct parse_opt_cmdmode_list *next = elem->next; 1212 free(elem); 1213 elem = next; 1214 } 1215 return parse_options_end(&ctx); 1216} 1217 1218static int usage_argh(const struct option *opts, FILE *outfile) 1219{ 1220 const char *s; 1221 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || 1222 !opts->argh || !!strpbrk(opts->argh, "()<>[]|"); 1223 if (opts->flags & PARSE_OPT_OPTARG) 1224 if (opts->long_name) 1225 /* 1226 * TRANSLATORS: The "<%s>" part of this string 1227 * stands for an optional value given to a command 1228 * line option in the long form, and "<>" is there 1229 * as a convention to signal that it is a 1230 * placeholder (i.e. the user should substitute it 1231 * with the real value). If your language uses a 1232 * different convention, you can change "<%s>" part 1233 * to match yours, e.g. it might use "|%s|" instead, 1234 * or if the alphabet is different enough it may use 1235 * "%s" without any placeholder signal. Most 1236 * translations leave this message as is. 1237 */ 1238 s = literal ? "[=%s]" : _("[=<%s>]"); 1239 else 1240 /* 1241 * TRANSLATORS: The "<%s>" part of this string 1242 * stands for an optional value given to a command 1243 * line option in the short form, and "<>" is there 1244 * as a convention to signal that it is a 1245 * placeholder (i.e. the user should substitute it 1246 * with the real value). If your language uses a 1247 * different convention, you can change "<%s>" part 1248 * to match yours, e.g. it might use "|%s|" instead, 1249 * or if the alphabet is different enough it may use 1250 * "%s" without any placeholder signal. Most 1251 * translations leave this message as is. 1252 */ 1253 s = literal ? "[%s]" : _("[<%s>]"); 1254 else 1255 /* 1256 * TRANSLATORS: The "<%s>" part of this string stands for a 1257 * value given to a command line option, and "<>" is there 1258 * as a convention to signal that it is a placeholder 1259 * (i.e. the user should substitute it with the real value). 1260 * If your language uses a different convention, you can 1261 * change "<%s>" part to match yours, e.g. it might use 1262 * "|%s|" instead, or if the alphabet is different enough it 1263 * may use "%s" without any placeholder signal. Most 1264 * translations leave this message as is. 1265 */ 1266 s = literal ? " %s" : _(" <%s>"); 1267 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); 1268} 1269 1270static int usage_indent(FILE *outfile) 1271{ 1272 return fprintf(outfile, " "); 1273} 1274 1275#define USAGE_OPTS_WIDTH 26 1276 1277static void usage_padding(FILE *outfile, size_t pos) 1278{ 1279 if (pos < USAGE_OPTS_WIDTH) 1280 fprintf(outfile, "%*s", USAGE_OPTS_WIDTH - (int)pos, ""); 1281 else 1282 fprintf(outfile, "\n%*s", USAGE_OPTS_WIDTH, ""); 1283} 1284 1285static const struct option *find_option_by_long_name(const struct option *opts, 1286 const char *long_name) 1287{ 1288 for (; opts->type != OPTION_END; opts++) { 1289 if (opts->long_name && !strcmp(opts->long_name, long_name)) 1290 return opts; 1291 } 1292 return NULL; 1293} 1294 1295static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx, 1296 const char * const *usagestr, 1297 const struct option *opts, 1298 int full, int err) 1299{ 1300 const struct option *all_opts = opts; 1301 FILE *outfile = err ? stderr : stdout; 1302 int need_newline; 1303 1304 const char *usage_prefix = _("usage: %s"); 1305 /* 1306 * The translation could be anything, but we can count on 1307 * msgfmt(1)'s --check option to have asserted that "%s" is in 1308 * the translation. So compute the length of the "usage: " 1309 * part. We are assuming that the translator wasn't overly 1310 * clever and used e.g. "%1$s" instead of "%s", there's only 1311 * one "%s" in "usage_prefix" above, so there's no reason to 1312 * do so even with a RTL language. 1313 */ 1314 size_t usage_len = strlen(usage_prefix) - strlen("%s"); 1315 /* 1316 * TRANSLATORS: the colon here should align with the 1317 * one in "usage: %s" translation. 1318 */ 1319 const char *or_prefix = _(" or: %s"); 1320 /* 1321 * TRANSLATORS: You should only need to translate this format 1322 * string if your language is a RTL language (e.g. Arabic, 1323 * Hebrew etc.), not if it's a LTR language (e.g. German, 1324 * Russian, Chinese etc.). 1325 * 1326 * When a translated usage string has an embedded "\n" it's 1327 * because options have wrapped to the next line. The line 1328 * after the "\n" will then be padded to align with the 1329 * command name, such as N_("git cmd [opt]\n<8 1330 * spaces>[opt2]"), where the 8 spaces are the same length as 1331 * "git cmd ". 1332 * 1333 * This format string prints out that already-translated 1334 * line. The "%*s" is whitespace padding to account for the 1335 * padding at the start of the line that we add in this 1336 * function. The "%s" is a line in the (hopefully already 1337 * translated) N_() usage string, which contained embedded 1338 * newlines before we split it up. 1339 */ 1340 const char *usage_continued = _("%*s%s"); 1341 const char *prefix = usage_prefix; 1342 int saw_empty_line = 0; 1343 1344 if (!usagestr) 1345 return PARSE_OPT_HELP; 1346 1347 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) 1348 fprintf(outfile, "cat <<\\EOF\n"); 1349 1350 while (*usagestr) { 1351 const char *str = _(*usagestr++); 1352 struct string_list list = STRING_LIST_INIT_DUP; 1353 unsigned int j; 1354 1355 if (!saw_empty_line && !*str) 1356 saw_empty_line = 1; 1357 1358 string_list_split(&list, str, "\n", -1); 1359 for (j = 0; j < list.nr; j++) { 1360 const char *line = list.items[j].string; 1361 1362 if (saw_empty_line && *line) 1363 fprintf_ln(outfile, _(" %s"), line); 1364 else if (saw_empty_line) 1365 fputc('\n', outfile); 1366 else if (!j) 1367 fprintf_ln(outfile, prefix, line); 1368 else 1369 fprintf_ln(outfile, usage_continued, 1370 (int)usage_len, "", line); 1371 } 1372 string_list_clear(&list, 0); 1373 1374 prefix = or_prefix; 1375 } 1376 1377 need_newline = 1; 1378 1379 for (; opts->type != OPTION_END; opts++) { 1380 size_t pos; 1381 const char *cp, *np; 1382 const char *positive_name = NULL; 1383 1384 if (opts->type == OPTION_SUBCOMMAND) 1385 continue; 1386 if (opts->type == OPTION_GROUP) { 1387 fputc('\n', outfile); 1388 need_newline = 0; 1389 if (*opts->help) 1390 fprintf(outfile, "%s\n", _(opts->help)); 1391 continue; 1392 } 1393 if (!full && (opts->flags & PARSE_OPT_HIDDEN)) 1394 continue; 1395 1396 if (need_newline) { 1397 fputc('\n', outfile); 1398 need_newline = 0; 1399 } 1400 1401 pos = usage_indent(outfile); 1402 if (opts->short_name) { 1403 if (opts->flags & PARSE_OPT_NODASH) 1404 pos += fprintf(outfile, "%c", opts->short_name); 1405 else 1406 pos += fprintf(outfile, "-%c", opts->short_name); 1407 } 1408 if (opts->long_name && opts->short_name) 1409 pos += fprintf(outfile, ", "); 1410 if (opts->long_name) { 1411 const char *long_name = opts->long_name; 1412 if ((opts->flags & PARSE_OPT_NONEG) || 1413 skip_prefix(long_name, "no-", &positive_name)) 1414 pos += fprintf(outfile, "--%s", long_name); 1415 else 1416 pos += fprintf(outfile, "--[no-]%s", long_name); 1417 } 1418 1419 if (opts->type == OPTION_NUMBER) 1420 pos += utf8_fprintf(outfile, _("-NUM")); 1421 1422 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || 1423 !(opts->flags & PARSE_OPT_NOARG)) 1424 pos += usage_argh(opts, outfile); 1425 1426 if (opts->type == OPTION_ALIAS) { 1427 usage_padding(outfile, pos); 1428 fprintf_ln(outfile, _("alias of --%s"), 1429 (const char *)opts->value); 1430 continue; 1431 } 1432 1433 for (cp = opts->help ? _(opts->help) : ""; *cp; cp = np) { 1434 np = strchrnul(cp, '\n'); 1435 if (*np) 1436 np++; 1437 usage_padding(outfile, pos); 1438 fwrite(cp, 1, np - cp, outfile); 1439 pos = 0; 1440 } 1441 fputc('\n', outfile); 1442 1443 if (positive_name) { 1444 if (find_option_by_long_name(all_opts, positive_name)) 1445 continue; 1446 pos = usage_indent(outfile); 1447 pos += fprintf(outfile, "--%s", positive_name); 1448 usage_padding(outfile, pos); 1449 fprintf_ln(outfile, _("opposite of --no-%s"), 1450 positive_name); 1451 } 1452 } 1453 fputc('\n', outfile); 1454 1455 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) 1456 fputs("EOF\n", outfile); 1457 1458 return PARSE_OPT_HELP; 1459} 1460 1461void NORETURN usage_with_options(const char * const *usagestr, 1462 const struct option *opts) 1463{ 1464 usage_with_options_internal(NULL, usagestr, opts, 1465 USAGE_NORMAL, USAGE_TO_STDERR); 1466 exit(129); 1467} 1468 1469void show_usage_with_options_if_asked(int ac, const char **av, 1470 const char * const *usagestr, 1471 const struct option *opts) 1472{ 1473 if (ac == 2) { 1474 if (!strcmp(av[1], "-h")) { 1475 usage_with_options_internal(NULL, usagestr, opts, 1476 USAGE_NORMAL, USAGE_TO_STDOUT); 1477 exit(129); 1478 } else if (!strcmp(av[1], "--help-all")) { 1479 usage_with_options_internal(NULL, usagestr, opts, 1480 USAGE_FULL, USAGE_TO_STDOUT); 1481 exit(129); 1482 } 1483 } 1484} 1485 1486void NORETURN usage_msg_opt(const char *msg, 1487 const char * const *usagestr, 1488 const struct option *options) 1489{ 1490 die_message("%s\n", msg); /* The extra \n is intentional */ 1491 usage_with_options(usagestr, options); 1492} 1493 1494void NORETURN usage_msg_optf(const char * const fmt, 1495 const char * const *usagestr, 1496 const struct option *options, ...) 1497{ 1498 struct strbuf msg = STRBUF_INIT; 1499 va_list ap; 1500 va_start(ap, options); 1501 strbuf_vaddf(&msg, fmt, ap); 1502 va_end(ap); 1503 1504 usage_msg_opt(msg.buf, usagestr, options); 1505} 1506 1507void die_for_incompatible_opt4(int opt1, const char *opt1_name, 1508 int opt2, const char *opt2_name, 1509 int opt3, const char *opt3_name, 1510 int opt4, const char *opt4_name) 1511{ 1512 int count = 0; 1513 const char *options[4]; 1514 1515 if (opt1) 1516 options[count++] = opt1_name; 1517 if (opt2) 1518 options[count++] = opt2_name; 1519 if (opt3) 1520 options[count++] = opt3_name; 1521 if (opt4) 1522 options[count++] = opt4_name; 1523 switch (count) { 1524 case 4: 1525 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"), 1526 opt1_name, opt2_name, opt3_name, opt4_name); 1527 break; 1528 case 3: 1529 die(_("options '%s', '%s', and '%s' cannot be used together"), 1530 options[0], options[1], options[2]); 1531 break; 1532 case 2: 1533 die(_("options '%s' and '%s' cannot be used together"), 1534 options[0], options[1]); 1535 break; 1536 default: 1537 break; 1538 } 1539}