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