parse-options.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include "util.h"
  2. #include "parse-options.h"
  3. #include "cache.h"
  4. #define OPT_SHORT 1
  5. #define OPT_UNSET 2
  6. static int opterror(const struct option *opt, const char *reason, int flags)
  7. {
  8. if (flags & OPT_SHORT)
  9. return error("switch `%c' %s", opt->short_name, reason);
  10. if (flags & OPT_UNSET)
  11. return error("option `no-%s' %s", opt->long_name, reason);
  12. return error("option `%s' %s", opt->long_name, reason);
  13. }
  14. static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
  15. int flags, const char **arg)
  16. {
  17. if (p->opt) {
  18. *arg = p->opt;
  19. p->opt = NULL;
  20. } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
  21. *arg = (const char *)opt->defval;
  22. } else if (p->argc > 1) {
  23. p->argc--;
  24. *arg = *++p->argv;
  25. } else
  26. return opterror(opt, "requires a value", flags);
  27. return 0;
  28. }
  29. static int get_value(struct parse_opt_ctx_t *p,
  30. const struct option *opt, int flags)
  31. {
  32. const char *s, *arg;
  33. const int unset = flags & OPT_UNSET;
  34. if (unset && p->opt)
  35. return opterror(opt, "takes no value", flags);
  36. if (unset && (opt->flags & PARSE_OPT_NONEG))
  37. return opterror(opt, "isn't available", flags);
  38. if (!(flags & OPT_SHORT) && p->opt) {
  39. switch (opt->type) {
  40. case OPTION_CALLBACK:
  41. if (!(opt->flags & PARSE_OPT_NOARG))
  42. break;
  43. /* FALLTHROUGH */
  44. case OPTION_BOOLEAN:
  45. case OPTION_BIT:
  46. case OPTION_SET_INT:
  47. case OPTION_SET_PTR:
  48. return opterror(opt, "takes no value", flags);
  49. default:
  50. break;
  51. }
  52. }
  53. switch (opt->type) {
  54. case OPTION_BIT:
  55. if (unset)
  56. *(int *)opt->value &= ~opt->defval;
  57. else
  58. *(int *)opt->value |= opt->defval;
  59. return 0;
  60. case OPTION_BOOLEAN:
  61. *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  62. return 0;
  63. case OPTION_SET_INT:
  64. *(int *)opt->value = unset ? 0 : opt->defval;
  65. return 0;
  66. case OPTION_SET_PTR:
  67. *(void **)opt->value = unset ? NULL : (void *)opt->defval;
  68. return 0;
  69. case OPTION_STRING:
  70. if (unset)
  71. *(const char **)opt->value = NULL;
  72. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  73. *(const char **)opt->value = (const char *)opt->defval;
  74. else
  75. return get_arg(p, opt, flags, (const char **)opt->value);
  76. return 0;
  77. case OPTION_CALLBACK:
  78. if (unset)
  79. return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
  80. if (opt->flags & PARSE_OPT_NOARG)
  81. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  82. if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  83. return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
  84. if (get_arg(p, opt, flags, &arg))
  85. return -1;
  86. return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
  87. case OPTION_INTEGER:
  88. if (unset) {
  89. *(int *)opt->value = 0;
  90. return 0;
  91. }
  92. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  93. *(int *)opt->value = opt->defval;
  94. return 0;
  95. }
  96. if (get_arg(p, opt, flags, &arg))
  97. return -1;
  98. *(int *)opt->value = strtol(arg, (char **)&s, 10);
  99. if (*s)
  100. return opterror(opt, "expects a numerical value", flags);
  101. return 0;
  102. default:
  103. die("should not happen, someone must be hit on the forehead");
  104. }
  105. }
  106. static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
  107. {
  108. for (; options->type != OPTION_END; options++) {
  109. if (options->short_name == *p->opt) {
  110. p->opt = p->opt[1] ? p->opt + 1 : NULL;
  111. return get_value(p, options, OPT_SHORT);
  112. }
  113. }
  114. return -2;
  115. }
  116. static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
  117. const struct option *options)
  118. {
  119. const char *arg_end = strchr(arg, '=');
  120. const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
  121. int abbrev_flags = 0, ambiguous_flags = 0;
  122. if (!arg_end)
  123. arg_end = arg + strlen(arg);
  124. for (; options->type != OPTION_END; options++) {
  125. const char *rest;
  126. int flags = 0;
  127. if (!options->long_name)
  128. continue;
  129. rest = skip_prefix(arg, options->long_name);
  130. if (options->type == OPTION_ARGUMENT) {
  131. if (!rest)
  132. continue;
  133. if (*rest == '=')
  134. return opterror(options, "takes no value", flags);
  135. if (*rest)
  136. continue;
  137. p->out[p->cpidx++] = arg - 2;
  138. return 0;
  139. }
  140. if (!rest) {
  141. /* abbreviated? */
  142. if (!strncmp(options->long_name, arg, arg_end - arg)) {
  143. is_abbreviated:
  144. if (abbrev_option) {
  145. /*
  146. * If this is abbreviated, it is
  147. * ambiguous. So when there is no
  148. * exact match later, we need to
  149. * error out.
  150. */
  151. ambiguous_option = abbrev_option;
  152. ambiguous_flags = abbrev_flags;
  153. }
  154. if (!(flags & OPT_UNSET) && *arg_end)
  155. p->opt = arg_end + 1;
  156. abbrev_option = options;
  157. abbrev_flags = flags;
  158. continue;
  159. }
  160. /* negated and abbreviated very much? */
  161. if (!prefixcmp("no-", arg)) {
  162. flags |= OPT_UNSET;
  163. goto is_abbreviated;
  164. }
  165. /* negated? */
  166. if (strncmp(arg, "no-", 3))
  167. continue;
  168. flags |= OPT_UNSET;
  169. rest = skip_prefix(arg + 3, options->long_name);
  170. /* abbreviated and negated? */
  171. if (!rest && !prefixcmp(options->long_name, arg + 3))
  172. goto is_abbreviated;
  173. if (!rest)
  174. continue;
  175. }
  176. if (*rest) {
  177. if (*rest != '=')
  178. continue;
  179. p->opt = rest + 1;
  180. }
  181. return get_value(p, options, flags);
  182. }
  183. if (ambiguous_option)
  184. return error("Ambiguous option: %s "
  185. "(could be --%s%s or --%s%s)",
  186. arg,
  187. (ambiguous_flags & OPT_UNSET) ? "no-" : "",
  188. ambiguous_option->long_name,
  189. (abbrev_flags & OPT_UNSET) ? "no-" : "",
  190. abbrev_option->long_name);
  191. if (abbrev_option)
  192. return get_value(p, abbrev_option, abbrev_flags);
  193. return -2;
  194. }
  195. static void check_typos(const char *arg, const struct option *options)
  196. {
  197. if (strlen(arg) < 3)
  198. return;
  199. if (!prefixcmp(arg, "no-")) {
  200. error ("did you mean `--%s` (with two dashes ?)", arg);
  201. exit(129);
  202. }
  203. for (; options->type != OPTION_END; options++) {
  204. if (!options->long_name)
  205. continue;
  206. if (!prefixcmp(options->long_name, arg)) {
  207. error ("did you mean `--%s` (with two dashes ?)", arg);
  208. exit(129);
  209. }
  210. }
  211. }
  212. void parse_options_start(struct parse_opt_ctx_t *ctx,
  213. int argc, const char **argv, int flags)
  214. {
  215. memset(ctx, 0, sizeof(*ctx));
  216. ctx->argc = argc - 1;
  217. ctx->argv = argv + 1;
  218. ctx->out = argv;
  219. ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
  220. ctx->flags = flags;
  221. if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
  222. (flags & PARSE_OPT_STOP_AT_NON_OPTION))
  223. die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
  224. }
  225. static int usage_with_options_internal(const char * const *,
  226. const struct option *, int);
  227. int parse_options_step(struct parse_opt_ctx_t *ctx,
  228. const struct option *options,
  229. const char * const usagestr[])
  230. {
  231. int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  232. /* we must reset ->opt, unknown short option leave it dangling */
  233. ctx->opt = NULL;
  234. for (; ctx->argc; ctx->argc--, ctx->argv++) {
  235. const char *arg = ctx->argv[0];
  236. if (*arg != '-' || !arg[1]) {
  237. if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
  238. break;
  239. ctx->out[ctx->cpidx++] = ctx->argv[0];
  240. continue;
  241. }
  242. if (arg[1] != '-') {
  243. ctx->opt = arg + 1;
  244. if (internal_help && *ctx->opt == 'h')
  245. return parse_options_usage(usagestr, options);
  246. switch (parse_short_opt(ctx, options)) {
  247. case -1:
  248. return parse_options_usage(usagestr, options);
  249. case -2:
  250. goto unknown;
  251. }
  252. if (ctx->opt)
  253. check_typos(arg + 1, options);
  254. while (ctx->opt) {
  255. if (internal_help && *ctx->opt == 'h')
  256. return parse_options_usage(usagestr, options);
  257. switch (parse_short_opt(ctx, options)) {
  258. case -1:
  259. return parse_options_usage(usagestr, options);
  260. case -2:
  261. /* fake a short option thing to hide the fact that we may have
  262. * started to parse aggregated stuff
  263. *
  264. * This is leaky, too bad.
  265. */
  266. ctx->argv[0] = strdup(ctx->opt - 1);
  267. *(char *)ctx->argv[0] = '-';
  268. goto unknown;
  269. }
  270. }
  271. continue;
  272. }
  273. if (!arg[2]) { /* "--" */
  274. if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
  275. ctx->argc--;
  276. ctx->argv++;
  277. }
  278. break;
  279. }
  280. if (internal_help && !strcmp(arg + 2, "help-all"))
  281. return usage_with_options_internal(usagestr, options, 1);
  282. if (internal_help && !strcmp(arg + 2, "help"))
  283. return parse_options_usage(usagestr, options);
  284. switch (parse_long_opt(ctx, arg + 2, options)) {
  285. case -1:
  286. return parse_options_usage(usagestr, options);
  287. case -2:
  288. goto unknown;
  289. }
  290. continue;
  291. unknown:
  292. if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
  293. return PARSE_OPT_UNKNOWN;
  294. ctx->out[ctx->cpidx++] = ctx->argv[0];
  295. ctx->opt = NULL;
  296. }
  297. return PARSE_OPT_DONE;
  298. }
  299. int parse_options_end(struct parse_opt_ctx_t *ctx)
  300. {
  301. memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
  302. ctx->out[ctx->cpidx + ctx->argc] = NULL;
  303. return ctx->cpidx + ctx->argc;
  304. }
  305. int parse_options(int argc, const char **argv, const struct option *options,
  306. const char * const usagestr[], int flags)
  307. {
  308. struct parse_opt_ctx_t ctx;
  309. parse_options_start(&ctx, argc, argv, flags);
  310. switch (parse_options_step(&ctx, options, usagestr)) {
  311. case PARSE_OPT_HELP:
  312. exit(129);
  313. case PARSE_OPT_DONE:
  314. break;
  315. default: /* PARSE_OPT_UNKNOWN */
  316. if (ctx.argv[0][1] == '-') {
  317. error("unknown option `%s'", ctx.argv[0] + 2);
  318. } else {
  319. error("unknown switch `%c'", *ctx.opt);
  320. }
  321. usage_with_options(usagestr, options);
  322. }
  323. return parse_options_end(&ctx);
  324. }
  325. #define USAGE_OPTS_WIDTH 24
  326. #define USAGE_GAP 2
  327. int usage_with_options_internal(const char * const *usagestr,
  328. const struct option *opts, int full)
  329. {
  330. if (!usagestr)
  331. return PARSE_OPT_HELP;
  332. fprintf(stderr, "usage: %s\n", *usagestr++);
  333. while (*usagestr && **usagestr)
  334. fprintf(stderr, " or: %s\n", *usagestr++);
  335. while (*usagestr) {
  336. fprintf(stderr, "%s%s\n",
  337. **usagestr ? " " : "",
  338. *usagestr);
  339. usagestr++;
  340. }
  341. if (opts->type != OPTION_GROUP)
  342. fputc('\n', stderr);
  343. for (; opts->type != OPTION_END; opts++) {
  344. size_t pos;
  345. int pad;
  346. if (opts->type == OPTION_GROUP) {
  347. fputc('\n', stderr);
  348. if (*opts->help)
  349. fprintf(stderr, "%s\n", opts->help);
  350. continue;
  351. }
  352. if (!full && (opts->flags & PARSE_OPT_HIDDEN))
  353. continue;
  354. pos = fprintf(stderr, " ");
  355. if (opts->short_name)
  356. pos += fprintf(stderr, "-%c", opts->short_name);
  357. if (opts->long_name && opts->short_name)
  358. pos += fprintf(stderr, ", ");
  359. if (opts->long_name)
  360. pos += fprintf(stderr, "--%s", opts->long_name);
  361. switch (opts->type) {
  362. case OPTION_ARGUMENT:
  363. break;
  364. case OPTION_INTEGER:
  365. if (opts->flags & PARSE_OPT_OPTARG)
  366. if (opts->long_name)
  367. pos += fprintf(stderr, "[=<n>]");
  368. else
  369. pos += fprintf(stderr, "[<n>]");
  370. else
  371. pos += fprintf(stderr, " <n>");
  372. break;
  373. case OPTION_CALLBACK:
  374. if (opts->flags & PARSE_OPT_NOARG)
  375. break;
  376. /* FALLTHROUGH */
  377. case OPTION_STRING:
  378. if (opts->argh) {
  379. if (opts->flags & PARSE_OPT_OPTARG)
  380. if (opts->long_name)
  381. pos += fprintf(stderr, "[=<%s>]", opts->argh);
  382. else
  383. pos += fprintf(stderr, "[<%s>]", opts->argh);
  384. else
  385. pos += fprintf(stderr, " <%s>", opts->argh);
  386. } else {
  387. if (opts->flags & PARSE_OPT_OPTARG)
  388. if (opts->long_name)
  389. pos += fprintf(stderr, "[=...]");
  390. else
  391. pos += fprintf(stderr, "[...]");
  392. else
  393. pos += fprintf(stderr, " ...");
  394. }
  395. break;
  396. default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
  397. break;
  398. }
  399. if (pos <= USAGE_OPTS_WIDTH)
  400. pad = USAGE_OPTS_WIDTH - pos;
  401. else {
  402. fputc('\n', stderr);
  403. pad = USAGE_OPTS_WIDTH;
  404. }
  405. fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
  406. }
  407. fputc('\n', stderr);
  408. return PARSE_OPT_HELP;
  409. }
  410. void usage_with_options(const char * const *usagestr,
  411. const struct option *opts)
  412. {
  413. usage_with_options_internal(usagestr, opts, 0);
  414. exit(129);
  415. }
  416. int parse_options_usage(const char * const *usagestr,
  417. const struct option *opts)
  418. {
  419. return usage_with_options_internal(usagestr, opts, 0);
  420. }
  421. /*----- some often used options -----*/
  422. #include "cache.h"
  423. int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
  424. int unset)
  425. {
  426. int *target = opt->value;
  427. if (unset)
  428. /* --no-quiet, --no-verbose */
  429. *target = 0;
  430. else if (opt->short_name == 'v') {
  431. if (*target >= 0)
  432. (*target)++;
  433. else
  434. *target = 1;
  435. } else {
  436. if (*target <= 0)
  437. (*target)--;
  438. else
  439. *target = -1;
  440. }
  441. return 0;
  442. }