]>
Commit | Line | Data |
---|---|---|
4a59fd13 PH |
1 | #include "git-compat-util.h" |
2 | #include "parse-options.h" | |
0b027f6c | 3 | #include "abspath.h" |
b2141fc1 | 4 | #include "config.h" |
269defdf | 5 | #include "commit.h" |
73e9da01 | 6 | #include "color.h" |
f394e093 | 7 | #include "gettext.h" |
d4a4f929 | 8 | #include "strbuf.h" |
c0821965 | 9 | #include "utf8.h" |
4a59fd13 | 10 | |
b02e7d5d JS |
11 | static int disallow_abbreviated_options; |
12 | ||
d3428345 ÆAB |
13 | enum opt_parsed { |
14 | OPT_LONG = 0, | |
15 | OPT_SHORT = 1<<0, | |
16 | OPT_UNSET = 1<<1, | |
17 | }; | |
4a59fd13 | 18 | |
53ca5694 | 19 | static void optbug(const struct option *opt, const char *reason) |
1e5ce570 | 20 | { |
53ca5694 ÆAB |
21 | if (opt->long_name && opt->short_name) |
22 | bug("switch '%c' (--%s) %s", opt->short_name, | |
23 | opt->long_name, reason); | |
24 | else if (opt->long_name) | |
25 | bug("option '%s' %s", opt->long_name, reason); | |
26 | else | |
27 | bug("switch '%c' %s", opt->short_name, reason); | |
1e5ce570 JN |
28 | } |
29 | ||
d3428345 | 30 | static const char *optname(const struct option *opt, enum opt_parsed flags) |
3c2047a7 ÆAB |
31 | { |
32 | static struct strbuf sb = STRBUF_INIT; | |
33 | ||
34 | strbuf_reset(&sb); | |
35 | if (flags & OPT_SHORT) | |
36 | strbuf_addf(&sb, "switch `%c'", opt->short_name); | |
37 | else if (flags & OPT_UNSET) | |
38 | strbuf_addf(&sb, "option `no-%s'", opt->long_name); | |
d3428345 | 39 | else if (flags == OPT_LONG) |
3c2047a7 | 40 | strbuf_addf(&sb, "option `%s'", opt->long_name); |
d3428345 ÆAB |
41 | else |
42 | BUG("optname() got unknown flags %d", flags); | |
3c2047a7 ÆAB |
43 | |
44 | return sb.buf; | |
45 | } | |
46 | ||
f41179f1 NTND |
47 | static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, |
48 | const struct option *opt, | |
d3428345 | 49 | enum opt_parsed flags, const char **arg) |
1cc6985c PH |
50 | { |
51 | if (p->opt) { | |
52 | *arg = p->opt; | |
53 | p->opt = NULL; | |
54 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { | |
55 | *arg = (const char *)opt->defval; | |
d5d745f9 | 56 | } else if (p->argc > 1) { |
1cc6985c PH |
57 | p->argc--; |
58 | *arg = *++p->argv; | |
59 | } else | |
9440b831 | 60 | return error(_("%s requires a value"), optname(opt, flags)); |
1cc6985c PH |
61 | return 0; |
62 | } | |
63 | ||
7ce4088a | 64 | static void fix_filename(const char *prefix, char **file) |
df217ed6 | 65 | { |
7ce4088a JK |
66 | if (!file || !*file) |
67 | ; /* leave as NULL */ | |
7ce4088a | 68 | else |
0bbe1031 | 69 | *file = prefix_filename_except_for_dash(prefix, *file); |
df217ed6 SB |
70 | } |
71 | ||
f41179f1 NTND |
72 | static enum parse_opt_result opt_command_mode_error( |
73 | const struct option *opt, | |
74 | const struct option *all_opts, | |
d3428345 | 75 | enum opt_parsed flags) |
11588263 JH |
76 | { |
77 | const struct option *that; | |
11588263 JH |
78 | struct strbuf that_name = STRBUF_INIT; |
79 | ||
80 | /* | |
81 | * Find the other option that was used to set the variable | |
82 | * already, and report that this is not compatible with it. | |
83 | */ | |
84 | for (that = all_opts; that->type != OPTION_END; that++) { | |
85 | if (that == opt || | |
bc8620b4 | 86 | !(that->flags & PARSE_OPT_CMDMODE) || |
11588263 JH |
87 | that->value != opt->value || |
88 | that->defval != *(int *)opt->value) | |
89 | continue; | |
90 | ||
91 | if (that->long_name) | |
92 | strbuf_addf(&that_name, "--%s", that->long_name); | |
93 | else | |
94 | strbuf_addf(&that_name, "-%c", that->short_name); | |
9440b831 NTND |
95 | error(_("%s is incompatible with %s"), |
96 | optname(opt, flags), that_name.buf); | |
11588263 | 97 | strbuf_release(&that_name); |
f41179f1 | 98 | return PARSE_OPT_ERROR; |
11588263 | 99 | } |
9440b831 NTND |
100 | return error(_("%s : incompatible with something else"), |
101 | optname(opt, flags)); | |
11588263 JH |
102 | } |
103 | ||
f41179f1 NTND |
104 | static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, |
105 | const struct option *opt, | |
106 | const struct option *all_opts, | |
d3428345 | 107 | enum opt_parsed flags) |
4a59fd13 | 108 | { |
ffe659f9 | 109 | const char *s, *arg; |
db7244bd | 110 | const int unset = flags & OPT_UNSET; |
df217ed6 | 111 | int err; |
4a59fd13 | 112 | |
db7244bd | 113 | if (unset && p->opt) |
9440b831 | 114 | return error(_("%s takes no value"), optname(opt, flags)); |
db7244bd | 115 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
9440b831 | 116 | return error(_("%s isn't available"), optname(opt, flags)); |
c1f4ec9e | 117 | if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) |
9440b831 | 118 | return error(_("%s takes no value"), optname(opt, flags)); |
db7244bd | 119 | |
bc8620b4 PB |
120 | /* |
121 | * Giving the same mode option twice, although unnecessary, | |
122 | * is not a grave error, so let it pass. | |
123 | */ | |
124 | if ((opt->flags & PARSE_OPT_CMDMODE) && | |
125 | *(int *)opt->value && *(int *)opt->value != opt->defval) | |
126 | return opt_command_mode_error(opt, all_opts, flags); | |
127 | ||
db7244bd | 128 | switch (opt->type) { |
b0b3a8b6 | 129 | case OPTION_LOWLEVEL_CALLBACK: |
3ebbe289 | 130 | return opt->ll_callback(p, opt, NULL, unset); |
b0b3a8b6 | 131 | |
db7244bd PH |
132 | case OPTION_BIT: |
133 | if (unset) | |
134 | *(int *)opt->value &= ~opt->defval; | |
4a59fd13 | 135 | else |
db7244bd PH |
136 | *(int *)opt->value |= opt->defval; |
137 | return 0; | |
138 | ||
2f4b97f9 RS |
139 | case OPTION_NEGBIT: |
140 | if (unset) | |
141 | *(int *)opt->value |= opt->defval; | |
142 | else | |
143 | *(int *)opt->value &= ~opt->defval; | |
144 | return 0; | |
145 | ||
f62470c6 NTND |
146 | case OPTION_BITOP: |
147 | if (unset) | |
148 | BUG("BITOP can't have unset form"); | |
149 | *(int *)opt->value &= ~opt->extra; | |
150 | *(int *)opt->value |= opt->defval; | |
151 | return 0; | |
152 | ||
b04ba2bb | 153 | case OPTION_COUNTUP: |
e0070e8b PB |
154 | if (*(int *)opt->value < 0) |
155 | *(int *)opt->value = 0; | |
db7244bd PH |
156 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
157 | return 0; | |
158 | ||
159 | case OPTION_SET_INT: | |
160 | *(int *)opt->value = unset ? 0 : opt->defval; | |
161 | return 0; | |
162 | ||
4a59fd13 | 163 | case OPTION_STRING: |
1cc6985c | 164 | if (unset) |
db7244bd | 165 | *(const char **)opt->value = NULL; |
1cc6985c | 166 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
ffe659f9 | 167 | *(const char **)opt->value = (const char *)opt->defval; |
1cc6985c PH |
168 | else |
169 | return get_arg(p, opt, flags, (const char **)opt->value); | |
4a59fd13 PH |
170 | return 0; |
171 | ||
df217ed6 SB |
172 | case OPTION_FILENAME: |
173 | err = 0; | |
174 | if (unset) | |
175 | *(const char **)opt->value = NULL; | |
176 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) | |
177 | *(const char **)opt->value = (const char *)opt->defval; | |
178 | else | |
179 | err = get_arg(p, opt, flags, (const char **)opt->value); | |
180 | ||
181 | if (!err) | |
7ce4088a | 182 | fix_filename(p->prefix, (char **)opt->value); |
df217ed6 SB |
183 | return err; |
184 | ||
ffe659f9 | 185 | case OPTION_CALLBACK: |
3ebbe289 NTND |
186 | { |
187 | const char *p_arg = NULL; | |
188 | int p_unset; | |
189 | ||
db7244bd | 190 | if (unset) |
3ebbe289 NTND |
191 | p_unset = 1; |
192 | else if (opt->flags & PARSE_OPT_NOARG) | |
193 | p_unset = 0; | |
194 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) | |
195 | p_unset = 0; | |
196 | else if (get_arg(p, opt, flags, &arg)) | |
1cc6985c | 197 | return -1; |
3ebbe289 NTND |
198 | else { |
199 | p_unset = 0; | |
200 | p_arg = arg; | |
201 | } | |
202 | if (opt->callback) | |
203 | return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0; | |
204 | else | |
205 | return (*opt->ll_callback)(p, opt, p_arg, p_unset); | |
206 | } | |
4a59fd13 | 207 | case OPTION_INTEGER: |
db7244bd | 208 | if (unset) { |
4a59fd13 PH |
209 | *(int *)opt->value = 0; |
210 | return 0; | |
211 | } | |
c43a2483 | 212 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
ffe659f9 PH |
213 | *(int *)opt->value = opt->defval; |
214 | return 0; | |
215 | } | |
1cc6985c PH |
216 | if (get_arg(p, opt, flags, &arg)) |
217 | return -1; | |
f7e68a08 NTND |
218 | if (!*arg) |
219 | return error(_("%s expects a numerical value"), | |
220 | optname(opt, flags)); | |
1cc6985c | 221 | *(int *)opt->value = strtol(arg, (char **)&s, 10); |
4a59fd13 | 222 | if (*s) |
9440b831 NTND |
223 | return error(_("%s expects a numerical value"), |
224 | optname(opt, flags)); | |
4a59fd13 PH |
225 | return 0; |
226 | ||
2a514ed8 CB |
227 | case OPTION_MAGNITUDE: |
228 | if (unset) { | |
229 | *(unsigned long *)opt->value = 0; | |
230 | return 0; | |
231 | } | |
232 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { | |
233 | *(unsigned long *)opt->value = opt->defval; | |
234 | return 0; | |
235 | } | |
236 | if (get_arg(p, opt, flags, &arg)) | |
237 | return -1; | |
238 | if (!git_parse_ulong(arg, opt->value)) | |
9440b831 NTND |
239 | return error(_("%s expects a non-negative integer value" |
240 | " with an optional k/m/g suffix"), | |
241 | optname(opt, flags)); | |
2a514ed8 CB |
242 | return 0; |
243 | ||
4a59fd13 | 244 | default: |
48a5499e | 245 | BUG("opt->type %d should not happen", opt->type); |
4a59fd13 PH |
246 | } |
247 | } | |
248 | ||
f41179f1 NTND |
249 | static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p, |
250 | const struct option *options) | |
4a59fd13 | 251 | { |
11588263 | 252 | const struct option *all_opts = options; |
e0319ff5 RS |
253 | const struct option *numopt = NULL; |
254 | ||
4a59fd13 PH |
255 | for (; options->type != OPTION_END; options++) { |
256 | if (options->short_name == *p->opt) { | |
257 | p->opt = p->opt[1] ? p->opt + 1 : NULL; | |
11588263 | 258 | return get_value(p, options, all_opts, OPT_SHORT); |
4a59fd13 | 259 | } |
e0319ff5 RS |
260 | |
261 | /* | |
262 | * Handle the numerical option later, explicit one-digit | |
263 | * options take precedence over it. | |
264 | */ | |
265 | if (options->type == OPTION_NUMBER) | |
266 | numopt = options; | |
267 | } | |
268 | if (numopt && isdigit(*p->opt)) { | |
269 | size_t len = 1; | |
270 | char *arg; | |
271 | int rc; | |
272 | ||
273 | while (isdigit(p->opt[len])) | |
274 | len++; | |
275 | arg = xmemdupz(p->opt, len); | |
276 | p->opt = p->opt[len] ? p->opt + len : NULL; | |
3ebbe289 NTND |
277 | if (numopt->callback) |
278 | rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; | |
279 | else | |
280 | rc = (*numopt->ll_callback)(p, numopt, arg, 0); | |
e0319ff5 RS |
281 | free(arg); |
282 | return rc; | |
4a59fd13 | 283 | } |
f41179f1 | 284 | return PARSE_OPT_UNKNOWN; |
4a59fd13 PH |
285 | } |
286 | ||
5c387428 NTND |
287 | static int has_string(const char *it, const char **array) |
288 | { | |
289 | while (*array) | |
290 | if (!strcmp(it, *(array++))) | |
291 | return 1; | |
292 | return 0; | |
293 | } | |
294 | ||
295 | static int is_alias(struct parse_opt_ctx_t *ctx, | |
296 | const struct option *one_opt, | |
297 | const struct option *another_opt) | |
298 | { | |
299 | const char **group; | |
300 | ||
301 | if (!ctx->alias_groups) | |
302 | return 0; | |
303 | ||
304 | if (!one_opt->long_name || !another_opt->long_name) | |
305 | return 0; | |
306 | ||
307 | for (group = ctx->alias_groups; *group; group += 3) { | |
308 | /* it and other are from the same family? */ | |
309 | if (has_string(one_opt->long_name, group) && | |
310 | has_string(another_opt->long_name, group)) | |
311 | return 1; | |
312 | } | |
313 | return 0; | |
314 | } | |
315 | ||
f41179f1 NTND |
316 | static enum parse_opt_result parse_long_opt( |
317 | struct parse_opt_ctx_t *p, const char *arg, | |
318 | const struct option *options) | |
4a59fd13 | 319 | { |
11588263 | 320 | const struct option *all_opts = options; |
2c5495f7 | 321 | const char *arg_end = strchrnul(arg, '='); |
243e0614 | 322 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
d3428345 | 323 | enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG; |
7f275b91 | 324 | |
4a59fd13 | 325 | for (; options->type != OPTION_END; options++) { |
0f1930c5 | 326 | const char *rest, *long_name = options->long_name; |
d3428345 | 327 | enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG; |
4a59fd13 | 328 | |
fa83cc83 SG |
329 | if (options->type == OPTION_SUBCOMMAND) |
330 | continue; | |
0f1930c5 | 331 | if (!long_name) |
4a59fd13 PH |
332 | continue; |
333 | ||
0f1930c5 | 334 | again: |
cf4fff57 JK |
335 | if (!skip_prefix(arg, long_name, &rest)) |
336 | rest = NULL; | |
4a59fd13 | 337 | if (!rest) { |
7f275b91 | 338 | /* abbreviated? */ |
99d86d60 | 339 | if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) && |
baa4adc6 | 340 | !strncmp(long_name, arg, arg_end - arg)) { |
7f275b91 | 341 | is_abbreviated: |
5c387428 NTND |
342 | if (abbrev_option && |
343 | !is_alias(p, abbrev_option, options)) { | |
243e0614 JS |
344 | /* |
345 | * If this is abbreviated, it is | |
346 | * ambiguous. So when there is no | |
347 | * exact match later, we need to | |
348 | * error out. | |
349 | */ | |
350 | ambiguous_option = abbrev_option; | |
351 | ambiguous_flags = abbrev_flags; | |
352 | } | |
7f275b91 JS |
353 | if (!(flags & OPT_UNSET) && *arg_end) |
354 | p->opt = arg_end + 1; | |
355 | abbrev_option = options; | |
0f1930c5 | 356 | abbrev_flags = flags ^ opt_flags; |
7f275b91 JS |
357 | continue; |
358 | } | |
6bbfd1fa AS |
359 | /* negation allowed? */ |
360 | if (options->flags & PARSE_OPT_NONEG) | |
361 | continue; | |
7f275b91 | 362 | /* negated and abbreviated very much? */ |
59556548 | 363 | if (starts_with("no-", arg)) { |
7f275b91 JS |
364 | flags |= OPT_UNSET; |
365 | goto is_abbreviated; | |
366 | } | |
367 | /* negated? */ | |
59556548 | 368 | if (!starts_with(arg, "no-")) { |
145136a9 | 369 | if (skip_prefix(long_name, "no-", &long_name)) { |
0f1930c5 RS |
370 | opt_flags |= OPT_UNSET; |
371 | goto again; | |
372 | } | |
4a59fd13 | 373 | continue; |
0f1930c5 | 374 | } |
4a59fd13 | 375 | flags |= OPT_UNSET; |
cf4fff57 JK |
376 | if (!skip_prefix(arg + 3, long_name, &rest)) { |
377 | /* abbreviated and negated? */ | |
378 | if (starts_with(long_name, arg + 3)) | |
379 | goto is_abbreviated; | |
380 | else | |
381 | continue; | |
382 | } | |
4a59fd13 PH |
383 | } |
384 | if (*rest) { | |
385 | if (*rest != '=') | |
386 | continue; | |
387 | p->opt = rest + 1; | |
388 | } | |
11588263 | 389 | return get_value(p, options, all_opts, flags ^ opt_flags); |
4a59fd13 | 390 | } |
243e0614 | 391 | |
b02e7d5d JS |
392 | if (disallow_abbreviated_options && (ambiguous_option || abbrev_option)) |
393 | die("disallowed abbreviated or ambiguous option '%.*s'", | |
394 | (int)(arg_end - arg), arg); | |
395 | ||
3bb0923f | 396 | if (ambiguous_option) { |
89003426 NTND |
397 | error(_("ambiguous option: %s " |
398 | "(could be --%s%s or --%s%s)"), | |
243e0614 JS |
399 | arg, |
400 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", | |
401 | ambiguous_option->long_name, | |
402 | (abbrev_flags & OPT_UNSET) ? "no-" : "", | |
403 | abbrev_option->long_name); | |
f41179f1 | 404 | return PARSE_OPT_HELP; |
3bb0923f | 405 | } |
7f275b91 | 406 | if (abbrev_option) |
11588263 | 407 | return get_value(p, abbrev_option, all_opts, abbrev_flags); |
f41179f1 | 408 | return PARSE_OPT_UNKNOWN; |
4a59fd13 PH |
409 | } |
410 | ||
68611f51 ÆAB |
411 | static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p, |
412 | const char *arg, | |
413 | const struct option *options) | |
51a9949e | 414 | { |
11588263 JH |
415 | const struct option *all_opts = options; |
416 | ||
51a9949e RS |
417 | for (; options->type != OPTION_END; options++) { |
418 | if (!(options->flags & PARSE_OPT_NODASH)) | |
419 | continue; | |
51a9949e | 420 | if (options->short_name == arg[0] && arg[1] == '\0') |
11588263 | 421 | return get_value(p, options, all_opts, OPT_SHORT); |
51a9949e | 422 | } |
68611f51 | 423 | return PARSE_OPT_ERROR; |
51a9949e RS |
424 | } |
425 | ||
fa83cc83 SG |
426 | static enum parse_opt_result parse_subcommand(const char *arg, |
427 | const struct option *options) | |
428 | { | |
429 | for (; options->type != OPTION_END; options++) | |
430 | if (options->type == OPTION_SUBCOMMAND && | |
431 | !strcmp(options->long_name, arg)) { | |
432 | *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn; | |
433 | return PARSE_OPT_SUBCOMMAND; | |
434 | } | |
435 | ||
436 | return PARSE_OPT_UNKNOWN; | |
437 | } | |
438 | ||
1121a878 | 439 | static void check_typos(const char *arg, const struct option *options) |
3a9f0f41 PH |
440 | { |
441 | if (strlen(arg) < 3) | |
442 | return; | |
443 | ||
59556548 | 444 | if (starts_with(arg, "no-")) { |
395518cf | 445 | error(_("did you mean `--%s` (with two dashes)?"), arg); |
3a9f0f41 PH |
446 | exit(129); |
447 | } | |
448 | ||
449 | for (; options->type != OPTION_END; options++) { | |
450 | if (!options->long_name) | |
451 | continue; | |
59556548 | 452 | if (starts_with(options->long_name, arg)) { |
395518cf | 453 | error(_("did you mean `--%s` (with two dashes)?"), arg); |
3a9f0f41 PH |
454 | exit(129); |
455 | } | |
456 | } | |
457 | } | |
458 | ||
cb9d398c PH |
459 | static void parse_options_check(const struct option *opts) |
460 | { | |
af465af8 | 461 | char short_opts[128]; |
fa83cc83 | 462 | void *subcommand_value = NULL; |
cb9d398c | 463 | |
af465af8 | 464 | memset(short_opts, '\0', sizeof(short_opts)); |
cb9d398c PH |
465 | for (; opts->type != OPTION_END; opts++) { |
466 | if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && | |
1e5ce570 | 467 | (opts->flags & PARSE_OPT_OPTARG)) |
53ca5694 ÆAB |
468 | optbug(opts, "uses incompatible flags " |
469 | "LASTARG_DEFAULT and OPTARG"); | |
af465af8 JH |
470 | if (opts->short_name) { |
471 | if (0x7F <= opts->short_name) | |
53ca5694 | 472 | optbug(opts, "invalid short name"); |
af465af8 | 473 | else if (short_opts[opts->short_name]++) |
53ca5694 | 474 | optbug(opts, "short name already used"); |
af465af8 | 475 | } |
a02dd4ff JN |
476 | if (opts->flags & PARSE_OPT_NODASH && |
477 | ((opts->flags & PARSE_OPT_OPTARG) || | |
478 | !(opts->flags & PARSE_OPT_NOARG) || | |
479 | !(opts->flags & PARSE_OPT_NONEG) || | |
480 | opts->long_name)) | |
53ca5694 ÆAB |
481 | optbug(opts, "uses feature " |
482 | "not supported for dashless options"); | |
5c400ed2 | 483 | switch (opts->type) { |
b04ba2bb | 484 | case OPTION_COUNTUP: |
5c400ed2 JN |
485 | case OPTION_BIT: |
486 | case OPTION_NEGBIT: | |
487 | case OPTION_SET_INT: | |
5c400ed2 JN |
488 | case OPTION_NUMBER: |
489 | if ((opts->flags & PARSE_OPT_OPTARG) || | |
490 | !(opts->flags & PARSE_OPT_NOARG)) | |
53ca5694 | 491 | optbug(opts, "should not accept an argument"); |
bf3ff338 NTND |
492 | break; |
493 | case OPTION_CALLBACK: | |
3ebbe289 | 494 | if (!opts->callback && !opts->ll_callback) |
5b2f5d92 ÆAB |
495 | optbug(opts, "OPTION_CALLBACK needs one callback"); |
496 | else if (opts->callback && opts->ll_callback) | |
497 | optbug(opts, "OPTION_CALLBACK can't have two callbacks"); | |
bf3ff338 NTND |
498 | break; |
499 | case OPTION_LOWLEVEL_CALLBACK: | |
500 | if (!opts->ll_callback) | |
5b2f5d92 | 501 | optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback"); |
bf3ff338 | 502 | if (opts->callback) |
5b2f5d92 | 503 | optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback"); |
bf3ff338 | 504 | break; |
5c387428 | 505 | case OPTION_ALIAS: |
5b2f5d92 ÆAB |
506 | optbug(opts, "OPT_ALIAS() should not remain at this point. " |
507 | "Are you using parse_options_step() directly?\n" | |
508 | "That case is not supported yet."); | |
509 | break; | |
fa83cc83 SG |
510 | case OPTION_SUBCOMMAND: |
511 | if (!opts->value || !opts->subcommand_fn) | |
512 | optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function"); | |
513 | if (!subcommand_value) | |
514 | subcommand_value = opts->value; | |
515 | else if (subcommand_value != opts->value) | |
516 | optbug(opts, "all OPTION_SUBCOMMANDs need the same value"); | |
517 | break; | |
5c400ed2 JN |
518 | default: |
519 | ; /* ok. (usually accepts an argument) */ | |
520 | } | |
b6c2a0d4 JH |
521 | if (opts->argh && |
522 | strcspn(opts->argh, " _") != strlen(opts->argh)) | |
53ca5694 | 523 | optbug(opts, "multi-word argh should use dash to separate words"); |
cb9d398c | 524 | } |
53ca5694 | 525 | BUG_if_bug("invalid 'struct option'"); |
cb9d398c PH |
526 | } |
527 | ||
fa83cc83 SG |
528 | static int has_subcommands(const struct option *options) |
529 | { | |
530 | for (; options->type != OPTION_END; options++) | |
531 | if (options->type == OPTION_SUBCOMMAND) | |
532 | return 1; | |
533 | return 0; | |
534 | } | |
535 | ||
5c387428 NTND |
536 | static void parse_options_start_1(struct parse_opt_ctx_t *ctx, |
537 | int argc, const char **argv, const char *prefix, | |
3f9ab7cc ÆAB |
538 | const struct option *options, |
539 | enum parse_opt_flags flags) | |
7e7bbcb4 | 540 | { |
202fbb33 NTND |
541 | ctx->argc = argc; |
542 | ctx->argv = argv; | |
543 | if (!(flags & PARSE_OPT_ONE_SHOT)) { | |
544 | ctx->argc--; | |
545 | ctx->argv++; | |
546 | } | |
547 | ctx->total = ctx->argc; | |
548 | ctx->out = argv; | |
37782920 | 549 | ctx->prefix = prefix; |
a32a4eaa | 550 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
7e7bbcb4 | 551 | ctx->flags = flags; |
fa83cc83 SG |
552 | ctx->has_subcommands = has_subcommands(options); |
553 | if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) | |
554 | BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands"); | |
555 | if (ctx->has_subcommands) { | |
556 | if (flags & PARSE_OPT_STOP_AT_NON_OPTION) | |
557 | BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION"); | |
558 | if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) { | |
559 | if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT) | |
560 | BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL"); | |
561 | if (flags & PARSE_OPT_KEEP_DASHDASH) | |
562 | BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL"); | |
563 | } | |
564 | } | |
99d86d60 | 565 | if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) && |
202fbb33 NTND |
566 | (flags & PARSE_OPT_STOP_AT_NON_OPTION) && |
567 | !(flags & PARSE_OPT_ONE_SHOT)) | |
48a5499e | 568 | BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); |
202fbb33 NTND |
569 | if ((flags & PARSE_OPT_ONE_SHOT) && |
570 | (flags & PARSE_OPT_KEEP_ARGV0)) | |
571 | BUG("Can't keep argv0 if you don't have it"); | |
9ca1169f | 572 | parse_options_check(options); |
7e7bbcb4 PH |
573 | } |
574 | ||
5c387428 NTND |
575 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
576 | int argc, const char **argv, const char *prefix, | |
3f9ab7cc ÆAB |
577 | const struct option *options, |
578 | enum parse_opt_flags flags) | |
5c387428 NTND |
579 | { |
580 | memset(ctx, 0, sizeof(*ctx)); | |
581 | parse_options_start_1(ctx, argc, argv, prefix, options, flags); | |
582 | } | |
583 | ||
a0abe5e3 RZ |
584 | static void show_negated_gitcomp(const struct option *opts, int show_all, |
585 | int nr_noopts) | |
b221b5ab NTND |
586 | { |
587 | int printed_dashdash = 0; | |
588 | ||
589 | for (; opts->type != OPTION_END; opts++) { | |
590 | int has_unset_form = 0; | |
591 | const char *name; | |
592 | ||
593 | if (!opts->long_name) | |
594 | continue; | |
a0abe5e3 RZ |
595 | if (!show_all && |
596 | (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))) | |
b221b5ab NTND |
597 | continue; |
598 | if (opts->flags & PARSE_OPT_NONEG) | |
599 | continue; | |
600 | ||
601 | switch (opts->type) { | |
602 | case OPTION_STRING: | |
603 | case OPTION_FILENAME: | |
604 | case OPTION_INTEGER: | |
605 | case OPTION_MAGNITUDE: | |
606 | case OPTION_CALLBACK: | |
607 | case OPTION_BIT: | |
608 | case OPTION_NEGBIT: | |
609 | case OPTION_COUNTUP: | |
610 | case OPTION_SET_INT: | |
611 | has_unset_form = 1; | |
612 | break; | |
613 | default: | |
614 | break; | |
615 | } | |
616 | if (!has_unset_form) | |
617 | continue; | |
618 | ||
619 | if (skip_prefix(opts->long_name, "no-", &name)) { | |
620 | if (nr_noopts < 0) | |
621 | printf(" --%s", name); | |
622 | } else if (nr_noopts >= 0) { | |
623 | if (nr_noopts && !printed_dashdash) { | |
624 | printf(" --"); | |
625 | printed_dashdash = 1; | |
626 | } | |
627 | printf(" --no-%s", opts->long_name); | |
628 | nr_noopts++; | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
a0abe5e3 | 633 | static int show_gitcomp(const struct option *opts, int show_all) |
b9d7f4b4 | 634 | { |
b221b5ab NTND |
635 | const struct option *original_opts = opts; |
636 | int nr_noopts = 0; | |
637 | ||
b9d7f4b4 | 638 | for (; opts->type != OPTION_END; opts++) { |
fa83cc83 | 639 | const char *prefix = "--"; |
b9d7f4b4 NTND |
640 | const char *suffix = ""; |
641 | ||
642 | if (!opts->long_name) | |
643 | continue; | |
a0abe5e3 | 644 | if (!show_all && |
ca2d62b7 | 645 | (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS))) |
b9d7f4b4 NTND |
646 | continue; |
647 | ||
648 | switch (opts->type) { | |
fa83cc83 SG |
649 | case OPTION_SUBCOMMAND: |
650 | prefix = ""; | |
651 | break; | |
b9d7f4b4 NTND |
652 | case OPTION_GROUP: |
653 | continue; | |
654 | case OPTION_STRING: | |
655 | case OPTION_FILENAME: | |
656 | case OPTION_INTEGER: | |
657 | case OPTION_MAGNITUDE: | |
658 | case OPTION_CALLBACK: | |
659 | if (opts->flags & PARSE_OPT_NOARG) | |
660 | break; | |
661 | if (opts->flags & PARSE_OPT_OPTARG) | |
662 | break; | |
663 | if (opts->flags & PARSE_OPT_LASTARG_DEFAULT) | |
664 | break; | |
665 | suffix = "="; | |
666 | break; | |
667 | default: | |
668 | break; | |
669 | } | |
ebc4a04e NTND |
670 | if (opts->flags & PARSE_OPT_COMP_ARG) |
671 | suffix = "="; | |
b221b5ab NTND |
672 | if (starts_with(opts->long_name, "no-")) |
673 | nr_noopts++; | |
fa83cc83 SG |
674 | printf("%s%s%s%s", opts == original_opts ? "" : " ", |
675 | prefix, opts->long_name, suffix); | |
b9d7f4b4 | 676 | } |
a0abe5e3 RZ |
677 | show_negated_gitcomp(original_opts, show_all, -1); |
678 | show_negated_gitcomp(original_opts, show_all, nr_noopts); | |
b9d7f4b4 | 679 | fputc('\n', stdout); |
a92ec7ef | 680 | return PARSE_OPT_COMPLETE; |
b9d7f4b4 NTND |
681 | } |
682 | ||
5c387428 NTND |
683 | /* |
684 | * Scan and may produce a new option[] array, which should be used | |
685 | * instead of the original 'options'. | |
686 | * | |
15beaaa3 | 687 | * Right now this is only used to preprocess and substitute |
5c387428 | 688 | * OPTION_ALIAS. |
64cc539f AH |
689 | * |
690 | * The returned options should be freed using free_preprocessed_options. | |
5c387428 NTND |
691 | */ |
692 | static struct option *preprocess_options(struct parse_opt_ctx_t *ctx, | |
693 | const struct option *options) | |
694 | { | |
695 | struct option *newopt; | |
696 | int i, nr, alias; | |
697 | int nr_aliases = 0; | |
698 | ||
699 | for (nr = 0; options[nr].type != OPTION_END; nr++) { | |
700 | if (options[nr].type == OPTION_ALIAS) | |
701 | nr_aliases++; | |
702 | } | |
703 | ||
704 | if (!nr_aliases) | |
705 | return NULL; | |
706 | ||
6e578410 | 707 | DUP_ARRAY(newopt, options, nr + 1); |
5c387428 NTND |
708 | |
709 | /* each alias has two string pointers and NULL */ | |
710 | CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); | |
711 | ||
712 | for (alias = 0, i = 0; i < nr; i++) { | |
713 | int short_name; | |
714 | const char *long_name; | |
715 | const char *source; | |
7c280589 | 716 | struct strbuf help = STRBUF_INIT; |
5c387428 NTND |
717 | int j; |
718 | ||
719 | if (newopt[i].type != OPTION_ALIAS) | |
720 | continue; | |
721 | ||
722 | short_name = newopt[i].short_name; | |
723 | long_name = newopt[i].long_name; | |
724 | source = newopt[i].value; | |
725 | ||
726 | if (!long_name) | |
727 | BUG("An alias must have long option name"); | |
7c280589 | 728 | strbuf_addf(&help, _("alias of --%s"), source); |
5c387428 NTND |
729 | |
730 | for (j = 0; j < nr; j++) { | |
731 | const char *name = options[j].long_name; | |
732 | ||
733 | if (!name || strcmp(name, source)) | |
734 | continue; | |
735 | ||
736 | if (options[j].type == OPTION_ALIAS) | |
737 | BUG("No please. Nested aliases are not supported."); | |
738 | ||
5c387428 NTND |
739 | memcpy(newopt + i, options + j, sizeof(*newopt)); |
740 | newopt[i].short_name = short_name; | |
741 | newopt[i].long_name = long_name; | |
7c280589 | 742 | newopt[i].help = strbuf_detach(&help, NULL); |
64cc539f | 743 | newopt[i].flags |= PARSE_OPT_FROM_ALIAS; |
5c387428 NTND |
744 | break; |
745 | } | |
746 | ||
747 | if (j == nr) | |
748 | BUG("could not find source option '%s' of alias '%s'", | |
749 | source, newopt[i].long_name); | |
750 | ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name; | |
751 | ctx->alias_groups[alias * 3 + 1] = options[j].long_name; | |
752 | ctx->alias_groups[alias * 3 + 2] = NULL; | |
753 | alias++; | |
754 | } | |
755 | ||
756 | return newopt; | |
757 | } | |
758 | ||
64cc539f AH |
759 | static void free_preprocessed_options(struct option *options) |
760 | { | |
761 | int i; | |
762 | ||
763 | if (!options) | |
764 | return; | |
765 | ||
766 | for (i = 0; options[i].type != OPTION_END; i++) { | |
767 | if (options[i].flags & PARSE_OPT_FROM_ALIAS) | |
768 | free((void *)options[i].help); | |
769 | } | |
770 | free(options); | |
771 | } | |
772 | ||
352e7613 ÆAB |
773 | static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *, |
774 | const char * const *, | |
775 | const struct option *, | |
776 | int, int); | |
dd3bf0f4 | 777 | |
352e7613 ÆAB |
778 | enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, |
779 | const struct option *options, | |
780 | const char * const usagestr[]) | |
4a59fd13 | 781 | { |
b92891f9 RS |
782 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
783 | ||
26141b5b PH |
784 | /* we must reset ->opt, unknown short option leave it dangling */ |
785 | ctx->opt = NULL; | |
786 | ||
ff43ec3e PH |
787 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
788 | const char *arg = ctx->argv[0]; | |
4a59fd13 | 789 | |
202fbb33 NTND |
790 | if (ctx->flags & PARSE_OPT_ONE_SHOT && |
791 | ctx->argc != ctx->total) | |
792 | break; | |
793 | ||
4a59fd13 | 794 | if (*arg != '-' || !arg[1]) { |
51a9949e RS |
795 | if (parse_nodash_opt(ctx, arg, options) == 0) |
796 | continue; | |
fa83cc83 SG |
797 | if (!ctx->has_subcommands) { |
798 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) | |
799 | return PARSE_OPT_NON_OPTION; | |
800 | ctx->out[ctx->cpidx++] = ctx->argv[0]; | |
801 | continue; | |
802 | } | |
803 | switch (parse_subcommand(arg, options)) { | |
804 | case PARSE_OPT_SUBCOMMAND: | |
805 | return PARSE_OPT_SUBCOMMAND; | |
806 | case PARSE_OPT_UNKNOWN: | |
807 | if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) | |
808 | /* | |
809 | * arg is neither a short or long | |
810 | * option nor a subcommand. Since | |
811 | * this command has a default | |
812 | * operation mode, we have to treat | |
813 | * this arg and all remaining args | |
814 | * as args meant to that default | |
815 | * operation mode. | |
816 | * So we are done parsing. | |
817 | */ | |
818 | return PARSE_OPT_DONE; | |
819 | error(_("unknown subcommand: `%s'"), arg); | |
820 | usage_with_options(usagestr, options); | |
821 | case PARSE_OPT_COMPLETE: | |
822 | case PARSE_OPT_HELP: | |
823 | case PARSE_OPT_ERROR: | |
824 | case PARSE_OPT_DONE: | |
825 | case PARSE_OPT_NON_OPTION: | |
826 | /* Impossible. */ | |
827 | BUG("parse_subcommand() cannot return these"); | |
828 | } | |
4a59fd13 PH |
829 | } |
830 | ||
5ad0d3d5 RS |
831 | /* lone -h asks for help */ |
832 | if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h")) | |
833 | goto show_usage; | |
834 | ||
a0abe5e3 RZ |
835 | /* |
836 | * lone --git-completion-helper and --git-completion-helper-all | |
837 | * are asked by git-completion.bash | |
838 | */ | |
839 | if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper")) | |
840 | return show_gitcomp(options, 0); | |
841 | if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all")) | |
842 | return show_gitcomp(options, 1); | |
b9d7f4b4 | 843 | |
4a59fd13 | 844 | if (arg[1] != '-') { |
ff43ec3e | 845 | ctx->opt = arg + 1; |
07fe54db | 846 | switch (parse_short_opt(ctx, options)) { |
f41179f1 | 847 | case PARSE_OPT_ERROR: |
3bb0923f | 848 | return PARSE_OPT_ERROR; |
f41179f1 | 849 | case PARSE_OPT_UNKNOWN: |
38916c5b RS |
850 | if (ctx->opt) |
851 | check_typos(arg + 1, options); | |
5ad0d3d5 RS |
852 | if (internal_help && *ctx->opt == 'h') |
853 | goto show_usage; | |
b5ce3a54 | 854 | goto unknown; |
f41179f1 | 855 | case PARSE_OPT_NON_OPTION: |
fa83cc83 | 856 | case PARSE_OPT_SUBCOMMAND: |
f41179f1 NTND |
857 | case PARSE_OPT_HELP: |
858 | case PARSE_OPT_COMPLETE: | |
859 | BUG("parse_short_opt() cannot return these"); | |
860 | case PARSE_OPT_DONE: | |
861 | break; | |
07fe54db | 862 | } |
ff43ec3e | 863 | if (ctx->opt) |
3a9f0f41 | 864 | check_typos(arg + 1, options); |
ff43ec3e | 865 | while (ctx->opt) { |
07fe54db | 866 | switch (parse_short_opt(ctx, options)) { |
f41179f1 | 867 | case PARSE_OPT_ERROR: |
3bb0923f | 868 | return PARSE_OPT_ERROR; |
f41179f1 | 869 | case PARSE_OPT_UNKNOWN: |
5ad0d3d5 RS |
870 | if (internal_help && *ctx->opt == 'h') |
871 | goto show_usage; | |
872 | ||
26141b5b PH |
873 | /* fake a short option thing to hide the fact that we may have |
874 | * started to parse aggregated stuff | |
875 | * | |
876 | * This is leaky, too bad. | |
877 | */ | |
878 | ctx->argv[0] = xstrdup(ctx->opt - 1); | |
879 | *(char *)ctx->argv[0] = '-'; | |
b5ce3a54 | 880 | goto unknown; |
f41179f1 | 881 | case PARSE_OPT_NON_OPTION: |
fa83cc83 | 882 | case PARSE_OPT_SUBCOMMAND: |
f41179f1 NTND |
883 | case PARSE_OPT_COMPLETE: |
884 | case PARSE_OPT_HELP: | |
885 | BUG("parse_short_opt() cannot return these"); | |
886 | case PARSE_OPT_DONE: | |
887 | break; | |
07fe54db | 888 | } |
3a9f0f41 | 889 | } |
4a59fd13 PH |
890 | continue; |
891 | } | |
892 | ||
51b4594b JK |
893 | if (!arg[2] /* "--" */ || |
894 | !strcmp(arg + 2, "end-of-options")) { | |
ff43ec3e PH |
895 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
896 | ctx->argc--; | |
897 | ctx->argv++; | |
4a59fd13 PH |
898 | } |
899 | break; | |
900 | } | |
901 | ||
b92891f9 | 902 | if (internal_help && !strcmp(arg + 2, "help-all")) |
47e9cd28 | 903 | return usage_with_options_internal(ctx, usagestr, options, 1, 0); |
b92891f9 | 904 | if (internal_help && !strcmp(arg + 2, "help")) |
ac20ff6d | 905 | goto show_usage; |
07fe54db | 906 | switch (parse_long_opt(ctx, arg + 2, options)) { |
f41179f1 | 907 | case PARSE_OPT_ERROR: |
3bb0923f | 908 | return PARSE_OPT_ERROR; |
f41179f1 | 909 | case PARSE_OPT_UNKNOWN: |
b5ce3a54 | 910 | goto unknown; |
f41179f1 | 911 | case PARSE_OPT_HELP: |
3bb0923f | 912 | goto show_usage; |
f41179f1 | 913 | case PARSE_OPT_NON_OPTION: |
fa83cc83 | 914 | case PARSE_OPT_SUBCOMMAND: |
f41179f1 NTND |
915 | case PARSE_OPT_COMPLETE: |
916 | BUG("parse_long_opt() cannot return these"); | |
917 | case PARSE_OPT_DONE: | |
918 | break; | |
07fe54db | 919 | } |
b5ce3a54 RS |
920 | continue; |
921 | unknown: | |
202fbb33 NTND |
922 | if (ctx->flags & PARSE_OPT_ONE_SHOT) |
923 | break; | |
fa83cc83 SG |
924 | if (ctx->has_subcommands && |
925 | (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) && | |
926 | (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) { | |
927 | /* | |
928 | * Found an unknown option given to a command with | |
929 | * subcommands that has a default operation mode: | |
930 | * we treat this option and all remaining args as | |
931 | * arguments meant to that default operation mode. | |
932 | * So we are done parsing. | |
933 | */ | |
934 | return PARSE_OPT_DONE; | |
935 | } | |
99d86d60 | 936 | if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) |
b5ce3a54 RS |
937 | return PARSE_OPT_UNKNOWN; |
938 | ctx->out[ctx->cpidx++] = ctx->argv[0]; | |
939 | ctx->opt = NULL; | |
ff43ec3e PH |
940 | } |
941 | return PARSE_OPT_DONE; | |
ac20ff6d | 942 | |
ac20ff6d | 943 | show_usage: |
3bb0923f | 944 | return usage_with_options_internal(ctx, usagestr, options, 0, 0); |
ff43ec3e PH |
945 | } |
946 | ||
947 | int parse_options_end(struct parse_opt_ctx_t *ctx) | |
948 | { | |
202fbb33 NTND |
949 | if (ctx->flags & PARSE_OPT_ONE_SHOT) |
950 | return ctx->total - ctx->argc; | |
951 | ||
f919ffeb | 952 | MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc); |
ff43ec3e PH |
953 | ctx->out[ctx->cpidx + ctx->argc] = NULL; |
954 | return ctx->cpidx + ctx->argc; | |
955 | } | |
956 | ||
06a199f3 ÆAB |
957 | int parse_options(int argc, const char **argv, |
958 | const char *prefix, | |
959 | const struct option *options, | |
960 | const char * const usagestr[], | |
961 | enum parse_opt_flags flags) | |
ff43ec3e PH |
962 | { |
963 | struct parse_opt_ctx_t ctx; | |
5c387428 | 964 | struct option *real_options; |
ff43ec3e | 965 | |
b02e7d5d JS |
966 | disallow_abbreviated_options = |
967 | git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0); | |
968 | ||
5c387428 NTND |
969 | memset(&ctx, 0, sizeof(ctx)); |
970 | real_options = preprocess_options(&ctx, options); | |
971 | if (real_options) | |
972 | options = real_options; | |
973 | parse_options_start_1(&ctx, argc, argv, prefix, options, flags); | |
ff43ec3e PH |
974 | switch (parse_options_step(&ctx, options, usagestr)) { |
975 | case PARSE_OPT_HELP: | |
3bb0923f | 976 | case PARSE_OPT_ERROR: |
ff43ec3e | 977 | exit(129); |
a92ec7ef NTND |
978 | case PARSE_OPT_COMPLETE: |
979 | exit(0); | |
979240fe | 980 | case PARSE_OPT_NON_OPTION: |
fa83cc83 SG |
981 | case PARSE_OPT_SUBCOMMAND: |
982 | break; | |
ff43ec3e | 983 | case PARSE_OPT_DONE: |
fa83cc83 SG |
984 | if (ctx.has_subcommands && |
985 | !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) { | |
986 | error(_("need a subcommand")); | |
987 | usage_with_options(usagestr, options); | |
988 | } | |
ff43ec3e | 989 | break; |
1b887353 | 990 | case PARSE_OPT_UNKNOWN: |
07fe54db | 991 | if (ctx.argv[0][1] == '-') { |
89003426 | 992 | error(_("unknown option `%s'"), ctx.argv[0] + 2); |
b141a478 | 993 | } else if (isascii(*ctx.opt)) { |
89003426 | 994 | error(_("unknown switch `%c'"), *ctx.opt); |
b141a478 | 995 | } else { |
89003426 | 996 | error(_("unknown non-ascii option in string: `%s'"), |
b141a478 | 997 | ctx.argv[0]); |
07fe54db PH |
998 | } |
999 | usage_with_options(usagestr, options); | |
4a59fd13 PH |
1000 | } |
1001 | ||
5c327502 | 1002 | precompose_argv_prefix(argc, argv, NULL); |
64cc539f | 1003 | free_preprocessed_options(real_options); |
5c387428 | 1004 | free(ctx.alias_groups); |
7e7bbcb4 | 1005 | return parse_options_end(&ctx); |
4a59fd13 | 1006 | } |
d7a38c54 | 1007 | |
9c7304e3 | 1008 | static int usage_argh(const struct option *opts, FILE *outfile) |
29f25d49 SB |
1009 | { |
1010 | const char *s; | |
5f0df44c RS |
1011 | int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
1012 | !opts->argh || !!strpbrk(opts->argh, "()<>[]|"); | |
29f25d49 SB |
1013 | if (opts->flags & PARSE_OPT_OPTARG) |
1014 | if (opts->long_name) | |
1015 | s = literal ? "[=%s]" : "[=<%s>]"; | |
1016 | else | |
1017 | s = literal ? "[%s]" : "[<%s>]"; | |
1018 | else | |
1019 | s = literal ? " %s" : " <%s>"; | |
c0821965 | 1020 | return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); |
29f25d49 SB |
1021 | } |
1022 | ||
d7a38c54 PH |
1023 | #define USAGE_OPTS_WIDTH 24 |
1024 | #define USAGE_GAP 2 | |
1025 | ||
352e7613 ÆAB |
1026 | static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx, |
1027 | const char * const *usagestr, | |
1028 | const struct option *opts, | |
1029 | int full, int err) | |
d7a38c54 | 1030 | { |
9c7304e3 | 1031 | FILE *outfile = err ? stderr : stdout; |
a6304fa4 | 1032 | int need_newline; |
9c7304e3 | 1033 | |
4631cfc2 ÆAB |
1034 | const char *usage_prefix = _("usage: %s"); |
1035 | /* | |
1036 | * The translation could be anything, but we can count on | |
1037 | * msgfmt(1)'s --check option to have asserted that "%s" is in | |
1038 | * the translation. So compute the length of the "usage: " | |
1039 | * part. We are assuming that the translator wasn't overly | |
1040 | * clever and used e.g. "%1$s" instead of "%s", there's only | |
1041 | * one "%s" in "usage_prefix" above, so there's no reason to | |
1042 | * do so even with a RTL language. | |
1043 | */ | |
1044 | size_t usage_len = strlen(usage_prefix) - strlen("%s"); | |
1045 | /* | |
1046 | * TRANSLATORS: the colon here should align with the | |
1047 | * one in "usage: %s" translation. | |
1048 | */ | |
1049 | const char *or_prefix = _(" or: %s"); | |
1050 | /* | |
1051 | * TRANSLATORS: You should only need to translate this format | |
1052 | * string if your language is a RTL language (e.g. Arabic, | |
1053 | * Hebrew etc.), not if it's a LTR language (e.g. German, | |
1054 | * Russian, Chinese etc.). | |
1055 | * | |
1056 | * When a translated usage string has an embedded "\n" it's | |
1057 | * because options have wrapped to the next line. The line | |
1058 | * after the "\n" will then be padded to align with the | |
1059 | * command name, such as N_("git cmd [opt]\n<8 | |
1060 | * spaces>[opt2]"), where the 8 spaces are the same length as | |
1061 | * "git cmd ". | |
1062 | * | |
1063 | * This format string prints out that already-translated | |
1064 | * line. The "%*s" is whitespace padding to account for the | |
1065 | * padding at the start of the line that we add in this | |
1066 | * function. The "%s" is a line in the (hopefully already | |
1067 | * translated) N_() usage string, which contained embedded | |
1068 | * newlines before we split it up. | |
1069 | */ | |
1070 | const char *usage_continued = _("%*s%s"); | |
1071 | const char *prefix = usage_prefix; | |
1072 | int saw_empty_line = 0; | |
1073 | ||
49b61802 RS |
1074 | if (!usagestr) |
1075 | return PARSE_OPT_HELP; | |
1076 | ||
47e9cd28 TR |
1077 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
1078 | fprintf(outfile, "cat <<\\EOF\n"); | |
1079 | ||
44d86e91 | 1080 | while (*usagestr) { |
4631cfc2 ÆAB |
1081 | const char *str = _(*usagestr++); |
1082 | struct string_list list = STRING_LIST_INIT_DUP; | |
1083 | unsigned int j; | |
1084 | ||
1085 | if (!saw_empty_line && !*str) | |
1086 | saw_empty_line = 1; | |
1087 | ||
1088 | string_list_split(&list, str, '\n', -1); | |
1089 | for (j = 0; j < list.nr; j++) { | |
1090 | const char *line = list.items[j].string; | |
1091 | ||
1092 | if (saw_empty_line && *line) | |
1093 | fprintf_ln(outfile, _(" %s"), line); | |
1094 | else if (saw_empty_line) | |
1095 | fputc('\n', outfile); | |
1096 | else if (!j) | |
1097 | fprintf_ln(outfile, prefix, line); | |
1098 | else | |
1099 | fprintf_ln(outfile, usage_continued, | |
1100 | (int)usage_len, "", line); | |
1101 | } | |
1102 | string_list_clear(&list, 0); | |
1103 | ||
1104 | prefix = or_prefix; | |
44d86e91 | 1105 | } |
d7a38c54 | 1106 | |
a6304fa4 | 1107 | need_newline = 1; |
d7a38c54 PH |
1108 | |
1109 | for (; opts->type != OPTION_END; opts++) { | |
1110 | size_t pos; | |
1111 | int pad; | |
1112 | ||
fa83cc83 SG |
1113 | if (opts->type == OPTION_SUBCOMMAND) |
1114 | continue; | |
d7a38c54 | 1115 | if (opts->type == OPTION_GROUP) { |
9c7304e3 | 1116 | fputc('\n', outfile); |
a6304fa4 | 1117 | need_newline = 0; |
d7a38c54 | 1118 | if (*opts->help) |
54e6dc7d | 1119 | fprintf(outfile, "%s\n", _(opts->help)); |
d7a38c54 PH |
1120 | continue; |
1121 | } | |
dd3bf0f4 PH |
1122 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
1123 | continue; | |
d7a38c54 | 1124 | |
a6304fa4 BC |
1125 | if (need_newline) { |
1126 | fputc('\n', outfile); | |
1127 | need_newline = 0; | |
1128 | } | |
1129 | ||
9c7304e3 | 1130 | pos = fprintf(outfile, " "); |
cbb08c2e | 1131 | if (opts->short_name) { |
51a9949e | 1132 | if (opts->flags & PARSE_OPT_NODASH) |
9c7304e3 | 1133 | pos += fprintf(outfile, "%c", opts->short_name); |
51a9949e | 1134 | else |
9c7304e3 | 1135 | pos += fprintf(outfile, "-%c", opts->short_name); |
51a9949e | 1136 | } |
d7a38c54 | 1137 | if (opts->long_name && opts->short_name) |
9c7304e3 | 1138 | pos += fprintf(outfile, ", "); |
d7a38c54 | 1139 | if (opts->long_name) |
cbb08c2e | 1140 | pos += fprintf(outfile, "--%s", opts->long_name); |
e0319ff5 | 1141 | if (opts->type == OPTION_NUMBER) |
c0821965 | 1142 | pos += utf8_fprintf(outfile, _("-NUM")); |
d7a38c54 | 1143 | |
b57c68a6 JN |
1144 | if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
1145 | !(opts->flags & PARSE_OPT_NOARG)) | |
9c7304e3 | 1146 | pos += usage_argh(opts, outfile); |
d7a38c54 | 1147 | |
f389c808 AR |
1148 | if (pos <= USAGE_OPTS_WIDTH) |
1149 | pad = USAGE_OPTS_WIDTH - pos; | |
d7a38c54 | 1150 | else { |
9c7304e3 | 1151 | fputc('\n', outfile); |
d7a38c54 PH |
1152 | pad = USAGE_OPTS_WIDTH; |
1153 | } | |
5c387428 NTND |
1154 | if (opts->type == OPTION_ALIAS) { |
1155 | fprintf(outfile, "%*s", pad + USAGE_GAP, ""); | |
1156 | fprintf_ln(outfile, _("alias of --%s"), | |
1157 | (const char *)opts->value); | |
1158 | continue; | |
1159 | } | |
54e6dc7d | 1160 | fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help)); |
d7a38c54 | 1161 | } |
9c7304e3 | 1162 | fputc('\n', outfile); |
f389c808 | 1163 | |
47e9cd28 TR |
1164 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
1165 | fputs("EOF\n", outfile); | |
1166 | ||
ee68b87a | 1167 | return PARSE_OPT_HELP; |
d7a38c54 | 1168 | } |
0ce865b1 | 1169 | |
c2e86add | 1170 | void NORETURN usage_with_options(const char * const *usagestr, |
ff43ec3e | 1171 | const struct option *opts) |
dd3bf0f4 | 1172 | { |
47e9cd28 | 1173 | usage_with_options_internal(NULL, usagestr, opts, 0, 1); |
ff43ec3e | 1174 | exit(129); |
dd3bf0f4 PH |
1175 | } |
1176 | ||
c2e86add | 1177 | void NORETURN usage_msg_opt(const char *msg, |
451bb210 CC |
1178 | const char * const *usagestr, |
1179 | const struct option *options) | |
1180 | { | |
e081a7c3 | 1181 | die_message("%s\n", msg); /* The extra \n is intentional */ |
451bb210 CC |
1182 | usage_with_options(usagestr, options); |
1183 | } | |
fa476be8 ÆAB |
1184 | |
1185 | void NORETURN usage_msg_optf(const char * const fmt, | |
1186 | const char * const *usagestr, | |
1187 | const struct option *options, ...) | |
1188 | { | |
1189 | struct strbuf msg = STRBUF_INIT; | |
1190 | va_list ap; | |
1191 | va_start(ap, options); | |
1192 | strbuf_vaddf(&msg, fmt, ap); | |
1193 | va_end(ap); | |
1194 | ||
1195 | usage_msg_opt(msg.buf, usagestr, options); | |
1196 | } | |
d21d5ddf | 1197 | |
a699367b JNA |
1198 | void die_for_incompatible_opt4(int opt1, const char *opt1_name, |
1199 | int opt2, const char *opt2_name, | |
1200 | int opt3, const char *opt3_name, | |
1201 | int opt4, const char *opt4_name) | |
1202 | { | |
1203 | int count = 0; | |
1204 | const char *options[4]; | |
1205 | ||
1206 | if (opt1) | |
1207 | options[count++] = opt1_name; | |
1208 | if (opt2) | |
1209 | options[count++] = opt2_name; | |
1210 | if (opt3) | |
1211 | options[count++] = opt3_name; | |
1212 | if (opt4) | |
1213 | options[count++] = opt4_name; | |
1214 | switch (count) { | |
1215 | case 4: | |
1216 | die(_("options '%s', '%s', '%s', and '%s' cannot be used together"), | |
1217 | opt1_name, opt2_name, opt3_name, opt4_name); | |
1218 | break; | |
1219 | case 3: | |
1220 | die(_("options '%s', '%s', and '%s' cannot be used together"), | |
1221 | options[0], options[1], options[2]); | |
1222 | break; | |
1223 | case 2: | |
1224 | die(_("options '%s' and '%s' cannot be used together"), | |
1225 | options[0], options[1]); | |
1226 | break; | |
1227 | default: | |
1228 | break; | |
1229 | } | |
1230 | } |