| 427 | } |
| 428 | |
| 429 | static int cmd_parseopt(int argc, const char **argv, const char *prefix) |
| 430 | { |
| 431 | int keep_dashdash = 0, stop_at_non_option = 0; |
| 432 | char const * const parseopt_usage[] = { |
| 433 | N_("git rev-parse --parseopt [<options>] -- [<args>...]"), |
| 434 | NULL |
| 435 | }; |
| 436 | struct option parseopt_opts[] = { |
| 437 | OPT_BOOL(0, "keep-dashdash", &keep_dashdash, |
| 438 | N_("keep the `--` passed as an arg")), |
| 439 | OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option, |
| 440 | N_("stop parsing after the " |
| 441 | "first non-option argument")), |
| 442 | OPT_BOOL(0, "stuck-long", &stuck_long, |
| 443 | N_("output in stuck long form")), |
| 444 | OPT_END(), |
| 445 | }; |
| 446 | struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; |
| 447 | struct strvec longnames = STRVEC_INIT; |
| 448 | struct strvec usage = STRVEC_INIT; |
| 449 | struct option *opts = NULL; |
| 450 | size_t opts_nr = 0, opts_alloc = 0; |
| 451 | |
| 452 | strbuf_addstr(&parsed, "set --"); |
| 453 | argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, |
| 454 | PARSE_OPT_KEEP_DASHDASH); |
| 455 | if (argc < 1 || strcmp(argv[0], "--")) |
| 456 | usage_with_options(parseopt_usage, parseopt_opts); |
| 457 | |
| 458 | /* get the usage up to the first line with a -- on it */ |
| 459 | for (;;) { |
| 460 | strbuf_reset(&sb); |
| 461 | if (strbuf_getline(&sb, stdin) == EOF) |
| 462 | die(_("premature end of input")); |
| 463 | if (!strcmp("--", sb.buf)) { |
| 464 | if (!usage.nr) |
| 465 | die(_("no usage string given before the `--' separator")); |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | strvec_push(&usage, sb.buf); |
| 470 | } |
| 471 | |
| 472 | /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */ |
| 473 | while (strbuf_getline(&sb, stdin) != EOF) { |
| 474 | const char *s; |
| 475 | char *help; |
| 476 | struct option *o; |
| 477 | |
| 478 | if (!sb.len) |
| 479 | continue; |
| 480 | |
| 481 | ALLOC_GROW(opts, opts_nr + 1, opts_alloc); |
| 482 | memset(opts + opts_nr, 0, sizeof(*opts)); |
| 483 | |
| 484 | o = &opts[opts_nr++]; |
| 485 | help = findspace(sb.buf); |
| 486 | if (!help || sb.buf == help) { |
no test coverage detected