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