parse-options.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. static int disallow_abbreviated_options;
  9. #define OPT_SHORT 1
  10. #define OPT_UNSET 2
  11. int optbug(const struct option *opt, const char *reason)
  12. {
  13. if (opt->long_name) {
  14. if (opt->short_name)
  15. return error("BUG: switch '%c' (--%s) %s",
  16. opt->short_name, opt->long_name, reason);
  17. return error("BUG: option '%s' %s", opt->long_name, reason);
  18. }
  19. return error("BUG: switch '%c' %s", opt->short_name, reason);
  20. }
  21. static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
  22. const struct option *opt,
  23. int flags, const char **arg)
  24. {
  25. if (p->opt) {
  26. *arg = p->opt;
  27. p->opt = NULL;
  28. } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
  29. *arg = (const char *)opt->defval;
  30. } else if (p->argc > 1) {
  31. p->argc--;
  32. *arg = *++p->argv;
  33. } else
  34. return error(_("%s requires a value"), optname(opt, flags));
  35. return 0;
  36. }
  37. static void fix_filename(const char *prefix, const char **file)
  38. {
  39. if (!file || !*file || !prefix || is_absolute_path(*file)
  40. || !strcmp("-", *file))
  41. return;
  42. *file = prefix_filename(prefix, *file);
  43. }
  44. static enum parse_opt_result opt_command_mode_error(
  45. const struct option *opt,
  46. const struct option *all_opts,
  47. int flags)
  48. {
  49. const struct option *that;
  50. struct strbuf that_name = STRBUF_INIT;
  51. /*
  52. * Find the other option that was used to set the variable
  53. * already, and report that this is not compatible with it.
  54. */
  55. for (that = all_opts; that->type != OPTION_END; that++) {
  56. if (that == opt ||
  57. !(that->flags & PARSE_OPT_CMDMODE) ||
  58. that->value != opt->value ||
  59. that->defval != *(int *)opt->value)
  60. continue;
  61. if (that->long_name)
  62. strbuf_addf(&that_name, "--%s", that->long_name);
  63. else
  64. strbuf_addf(&that_name, "-%c", that->short_name);
  65. error(_("%s is incompatible with %s"),
  66. optname(opt, flags), that_name.buf);
  67. strbuf_release(&that_name);
  68. return PARSE_OPT_ERROR;
  69. }
  70. return error(_("%s : incompatible with something else"),
  71. optname(opt, flags));
  72. }
  73. static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
  74. const struct option *opt,
  75. const struct option *all_opts,
  76. int flags)
  77. {
  78. const char *s, *arg;
  79. const int unset = flags & OPT_UNSET;
  80. int err;
  81. if (unset && p->opt)
  82. return error(_("%s takes no value"), optname(opt, flags));
  83. if (unset && (opt->flags & PARSE_OPT_NONEG))
  84. return error(_("%s isn't available"), optname(opt, flags));
  85. if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
  86. return error(_("%s takes no value"), optname(opt, flags));
  87. /*
  88. * Giving the same mode option twice, although unnecessary,
  89. * is not a grave error, so let it pass.
  90. */
  91. if ((opt->flags & PARSE_OPT_CMDMODE) &&
  92. *(int *)opt->value && *(int *)opt->value != opt->defval)
  93. return opt_command_mode_error(opt, all_opts, flags);
  94. switch (opt->type) {
  95. case OPTION_LOWLEVEL_CALLBACK:
  96. return opt->ll_callback(p, opt, NULL, unset);
  97. case OPTION_BIT:
  98. if (unset)
  99. *(int *)opt->value &= ~opt->defval;
  100. else
  101. *(int *)opt->value |= opt->defval;
  102. return 0;
  103. case OPTION_NEGBIT:
  104. if (unset)
  105. *(int *)opt->value |= opt->defval;
  106. else
  107. *(int *)opt->value &= ~opt->defval;
  108. return 0;
  109. case OPTION_BITOP:
  110. if (unset)
  111. BUG("BITOP can't have unset form");
  112. *(int *)opt->value &= ~opt->extra;
  113. *(int *)opt->value |= opt->defval;
  114. return 0;
  115. case OPTION_COUNTUP:
  116. if (*(int *)opt->value < 0)
  117. *(int *)opt->value = 0;
  118. *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
  119. return 0;
  120. case OPTION_SET_INT:
  121. *(int *)opt->value = unset ? 0 : opt->defval;
  122. return 0;
  123. case OPTION_STRING:
  124. if (unset)
  125. *(const char **)opt->value = NULL;
  126. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  127. *(const char **)opt->value = (const char *)opt->defval;
  128. else
  129. return get_arg(p, opt, flags, (const char **)opt->value);
  130. return 0;
  131. case OPTION_FILENAME:
  132. err = 0;
  133. if (unset)
  134. *(const char **)opt->value = NULL;
  135. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  136. *(const char **)opt->value = (const char *)opt->defval;
  137. else
  138. err = get_arg(p, opt, flags, (const char **)opt->value);
  139. if (!err)
  140. fix_filename(p->prefix, (const char **)opt->value);
  141. return err;
  142. case OPTION_CALLBACK:
  143. {
  144. const char *p_arg = NULL;
  145. int p_unset;
  146. if (unset)
  147. p_unset = 1;
  148. else if (opt->flags & PARSE_OPT_NOARG)
  149. p_unset = 0;
  150. else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
  151. p_unset = 0;
  152. else if (get_arg(p, opt, flags, &arg))
  153. return -1;
  154. else {
  155. p_unset = 0;
  156. p_arg = arg;
  157. }
  158. if (opt->callback)
  159. return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
  160. else
  161. return (*opt->ll_callback)(p, opt, p_arg, p_unset);
  162. }
  163. case OPTION_INTEGER:
  164. if (unset) {
  165. *(int *)opt->value = 0;
  166. return 0;
  167. }
  168. if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
  169. *(int *)opt->value = opt->defval;
  170. return 0;
  171. }
  172. if (get_arg(p, opt, flags, &arg))
  173. return -1;
  174. if (!*arg)
  175. return error(_("%s expects a numerical value"),
  176. optname(opt, flags));
  177. *(int *)opt->value = strtol(arg, (char **)&s, 10);
  178. if (*s)
  179. return error(_("%s expects a numerical value"),
  180. optname(opt, flags));
  181. return 0;
  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 error(_("%s expects a non-negative integer value"
  195. " with an optional k/m/g suffix"),
  196. optname(opt, flags));
  197. return 0;
  198. default:
  199. BUG("opt->type %d should not happen", opt->type);
  200. }
  201. }
  202. static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
  203. const struct option *options)
  204. {
  205. const struct option *all_opts = options;
  206. const struct option *numopt = NULL;
  207. for (; options->type != OPTION_END; options++) {
  208. if (options->short_name == *p->opt) {
  209. p->opt = p->opt[1] ? p->opt + 1 : NULL;
  210. return get_value(p, options, all_opts, OPT_SHORT);
  211. }
  212. /*
  213. * Handle the numerical option later, explicit one-digit
  214. * options take precedence over it.
  215. */
  216. if (options->type == OPTION_NUMBER)
  217. numopt = options;
  218. }
  219. if (numopt && isdigit(*p->opt)) {
  220. size_t len = 1;
  221. char *arg;
  222. int rc;
  223. while (isdigit(p->opt[len]))
  224. len++;
  225. arg = xmemdupz(p->opt, len);
  226. p->opt = p->opt[len] ? p->opt + len : NULL;
  227. if (numopt->callback)
  228. rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
  229. else
  230. rc = (*numopt->ll_callback)(p, numopt, arg, 0);
  231. free(arg);
  232. return rc;
  233. }
  234. return PARSE_OPT_UNKNOWN;
  235. }
  236. static int has_string(const char *it, const char **array)
  237. {
  238. while (*array)
  239. if (!strcmp(it, *(array++)))
  240. return 1;
  241. return 0;
  242. }
  243. static int is_alias(struct parse_opt_ctx_t *ctx,
  244. const struct option *one_opt,
  245. const struct option *another_opt)
  246. {
  247. const char **group;
  248. if (!ctx->alias_groups)
  249. return 0;
  250. if (!one_opt->long_name || !another_opt->long_name)
  251. return 0;
  252. for (group = ctx->alias_groups; *group; group += 3) {
  253. /* it and other are from the same family? */
  254. if (has_string(one_opt->long_name, group) &&
  255. has_string(another_opt->long_name, group))
  256. return 1;
  257. }
  258. return 0;
  259. }
  260. static enum parse_opt_result parse_long_opt(
  261. struct parse_opt_ctx_t *p, const char *arg,
  262. const struct option *options)
  263. {
  264. const struct option *all_opts = options;
  265. const char *arg_end = strchrnul(arg, '=');
  266. const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
  267. int abbrev_flags = 0, ambiguous_flags = 0;
  268. for (; options->type != OPTION_END; options++) {
  269. const char *rest, *long_name = options->long_name;
  270. int flags = 0, opt_flags = 0;
  271. if (!long_name)
  272. continue;
  273. again:
  274. if (!skip_prefix(arg, long_name, &rest))
  275. rest = NULL;
  276. if (options->type == OPTION_ARGUMENT) {
  277. if (!rest)
  278. continue;
  279. if (*rest == '=')
  280. return error(_("%s takes no value"),
  281. optname(options, flags));
  282. if (*rest)
  283. continue;
  284. if (options->value)
  285. *(int *)options->value = options->defval;
  286. p->out[p->cpidx++] = arg - 2;
  287. return PARSE_OPT_DONE;
  288. }
  289. if (!rest) {
  290. /* abbreviated? */
  291. if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
  292. !strncmp(long_name, arg, arg_end - arg)) {
  293. is_abbreviated:
  294. if (abbrev_option &&
  295. !is_alias(p, abbrev_option, options)) {
  296. /*
  297. * If this is abbreviated, it is
  298. * ambiguous. So when there is no
  299. * exact match later, we need to
  300. * error out.
  301. */
  302. ambiguous_option = abbrev_option;
  303. ambiguous_flags = abbrev_flags;
  304. }
  305. if (!(flags & OPT_UNSET) && *arg_end)
  306. p->opt = arg_end + 1;
  307. abbrev_option = options;
  308. abbrev_flags = flags ^ opt_flags;
  309. continue;
  310. }
  311. /* negation allowed? */
  312. if (options->flags & PARSE_OPT_NONEG)
  313. continue;
  314. /* negated and abbreviated very much? */
  315. if (starts_with("no-", arg)) {
  316. flags |= OPT_UNSET;
  317. goto is_abbreviated;
  318. }
  319. /* negated? */
  320. if (!starts_with(arg, "no-")) {
  321. if (skip_prefix(long_name, "no-", &long_name)) {
  322. opt_flags |= OPT_UNSET;
  323. goto again;
  324. }
  325. continue;
  326. }
  327. flags |= OPT_UNSET;
  328. if (!skip_prefix(arg + 3, long_name, &rest)) {
  329. /* abbreviated and negated? */
  330. if (starts_with(long_name, arg + 3))
  331. goto is_abbreviated;
  332. else
  333. continue;
  334. }
  335. }
  336. if (*rest) {
  337. if (*rest != '=')
  338. continue;
  339. p->opt = rest + 1;
  340. }
  341. return get_value(p, options, all_opts, flags ^ opt_flags);
  342. }
  343. if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
  344. die("disallowed abbreviated or ambiguous option '%.*s'",
  345. (int)(arg_end - arg), arg);
  346. if (ambiguous_option) {
  347. error(_("ambiguous option: %s "
  348. "(could be --%s%s or --%s%s)"),
  349. arg,
  350. (ambiguous_flags & OPT_UNSET) ? "no-" : "",
  351. ambiguous_option->long_name,
  352. (abbrev_flags & OPT_UNSET) ? "no-" : "",
  353. abbrev_option->long_name);
  354. return PARSE_OPT_HELP;
  355. }
  356. if (abbrev_option)
  357. return get_value(p, abbrev_option, all_opts, abbrev_flags);
  358. return PARSE_OPT_UNKNOWN;
  359. }
  360. static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
  361. const struct option *options)
  362. {
  363. const struct option *all_opts = options;
  364. for (; options->type != OPTION_END; options++) {
  365. if (!(options->flags & PARSE_OPT_NODASH))
  366. continue;
  367. if (options->short_name == arg[0] && arg[1] == '\0')
  368. return get_value(p, options, all_opts, OPT_SHORT);
  369. }
  370. return -2;
  371. }
  372. static void check_typos(const char *arg, const struct option *options)
  373. {
  374. if (strlen(arg) < 3)
  375. return;
  376. if (starts_with(arg, "no-")) {
  377. error(_("did you mean `--%s` (with two dashes)?"), arg);
  378. exit(129);
  379. }
  380. for (; options->type != OPTION_END; options++) {
  381. if (!options->long_name)
  382. continue;
  383. if (starts_with(options->long_name, arg)) {
  384. error(_("did you mean `--%s` (with two dashes)?"), arg);
  385. exit(129);
  386. }
  387. }
  388. }
  389. static void parse_options_check(const struct option *opts)
  390. {
  391. int err = 0;
  392. char short_opts[128];
  393. memset(short_opts, '\0', sizeof(short_opts));
  394. for (; opts->type != OPTION_END; opts++) {
  395. if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
  396. (opts->flags & PARSE_OPT_OPTARG))
  397. err |= optbug(opts, "uses incompatible flags "
  398. "LASTARG_DEFAULT and OPTARG");
  399. if (opts->short_name) {
  400. if (0x7F <= opts->short_name)
  401. err |= optbug(opts, "invalid short name");
  402. else if (short_opts[opts->short_name]++)
  403. err |= optbug(opts, "short name already used");
  404. }
  405. if (opts->flags & PARSE_OPT_NODASH &&
  406. ((opts->flags & PARSE_OPT_OPTARG) ||
  407. !(opts->flags & PARSE_OPT_NOARG) ||
  408. !(opts->flags & PARSE_OPT_NONEG) ||
  409. opts->long_name))
  410. err |= optbug(opts, "uses feature "
  411. "not supported for dashless options");
  412. switch (opts->type) {
  413. case OPTION_COUNTUP:
  414. case OPTION_BIT:
  415. case OPTION_NEGBIT:
  416. case OPTION_SET_INT:
  417. case OPTION_NUMBER:
  418. if ((opts->flags & PARSE_OPT_OPTARG) ||
  419. !(opts->flags & PARSE_OPT_NOARG))
  420. err |= optbug(opts, "should not accept an argument");
  421. break;
  422. case OPTION_CALLBACK:
  423. if (!opts->callback && !opts->ll_callback)
  424. BUG("OPTION_CALLBACK needs one callback");
  425. if (opts->callback && opts->ll_callback)
  426. BUG("OPTION_CALLBACK can't have two callbacks");
  427. break;
  428. case OPTION_LOWLEVEL_CALLBACK:
  429. if (!opts->ll_callback)
  430. BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
  431. if (opts->callback)
  432. BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
  433. break;
  434. case OPTION_ALIAS:
  435. BUG("OPT_ALIAS() should not remain at this point. "
  436. "Are you using parse_options_step() directly?\n"
  437. "That case is not supported yet.");
  438. default:
  439. ; /* ok. (usually accepts an argument) */
  440. }
  441. if (opts->argh &&
  442. strcspn(opts->argh, " _") != strlen(opts->argh))
  443. err |= optbug(opts, "multi-word argh should use dash to separate words");
  444. }
  445. if (err)
  446. exit(128);
  447. }
  448. static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
  449. int argc, const char **argv, const char *prefix,
  450. const struct option *options, int flags)
  451. {
  452. ctx->argc = argc;
  453. ctx->argv = argv;
  454. if (!(flags & PARSE_OPT_ONE_SHOT)) {
  455. ctx->argc--;
  456. ctx->argv++;
  457. }
  458. ctx->total = ctx->argc;
  459. ctx->out = argv;
  460. ctx->prefix = prefix;
  461. ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
  462. ctx->flags = flags;
  463. if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
  464. (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
  465. !(flags & PARSE_OPT_ONE_SHOT))
  466. BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
  467. if ((flags & PARSE_OPT_ONE_SHOT) &&
  468. (flags & PARSE_OPT_KEEP_ARGV0))
  469. BUG("Can't keep argv0 if you don't have it");
  470. parse_options_check(options);
  471. }
  472. void parse_options_start(struct parse_opt_ctx_t *ctx,
  473. int argc, const char **argv, const char *prefix,
  474. const struct option *options, int flags)
  475. {
  476. memset(ctx, 0, sizeof(*ctx));
  477. parse_options_start_1(ctx, argc, argv, prefix, options, flags);
  478. }
  479. static void show_negated_gitcomp(const struct option *opts, int show_all,
  480. int nr_noopts)
  481. {
  482. int printed_dashdash = 0;
  483. for (; opts->type != OPTION_END; opts++) {
  484. int has_unset_form = 0;
  485. const char *name;
  486. if (!opts->long_name)
  487. continue;
  488. if (!show_all &&
  489. (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
  490. continue;
  491. if (opts->flags & PARSE_OPT_NONEG)
  492. continue;
  493. switch (opts->type) {
  494. case OPTION_STRING:
  495. case OPTION_FILENAME:
  496. case OPTION_INTEGER:
  497. case OPTION_MAGNITUDE:
  498. case OPTION_CALLBACK:
  499. case OPTION_BIT:
  500. case OPTION_NEGBIT:
  501. case OPTION_COUNTUP:
  502. case OPTION_SET_INT:
  503. has_unset_form = 1;
  504. break;
  505. default:
  506. break;
  507. }
  508. if (!has_unset_form)
  509. continue;
  510. if (skip_prefix(opts->long_name, "no-", &name)) {
  511. if (nr_noopts < 0)
  512. printf(" --%s", name);
  513. } else if (nr_noopts >= 0) {
  514. if (nr_noopts && !printed_dashdash) {
  515. printf(" --");
  516. printed_dashdash = 1;
  517. }
  518. printf(" --no-%s", opts->long_name);
  519. nr_noopts++;
  520. }
  521. }
  522. }
  523. static int show_gitcomp(const struct option *opts, int show_all)
  524. {
  525. const struct option *original_opts = opts;
  526. int nr_noopts = 0;
  527. for (; opts->type != OPTION_END; opts++) {
  528. const char *suffix = "";
  529. if (!opts->long_name)
  530. continue;
  531. if (!show_all &&
  532. (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
  533. continue;
  534. switch (opts->type) {
  535. case OPTION_GROUP:
  536. continue;
  537. case OPTION_STRING:
  538. case OPTION_FILENAME:
  539. case OPTION_INTEGER:
  540. case OPTION_MAGNITUDE:
  541. case OPTION_CALLBACK:
  542. if (opts->flags & PARSE_OPT_NOARG)
  543. break;
  544. if (opts->flags & PARSE_OPT_OPTARG)
  545. break;
  546. if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
  547. break;
  548. suffix = "=";
  549. break;
  550. default:
  551. break;
  552. }
  553. if (opts->flags & PARSE_OPT_COMP_ARG)
  554. suffix = "=";
  555. if (starts_with(opts->long_name, "no-"))
  556. nr_noopts++;
  557. printf(" --%s%s", opts->long_name, suffix);
  558. }
  559. show_negated_gitcomp(original_opts, show_all, -1);
  560. show_negated_gitcomp(original_opts, show_all, nr_noopts);
  561. fputc('\n', stdout);
  562. return PARSE_OPT_COMPLETE;
  563. }
  564. /*
  565. * Scan and may produce a new option[] array, which should be used
  566. * instead of the original 'options'.
  567. *
  568. * Right now this is only used to preprocess and substitute
  569. * OPTION_ALIAS.
  570. */
  571. static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
  572. const struct option *options)
  573. {
  574. struct option *newopt;
  575. int i, nr, alias;
  576. int nr_aliases = 0;
  577. for (nr = 0; options[nr].type != OPTION_END; nr++) {
  578. if (options[nr].type == OPTION_ALIAS)
  579. nr_aliases++;
  580. }
  581. if (!nr_aliases)
  582. return NULL;
  583. ALLOC_ARRAY(newopt, nr + 1);
  584. COPY_ARRAY(newopt, options, nr + 1);
  585. /* each alias has two string pointers and NULL */
  586. CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
  587. for (alias = 0, i = 0; i < nr; i++) {
  588. int short_name;
  589. const char *long_name;
  590. const char *source;
  591. struct strbuf help = STRBUF_INIT;
  592. int j;
  593. if (newopt[i].type != OPTION_ALIAS)
  594. continue;
  595. short_name = newopt[i].short_name;
  596. long_name = newopt[i].long_name;
  597. source = newopt[i].value;
  598. if (!long_name)
  599. BUG("An alias must have long option name");
  600. strbuf_addf(&help, _("alias of --%s"), source);
  601. for (j = 0; j < nr; j++) {
  602. const char *name = options[j].long_name;
  603. if (!name || strcmp(name, source))
  604. continue;
  605. if (options[j].type == OPTION_ALIAS)
  606. BUG("No please. Nested aliases are not supported.");
  607. memcpy(newopt + i, options + j, sizeof(*newopt));
  608. newopt[i].short_name = short_name;
  609. newopt[i].long_name = long_name;
  610. newopt[i].help = strbuf_detach(&help, NULL);
  611. break;
  612. }
  613. if (j == nr)
  614. BUG("could not find source option '%s' of alias '%s'",
  615. source, newopt[i].long_name);
  616. ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
  617. ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
  618. ctx->alias_groups[alias * 3 + 2] = NULL;
  619. alias++;
  620. }
  621. return newopt;
  622. }
  623. static int usage_with_options_internal(struct parse_opt_ctx_t *,
  624. const char * const *,
  625. const struct option *, int, int);
  626. int parse_options_step(struct parse_opt_ctx_t *ctx,
  627. const struct option *options,
  628. const char * const usagestr[])
  629. {
  630. int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  631. /* we must reset ->opt, unknown short option leave it dangling */
  632. ctx->opt = NULL;
  633. for (; ctx->argc; ctx->argc--, ctx->argv++) {
  634. const char *arg = ctx->argv[0];
  635. if (ctx->flags & PARSE_OPT_ONE_SHOT &&
  636. ctx->argc != ctx->total)
  637. break;
  638. if (*arg != '-' || !arg[1]) {
  639. if (parse_nodash_opt(ctx, arg, options) == 0)
  640. continue;
  641. if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
  642. return PARSE_OPT_NON_OPTION;
  643. ctx->out[ctx->cpidx++] = ctx->argv[0];
  644. continue;
  645. }
  646. /* lone -h asks for help */
  647. if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
  648. goto show_usage;
  649. /*
  650. * lone --git-completion-helper and --git-completion-helper-all
  651. * are asked by git-completion.bash
  652. */
  653. if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
  654. return show_gitcomp(options, 0);
  655. if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
  656. return show_gitcomp(options, 1);
  657. if (arg[1] != '-') {
  658. ctx->opt = arg + 1;
  659. switch (parse_short_opt(ctx, options)) {
  660. case PARSE_OPT_ERROR:
  661. return PARSE_OPT_ERROR;
  662. case PARSE_OPT_UNKNOWN:
  663. if (ctx->opt)
  664. check_typos(arg + 1, options);
  665. if (internal_help && *ctx->opt == 'h')
  666. goto show_usage;
  667. goto unknown;
  668. case PARSE_OPT_NON_OPTION:
  669. case PARSE_OPT_HELP:
  670. case PARSE_OPT_COMPLETE:
  671. BUG("parse_short_opt() cannot return these");
  672. case PARSE_OPT_DONE:
  673. break;
  674. }
  675. if (ctx->opt)
  676. check_typos(arg + 1, options);
  677. while (ctx->opt) {
  678. switch (parse_short_opt(ctx, options)) {
  679. case PARSE_OPT_ERROR:
  680. return PARSE_OPT_ERROR;
  681. case PARSE_OPT_UNKNOWN:
  682. if (internal_help && *ctx->opt == 'h')
  683. goto show_usage;
  684. /* fake a short option thing to hide the fact that we may have
  685. * started to parse aggregated stuff
  686. *
  687. * This is leaky, too bad.
  688. */
  689. ctx->argv[0] = xstrdup(ctx->opt - 1);
  690. *(char *)ctx->argv[0] = '-';
  691. goto unknown;
  692. case PARSE_OPT_NON_OPTION:
  693. case PARSE_OPT_COMPLETE:
  694. case PARSE_OPT_HELP:
  695. BUG("parse_short_opt() cannot return these");
  696. case PARSE_OPT_DONE:
  697. break;
  698. }
  699. }
  700. continue;
  701. }
  702. if (!arg[2] /* "--" */ ||
  703. !strcmp(arg + 2, "end-of-options")) {
  704. if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
  705. ctx->argc--;
  706. ctx->argv++;
  707. }
  708. break;
  709. }
  710. if (internal_help && !strcmp(arg + 2, "help-all"))
  711. return usage_with_options_internal(ctx, usagestr, options, 1, 0);
  712. if (internal_help && !strcmp(arg + 2, "help"))
  713. goto show_usage;
  714. switch (parse_long_opt(ctx, arg + 2, options)) {
  715. case PARSE_OPT_ERROR:
  716. return PARSE_OPT_ERROR;
  717. case PARSE_OPT_UNKNOWN:
  718. goto unknown;
  719. case PARSE_OPT_HELP:
  720. goto show_usage;
  721. case PARSE_OPT_NON_OPTION:
  722. case PARSE_OPT_COMPLETE:
  723. BUG("parse_long_opt() cannot return these");
  724. case PARSE_OPT_DONE:
  725. break;
  726. }
  727. continue;
  728. unknown:
  729. if (ctx->flags & PARSE_OPT_ONE_SHOT)
  730. break;
  731. if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
  732. return PARSE_OPT_UNKNOWN;
  733. ctx->out[ctx->cpidx++] = ctx->argv[0];
  734. ctx->opt = NULL;
  735. }
  736. return PARSE_OPT_DONE;
  737. show_usage:
  738. return usage_with_options_internal(ctx, usagestr, options, 0, 0);
  739. }
  740. int parse_options_end(struct parse_opt_ctx_t *ctx)
  741. {
  742. if (ctx->flags & PARSE_OPT_ONE_SHOT)
  743. return ctx->total - ctx->argc;
  744. MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
  745. ctx->out[ctx->cpidx + ctx->argc] = NULL;
  746. return ctx->cpidx + ctx->argc;
  747. }
  748. int parse_options(int argc, const char **argv, const char *prefix,
  749. const struct option *options, const char * const usagestr[],
  750. int flags)
  751. {
  752. struct parse_opt_ctx_t ctx;
  753. struct option *real_options;
  754. disallow_abbreviated_options =
  755. git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
  756. memset(&ctx, 0, sizeof(ctx));
  757. real_options = preprocess_options(&ctx, options);
  758. if (real_options)
  759. options = real_options;
  760. parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
  761. switch (parse_options_step(&ctx, options, usagestr)) {
  762. case PARSE_OPT_HELP:
  763. case PARSE_OPT_ERROR:
  764. exit(129);
  765. case PARSE_OPT_COMPLETE:
  766. exit(0);
  767. case PARSE_OPT_NON_OPTION:
  768. case PARSE_OPT_DONE:
  769. break;
  770. default: /* PARSE_OPT_UNKNOWN */
  771. if (ctx.argv[0][1] == '-') {
  772. error(_("unknown option `%s'"), ctx.argv[0] + 2);
  773. } else if (isascii(*ctx.opt)) {
  774. error(_("unknown switch `%c'"), *ctx.opt);
  775. } else {
  776. error(_("unknown non-ascii option in string: `%s'"),
  777. ctx.argv[0]);
  778. }
  779. usage_with_options(usagestr, options);
  780. }
  781. precompose_argv(argc, argv);
  782. free(real_options);
  783. free(ctx.alias_groups);
  784. return parse_options_end(&ctx);
  785. }
  786. static int usage_argh(const struct option *opts, FILE *outfile)
  787. {
  788. const char *s;
  789. int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
  790. !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
  791. if (opts->flags & PARSE_OPT_OPTARG)
  792. if (opts->long_name)
  793. s = literal ? "[=%s]" : "[=<%s>]";
  794. else
  795. s = literal ? "[%s]" : "[<%s>]";
  796. else
  797. s = literal ? " %s" : " <%s>";
  798. return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
  799. }
  800. #define USAGE_OPTS_WIDTH 24
  801. #define USAGE_GAP 2
  802. static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
  803. const char * const *usagestr,
  804. const struct option *opts, int full, int err)
  805. {
  806. FILE *outfile = err ? stderr : stdout;
  807. int need_newline;
  808. if (!usagestr)
  809. return PARSE_OPT_HELP;
  810. if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
  811. fprintf(outfile, "cat <<\\EOF\n");
  812. fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
  813. while (*usagestr && **usagestr)
  814. /*
  815. * TRANSLATORS: the colon here should align with the
  816. * one in "usage: %s" translation.
  817. */
  818. fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
  819. while (*usagestr) {
  820. if (**usagestr)
  821. fprintf_ln(outfile, _(" %s"), _(*usagestr));
  822. else
  823. fputc('\n', outfile);
  824. usagestr++;
  825. }
  826. need_newline = 1;
  827. for (; opts->type != OPTION_END; opts++) {
  828. size_t pos;
  829. int pad;
  830. if (opts->type == OPTION_GROUP) {
  831. fputc('\n', outfile);
  832. need_newline = 0;
  833. if (*opts->help)
  834. fprintf(outfile, "%s\n", _(opts->help));
  835. continue;
  836. }
  837. if (!full && (opts->flags & PARSE_OPT_HIDDEN))
  838. continue;
  839. if (need_newline) {
  840. fputc('\n', outfile);
  841. need_newline = 0;
  842. }
  843. pos = fprintf(outfile, " ");
  844. if (opts->short_name) {
  845. if (opts->flags & PARSE_OPT_NODASH)
  846. pos += fprintf(outfile, "%c", opts->short_name);
  847. else
  848. pos += fprintf(outfile, "-%c", opts->short_name);
  849. }
  850. if (opts->long_name && opts->short_name)
  851. pos += fprintf(outfile, ", ");
  852. if (opts->long_name)
  853. pos += fprintf(outfile, "--%s", opts->long_name);
  854. if (opts->type == OPTION_NUMBER)
  855. pos += utf8_fprintf(outfile, _("-NUM"));
  856. if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
  857. !(opts->flags & PARSE_OPT_NOARG))
  858. pos += usage_argh(opts, outfile);
  859. if (pos <= USAGE_OPTS_WIDTH)
  860. pad = USAGE_OPTS_WIDTH - pos;
  861. else {
  862. fputc('\n', outfile);
  863. pad = USAGE_OPTS_WIDTH;
  864. }
  865. if (opts->type == OPTION_ALIAS) {
  866. fprintf(outfile, "%*s", pad + USAGE_GAP, "");
  867. fprintf_ln(outfile, _("alias of --%s"),
  868. (const char *)opts->value);
  869. continue;
  870. }
  871. fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
  872. }
  873. fputc('\n', outfile);
  874. if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
  875. fputs("EOF\n", outfile);
  876. return PARSE_OPT_HELP;
  877. }
  878. void NORETURN usage_with_options(const char * const *usagestr,
  879. const struct option *opts)
  880. {
  881. usage_with_options_internal(NULL, usagestr, opts, 0, 1);
  882. exit(129);
  883. }
  884. void NORETURN usage_msg_opt(const char *msg,
  885. const char * const *usagestr,
  886. const struct option *options)
  887. {
  888. fprintf(stderr, "fatal: %s\n\n", msg);
  889. usage_with_options(usagestr, options);
  890. }
  891. const char *optname(const struct option *opt, int flags)
  892. {
  893. static struct strbuf sb = STRBUF_INIT;
  894. strbuf_reset(&sb);
  895. if (flags & OPT_SHORT)
  896. strbuf_addf(&sb, "switch `%c'", opt->short_name);
  897. else if (flags & OPT_UNSET)
  898. strbuf_addf(&sb, "option `no-%s'", opt->long_name);
  899. else
  900. strbuf_addf(&sb, "option `%s'", opt->long_name);
  901. return sb.buf;
  902. }