parse-options.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #include "util.h"
  2. #include "parse-options.h"
  3. #include "cache.h"
  4. #include "header.h"
  5. #define OPT_SHORT 1
  6. #define OPT_UNSET 2
  7. static int opterror(const struct option *opt, const char *reason, int flags)
  8. {
  9. if (flags & OPT_SHORT)
  10. return error("switch `%c' %s", opt->short_name, reason);
  11. if (flags & OPT_UNSET)
  12. return error("option `no-%s' %s", opt->long_name, reason);
  13. return error("option `%s' %s", opt->long_name, reason);
  14. }
  15. static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  16. int flags, const char **arg)
  17. {
  18. if (p->opt) {
  19. *arg = p->opt;
  20. p->opt = NULL;
  21. } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
  22. **(p->argv + 1) == '-')) {
  23. *arg = (const char *)opt->defval;
  24. } else if (p->argc > 1) {
  25. p->argc--;
  26. *arg = *++p->argv;
  27. } else
  28. return opterror(opt, "requires a value", flags);
  29. return 0;
  30. }
  31. static int get_value(struct parse_opt_ctx_t *p,
  32. const struct option *opt, int flags)
  33. {
  34. const char *s, *arg = NULL;
  35. const int unset = flags & OPT_UNSET;
  36. int err;
  37. if (unset && p->opt)
  38. return opterror(opt, "takes no value", flags);
  39. if (unset && (opt->flags & PARSE_OPT_NONEG))
  40. return opterror(opt, "isn't available", flags);
  41. if (opt->flags & PARSE_OPT_DISABLED)
  42. return opterror(opt, "is not usable", flags);
  43. if (opt->flags & PARSE_OPT_EXCLUSIVE) {
  44. if (p->excl_opt && p->excl_opt != opt) {
  45. char msg[128];
  46. if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
  47. p->excl_opt->long_name == NULL) {
  48. scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
  49. p->excl_opt->short_name);
  50. } else {
  51. scnprintf(msg, sizeof(msg), "cannot be used with %s",
  52. p->excl_opt->long_name);
  53. }
  54. opterror(opt, msg, flags);
  55. return -3;
  56. }
  57. p->excl_opt = opt;
  58. }
  59. if (!(flags & OPT_SHORT) && p->opt) {
  60. switch (opt->type) {
  61. case OPTION_CALLBACK:
  62. if (!(opt->flags & PARSE_OPT_NOARG))
  63. break;
  64. /* FALLTHROUGH */
  65. case OPTION_BOOLEAN:
  66. case OPTION_INCR:
  67. case OPTION_BIT:
  68. case OPTION_SET_UINT:
  69. case OPTION_SET_PTR:
  70. return opterror(opt, "takes no value", flags);
  71. case OPTION_END:
  72. case OPTION_ARGUMENT:
  73. case OPTION_GROUP:
  74. case OPTION_STRING:
  75. case OPTION_INTEGER:
  76. case OPTION_UINTEGER:
  77. case OPTION_LONG:
  78. case OPTION_U64:
  79. default:
  80. break;
  81. }
  82. }
  83. switch (opt->type) {
  84. case OPTION_BIT:
  85. if (unset)
  86. *(int *)opt->value &= ~opt->defval;
  87. else
  88. *(int *)opt->value |= opt->defval;
  89. return 0;
  90. case OPTION_BOOLEAN:
  91. *(bool *)opt->value = unset ? false : true;
  92. if (opt->set)
  93. *(bool *)opt->set = true;
  94. return 0;
  95. case OPTION_INCR:
  96. *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  97. return 0;
  98. case OPTION_SET_UINT:
  99. *(unsigned int *)opt->value = unset ? 0 : opt->defval;
  100. return 0;
  101. case OPTION_SET_PTR:
  102. *(void **)opt->value = unset ? NULL : (void *)opt->defval;
  103. return 0;
  104. case OPTION_STRING:
  105. err = 0;
  106. if (unset)
  107. *(const char **)opt->value = NULL;
  108. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  109. *(const char **)opt->value = (const char *)opt->defval;
  110. else
  111. err = get_arg(p, opt, flags, (const char **)opt->value);
  112. /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
  113. if (opt->flags & PARSE_OPT_NOEMPTY) {
  114. const char *val = *(const char **)opt->value;
  115. if (!val)
  116. return err;
  117. /* Similar to unset if we are given an empty string. */
  118. if (val[0] == '\0') {
  119. *(const char **)opt->value = NULL;
  120. return 0;
  121. }
  122. }
  123. return err;
  124. case OPTION_CALLBACK:
  125. if (unset)
  126. return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
  127. if (opt->flags & PARSE_OPT_NOARG)
  128. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  129. if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  130. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  131. if (get_arg(p, opt, flags, &arg))
  132. return -1;
  133. return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
  134. case OPTION_INTEGER:
  135. if (unset) {
  136. *(int *)opt->value = 0;
  137. return 0;
  138. }
  139. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  140. *(int *)opt->value = opt->defval;
  141. return 0;
  142. }
  143. if (get_arg(p, opt, flags, &arg))
  144. return -1;
  145. *(int *)opt->value = strtol(arg, (char **)&s, 10);
  146. if (*s)
  147. return opterror(opt, "expects a numerical value", flags);
  148. return 0;
  149. case OPTION_UINTEGER:
  150. if (unset) {
  151. *(unsigned int *)opt->value = 0;
  152. return 0;
  153. }
  154. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  155. *(unsigned int *)opt->value = opt->defval;
  156. return 0;
  157. }
  158. if (get_arg(p, opt, flags, &arg))
  159. return -1;
  160. *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
  161. if (*s)
  162. return opterror(opt, "expects a numerical value", flags);
  163. return 0;
  164. case OPTION_LONG:
  165. if (unset) {
  166. *(long *)opt->value = 0;
  167. return 0;
  168. }
  169. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  170. *(long *)opt->value = opt->defval;
  171. return 0;
  172. }
  173. if (get_arg(p, opt, flags, &arg))
  174. return -1;
  175. *(long *)opt->value = strtol(arg, (char **)&s, 10);
  176. if (*s)
  177. return opterror(opt, "expects a numerical value", flags);
  178. return 0;
  179. case OPTION_U64:
  180. if (unset) {
  181. *(u64 *)opt->value = 0;
  182. return 0;
  183. }
  184. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  185. *(u64 *)opt->value = opt->defval;
  186. return 0;
  187. }
  188. if (get_arg(p, opt, flags, &arg))
  189. return -1;
  190. *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
  191. if (*s)
  192. return opterror(opt, "expects a numerical value", flags);
  193. return 0;
  194. case OPTION_END:
  195. case OPTION_ARGUMENT:
  196. case OPTION_GROUP:
  197. default:
  198. die("should not happen, someone must be hit on the forehead");
  199. }
  200. }
  201. static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
  202. {
  203. for (; options->type != OPTION_END; options++) {
  204. if (options->short_name == *p->opt) {
  205. p->opt = p->opt[1] ? p->opt + 1 : NULL;
  206. return get_value(p, options, OPT_SHORT);
  207. }
  208. }
  209. return -2;
  210. }
  211. static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
  212. const struct option *options)
  213. {
  214. const char *arg_end = strchr(arg, '=');
  215. const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
  216. int abbrev_flags = 0, ambiguous_flags = 0;
  217. if (!arg_end)
  218. arg_end = arg + strlen(arg);
  219. for (; options->type != OPTION_END; options++) {
  220. const char *rest;
  221. int flags = 0;
  222. if (!options->long_name)
  223. continue;
  224. rest = skip_prefix(arg, options->long_name);
  225. if (options->type == OPTION_ARGUMENT) {
  226. if (!rest)
  227. continue;
  228. if (*rest == '=')
  229. return opterror(options, "takes no value", flags);
  230. if (*rest)
  231. continue;
  232. p->out[p->cpidx++] = arg - 2;
  233. return 0;
  234. }
  235. if (!rest) {
  236. if (!prefixcmp(options->long_name, "no-")) {
  237. /*
  238. * The long name itself starts with "no-", so
  239. * accept the option without "no-" so that users
  240. * do not have to enter "no-no-" to get the
  241. * negation.
  242. */
  243. rest = skip_prefix(arg, options->long_name + 3);
  244. if (rest) {
  245. flags |= OPT_UNSET;
  246. goto match;
  247. }
  248. /* Abbreviated case */
  249. if (!prefixcmp(options->long_name + 3, arg)) {
  250. flags |= OPT_UNSET;
  251. goto is_abbreviated;
  252. }
  253. }
  254. /* abbreviated? */
  255. if (!strncmp(options->long_name, arg, arg_end - arg)) {
  256. is_abbreviated:
  257. if (abbrev_option) {
  258. /*
  259. * If this is abbreviated, it is
  260. * ambiguous. So when there is no
  261. * exact match later, we need to
  262. * error out.
  263. */
  264. ambiguous_option = abbrev_option;
  265. ambiguous_flags = abbrev_flags;
  266. }
  267. if (!(flags & OPT_UNSET) && *arg_end)
  268. p->opt = arg_end + 1;
  269. abbrev_option = options;
  270. abbrev_flags = flags;
  271. continue;
  272. }
  273. /* negated and abbreviated very much? */
  274. if (!prefixcmp("no-", arg)) {
  275. flags |= OPT_UNSET;
  276. goto is_abbreviated;
  277. }
  278. /* negated? */
  279. if (strncmp(arg, "no-", 3))
  280. continue;
  281. flags |= OPT_UNSET;
  282. rest = skip_prefix(arg + 3, options->long_name);
  283. /* abbreviated and negated? */
  284. if (!rest && !prefixcmp(options->long_name, arg + 3))
  285. goto is_abbreviated;
  286. if (!rest)
  287. continue;
  288. }
  289. match:
  290. if (*rest) {
  291. if (*rest != '=')
  292. continue;
  293. p->opt = rest + 1;
  294. }
  295. return get_value(p, options, flags);
  296. }
  297. if (ambiguous_option)
  298. return error("Ambiguous option: %s "
  299. "(could be --%s%s or --%s%s)",
  300. arg,
  301. (ambiguous_flags & OPT_UNSET) ? "no-" : "",
  302. ambiguous_option->long_name,
  303. (abbrev_flags & OPT_UNSET) ? "no-" : "",
  304. abbrev_option->long_name);
  305. if (abbrev_option)
  306. return get_value(p, abbrev_option, abbrev_flags);
  307. return -2;
  308. }
  309. static void check_typos(const char *arg, const struct option *options)
  310. {
  311. if (strlen(arg) < 3)
  312. return;
  313. if (!prefixcmp(arg, "no-")) {
  314. error ("did you mean `--%s` (with two dashes ?)", arg);
  315. exit(129);
  316. }
  317. for (; options->type != OPTION_END; options++) {
  318. if (!options->long_name)
  319. continue;
  320. if (!prefixcmp(options->long_name, arg)) {
  321. error ("did you mean `--%s` (with two dashes ?)", arg);
  322. exit(129);
  323. }
  324. }
  325. }
  326. void parse_options_start(struct parse_opt_ctx_t *ctx,
  327. int argc, const char **argv, int flags)
  328. {
  329. memset(ctx, 0, sizeof(*ctx));
  330. ctx->argc = argc - 1;
  331. ctx->argv = argv + 1;
  332. ctx->out = argv;
  333. ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
  334. ctx->flags = flags;
  335. if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
  336. (flags & PARSE_OPT_STOP_AT_NON_OPTION))
  337. die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
  338. }
  339. static int usage_with_options_internal(const char * const *,
  340. const struct option *, int);
  341. int parse_options_step(struct parse_opt_ctx_t *ctx,
  342. const struct option *options,
  343. const char * const usagestr[])
  344. {
  345. int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  346. int excl_short_opt = 1;
  347. const char *arg;
  348. /* we must reset ->opt, unknown short option leave it dangling */
  349. ctx->opt = NULL;
  350. for (; ctx->argc; ctx->argc--, ctx->argv++) {
  351. arg = ctx->argv[0];
  352. if (*arg != '-' || !arg[1]) {
  353. if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
  354. break;
  355. ctx->out[ctx->cpidx++] = ctx->argv[0];
  356. continue;
  357. }
  358. if (arg[1] != '-') {
  359. ctx->opt = ++arg;
  360. if (internal_help && *ctx->opt == 'h')
  361. return usage_with_options_internal(usagestr, options, 0);
  362. switch (parse_short_opt(ctx, options)) {
  363. case -1:
  364. return parse_options_usage(usagestr, options, arg, 1);
  365. case -2:
  366. goto unknown;
  367. case -3:
  368. goto exclusive;
  369. default:
  370. break;
  371. }
  372. if (ctx->opt)
  373. check_typos(arg, options);
  374. while (ctx->opt) {
  375. if (internal_help && *ctx->opt == 'h')
  376. return usage_with_options_internal(usagestr, options, 0);
  377. arg = ctx->opt;
  378. switch (parse_short_opt(ctx, options)) {
  379. case -1:
  380. return parse_options_usage(usagestr, options, arg, 1);
  381. case -2:
  382. /* fake a short option thing to hide the fact that we may have
  383. * started to parse aggregated stuff
  384. *
  385. * This is leaky, too bad.
  386. */
  387. ctx->argv[0] = strdup(ctx->opt - 1);
  388. *(char *)ctx->argv[0] = '-';
  389. goto unknown;
  390. case -3:
  391. goto exclusive;
  392. default:
  393. break;
  394. }
  395. }
  396. continue;
  397. }
  398. if (!arg[2]) { /* "--" */
  399. if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
  400. ctx->argc--;
  401. ctx->argv++;
  402. }
  403. break;
  404. }
  405. arg += 2;
  406. if (internal_help && !strcmp(arg, "help-all"))
  407. return usage_with_options_internal(usagestr, options, 1);
  408. if (internal_help && !strcmp(arg, "help"))
  409. return usage_with_options_internal(usagestr, options, 0);
  410. if (!strcmp(arg, "list-opts"))
  411. return PARSE_OPT_LIST_OPTS;
  412. if (!strcmp(arg, "list-cmds"))
  413. return PARSE_OPT_LIST_SUBCMDS;
  414. switch (parse_long_opt(ctx, arg, options)) {
  415. case -1:
  416. return parse_options_usage(usagestr, options, arg, 0);
  417. case -2:
  418. goto unknown;
  419. case -3:
  420. excl_short_opt = 0;
  421. goto exclusive;
  422. default:
  423. break;
  424. }
  425. continue;
  426. unknown:
  427. if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
  428. return PARSE_OPT_UNKNOWN;
  429. ctx->out[ctx->cpidx++] = ctx->argv[0];
  430. ctx->opt = NULL;
  431. }
  432. return PARSE_OPT_DONE;
  433. exclusive:
  434. parse_options_usage(usagestr, options, arg, excl_short_opt);
  435. if ((excl_short_opt && ctx->excl_opt->short_name) ||
  436. ctx->excl_opt->long_name == NULL) {
  437. char opt = ctx->excl_opt->short_name;
  438. parse_options_usage(NULL, options, &opt, 1);
  439. } else {
  440. parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
  441. }
  442. return PARSE_OPT_HELP;
  443. }
  444. int parse_options_end(struct parse_opt_ctx_t *ctx)
  445. {
  446. memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
  447. ctx->out[ctx->cpidx + ctx->argc] = NULL;
  448. return ctx->cpidx + ctx->argc;
  449. }
  450. int parse_options_subcommand(int argc, const char **argv, const struct option *options,
  451. const char *const subcommands[], const char *usagestr[], int flags)
  452. {
  453. struct parse_opt_ctx_t ctx;
  454. perf_header__set_cmdline(argc, argv);
  455. /* build usage string if it's not provided */
  456. if (subcommands && !usagestr[0]) {
  457. struct strbuf buf = STRBUF_INIT;
  458. strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
  459. for (int i = 0; subcommands[i]; i++) {
  460. if (i)
  461. strbuf_addstr(&buf, "|");
  462. strbuf_addstr(&buf, subcommands[i]);
  463. }
  464. strbuf_addstr(&buf, "}");
  465. usagestr[0] = strdup(buf.buf);
  466. strbuf_release(&buf);
  467. }
  468. parse_options_start(&ctx, argc, argv, flags);
  469. switch (parse_options_step(&ctx, options, usagestr)) {
  470. case PARSE_OPT_HELP:
  471. exit(129);
  472. case PARSE_OPT_DONE:
  473. break;
  474. case PARSE_OPT_LIST_OPTS:
  475. while (options->type != OPTION_END) {
  476. if (options->long_name)
  477. printf("--%s ", options->long_name);
  478. options++;
  479. }
  480. putchar('\n');
  481. exit(130);
  482. case PARSE_OPT_LIST_SUBCMDS:
  483. if (subcommands) {
  484. for (int i = 0; subcommands[i]; i++)
  485. printf("%s ", subcommands[i]);
  486. }
  487. putchar('\n');
  488. exit(130);
  489. default: /* PARSE_OPT_UNKNOWN */
  490. if (ctx.argv[0][1] == '-') {
  491. error("unknown option `%s'", ctx.argv[0] + 2);
  492. } else {
  493. error("unknown switch `%c'", *ctx.opt);
  494. }
  495. usage_with_options(usagestr, options);
  496. }
  497. return parse_options_end(&ctx);
  498. }
  499. int parse_options(int argc, const char **argv, const struct option *options,
  500. const char * const usagestr[], int flags)
  501. {
  502. return parse_options_subcommand(argc, argv, options, NULL,
  503. (const char **) usagestr, flags);
  504. }
  505. #define USAGE_OPTS_WIDTH 24
  506. #define USAGE_GAP 2
  507. static void print_option_help(const struct option *opts, int full)
  508. {
  509. size_t pos;
  510. int pad;
  511. if (opts->type == OPTION_GROUP) {
  512. fputc('\n', stderr);
  513. if (*opts->help)
  514. fprintf(stderr, "%s\n", opts->help);
  515. return;
  516. }
  517. if (!full && (opts->flags & PARSE_OPT_HIDDEN))
  518. return;
  519. if (opts->flags & PARSE_OPT_DISABLED)
  520. return;
  521. pos = fprintf(stderr, " ");
  522. if (opts->short_name)
  523. pos += fprintf(stderr, "-%c", opts->short_name);
  524. else
  525. pos += fprintf(stderr, " ");
  526. if (opts->long_name && opts->short_name)
  527. pos += fprintf(stderr, ", ");
  528. if (opts->long_name)
  529. pos += fprintf(stderr, "--%s", opts->long_name);
  530. switch (opts->type) {
  531. case OPTION_ARGUMENT:
  532. break;
  533. case OPTION_LONG:
  534. case OPTION_U64:
  535. case OPTION_INTEGER:
  536. case OPTION_UINTEGER:
  537. if (opts->flags & PARSE_OPT_OPTARG)
  538. if (opts->long_name)
  539. pos += fprintf(stderr, "[=<n>]");
  540. else
  541. pos += fprintf(stderr, "[<n>]");
  542. else
  543. pos += fprintf(stderr, " <n>");
  544. break;
  545. case OPTION_CALLBACK:
  546. if (opts->flags & PARSE_OPT_NOARG)
  547. break;
  548. /* FALLTHROUGH */
  549. case OPTION_STRING:
  550. if (opts->argh) {
  551. if (opts->flags & PARSE_OPT_OPTARG)
  552. if (opts->long_name)
  553. pos += fprintf(stderr, "[=<%s>]", opts->argh);
  554. else
  555. pos += fprintf(stderr, "[<%s>]", opts->argh);
  556. else
  557. pos += fprintf(stderr, " <%s>", opts->argh);
  558. } else {
  559. if (opts->flags & PARSE_OPT_OPTARG)
  560. if (opts->long_name)
  561. pos += fprintf(stderr, "[=...]");
  562. else
  563. pos += fprintf(stderr, "[...]");
  564. else
  565. pos += fprintf(stderr, " ...");
  566. }
  567. break;
  568. default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
  569. case OPTION_END:
  570. case OPTION_GROUP:
  571. case OPTION_BIT:
  572. case OPTION_BOOLEAN:
  573. case OPTION_INCR:
  574. case OPTION_SET_UINT:
  575. case OPTION_SET_PTR:
  576. break;
  577. }
  578. if (pos <= USAGE_OPTS_WIDTH)
  579. pad = USAGE_OPTS_WIDTH - pos;
  580. else {
  581. fputc('\n', stderr);
  582. pad = USAGE_OPTS_WIDTH;
  583. }
  584. fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
  585. }
  586. int usage_with_options_internal(const char * const *usagestr,
  587. const struct option *opts, int full)
  588. {
  589. if (!usagestr)
  590. return PARSE_OPT_HELP;
  591. fprintf(stderr, "\n usage: %s\n", *usagestr++);
  592. while (*usagestr && **usagestr)
  593. fprintf(stderr, " or: %s\n", *usagestr++);
  594. while (*usagestr) {
  595. fprintf(stderr, "%s%s\n",
  596. **usagestr ? " " : "",
  597. *usagestr);
  598. usagestr++;
  599. }
  600. if (opts->type != OPTION_GROUP)
  601. fputc('\n', stderr);
  602. for ( ; opts->type != OPTION_END; opts++)
  603. print_option_help(opts, full);
  604. fputc('\n', stderr);
  605. return PARSE_OPT_HELP;
  606. }
  607. void usage_with_options(const char * const *usagestr,
  608. const struct option *opts)
  609. {
  610. exit_browser(false);
  611. usage_with_options_internal(usagestr, opts, 0);
  612. exit(129);
  613. }
  614. int parse_options_usage(const char * const *usagestr,
  615. const struct option *opts,
  616. const char *optstr, bool short_opt)
  617. {
  618. if (!usagestr)
  619. goto opt;
  620. fprintf(stderr, "\n usage: %s\n", *usagestr++);
  621. while (*usagestr && **usagestr)
  622. fprintf(stderr, " or: %s\n", *usagestr++);
  623. while (*usagestr) {
  624. fprintf(stderr, "%s%s\n",
  625. **usagestr ? " " : "",
  626. *usagestr);
  627. usagestr++;
  628. }
  629. fputc('\n', stderr);
  630. opt:
  631. for ( ; opts->type != OPTION_END; opts++) {
  632. if (short_opt) {
  633. if (opts->short_name == *optstr)
  634. break;
  635. continue;
  636. }
  637. if (opts->long_name == NULL)
  638. continue;
  639. if (!prefixcmp(optstr, opts->long_name))
  640. break;
  641. if (!prefixcmp(optstr, "no-") &&
  642. !prefixcmp(optstr + 3, opts->long_name))
  643. break;
  644. }
  645. if (opts->type != OPTION_END)
  646. print_option_help(opts, 0);
  647. return PARSE_OPT_HELP;
  648. }
  649. int parse_opt_verbosity_cb(const struct option *opt,
  650. const char *arg __maybe_unused,
  651. int unset)
  652. {
  653. int *target = opt->value;
  654. if (unset)
  655. /* --no-quiet, --no-verbose */
  656. *target = 0;
  657. else if (opt->short_name == 'v') {
  658. if (*target >= 0)
  659. (*target)++;
  660. else
  661. *target = 1;
  662. } else {
  663. if (*target <= 0)
  664. (*target)--;
  665. else
  666. *target = -1;
  667. }
  668. return 0;
  669. }
  670. void set_option_flag(struct option *opts, int shortopt, const char *longopt,
  671. int flag)
  672. {
  673. for (; opts->type != OPTION_END; opts++) {
  674. if ((shortopt && opts->short_name == shortopt) ||
  675. (opts->long_name && longopt &&
  676. !strcmp(opts->long_name, longopt))) {
  677. opts->flags |= flag;
  678. break;
  679. }
  680. }
  681. }