]>
Commit | Line | Data |
---|---|---|
4a59fd13 PH |
1 | #include "git-compat-util.h" |
2 | #include "parse-options.h" | |
26141b5b | 3 | #include "cache.h" |
269defdf | 4 | #include "commit.h" |
73e9da01 | 5 | #include "color.h" |
c0821965 | 6 | #include "utf8.h" |
4a59fd13 | 7 | |
47e9cd28 TR |
8 | static int parse_options_usage(struct parse_opt_ctx_t *ctx, |
9 | const char * const *usagestr, | |
9c7304e3 | 10 | const struct option *opts, int err); |
41064ebc | 11 | |
4a59fd13 PH |
12 | #define OPT_SHORT 1 |
13 | #define OPT_UNSET 2 | |
14 | ||
1f275b7c | 15 | int optbug(const struct option *opt, const char *reason) |
1e5ce570 JN |
16 | { |
17 | if (opt->long_name) | |
18 | return error("BUG: option '%s' %s", opt->long_name, reason); | |
19 | return error("BUG: switch '%c' %s", opt->short_name, reason); | |
20 | } | |
21 | ||
1cc6985c PH |
22 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
23 | int flags, const char **arg) | |
24 | { | |
25 | if (p->opt) { | |
26 | *arg = p->opt; | |
27 | p->opt = NULL; | |
28 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { | |
29 | *arg = (const char *)opt->defval; | |
d5d745f9 | 30 | } else if (p->argc > 1) { |
1cc6985c PH |
31 | p->argc--; |
32 | *arg = *++p->argv; | |
33 | } else | |
34 | return opterror(opt, "requires a value", flags); | |
35 | return 0; | |
36 | } | |
37 | ||
df217ed6 SB |
38 | static void fix_filename(const char *prefix, const char **file) |
39 | { | |
40 | if (!file || !*file || !prefix || is_absolute_path(*file) | |
41 | || !strcmp("-", *file)) | |
42 | return; | |
43 | *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file)); | |
44 | } | |
45 | ||
7e7bbcb4 | 46 | static int get_value(struct parse_opt_ctx_t *p, |
1cc6985c | 47 | const struct option *opt, int flags) |
4a59fd13 | 48 | { |
ffe659f9 | 49 | const char *s, *arg; |
db7244bd | 50 | const int unset = flags & OPT_UNSET; |
df217ed6 | 51 | int err; |
4a59fd13 | 52 | |
db7244bd | 53 | if (unset && p->opt) |
4a59fd13 | 54 | return opterror(opt, "takes no value", flags); |
db7244bd PH |
55 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
56 | return opterror(opt, "isn't available", flags); | |
c1f4ec9e SB |
57 | if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) |
58 | return opterror(opt, "takes no value", flags); | |
db7244bd | 59 | |
db7244bd | 60 | switch (opt->type) { |
b0b3a8b6 JN |
61 | case OPTION_LOWLEVEL_CALLBACK: |
62 | return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset); | |
63 | ||
db7244bd PH |
64 | case OPTION_BIT: |
65 | if (unset) | |
66 | *(int *)opt->value &= ~opt->defval; | |
4a59fd13 | 67 | else |
db7244bd PH |
68 | *(int *)opt->value |= opt->defval; |
69 | return 0; | |
70 | ||
2f4b97f9 RS |
71 | case OPTION_NEGBIT: |
72 | if (unset) | |
73 | *(int *)opt->value |= opt->defval; | |
74 | else | |
75 | *(int *)opt->value &= ~opt->defval; | |
76 | return 0; | |
77 | ||
b04ba2bb | 78 | case OPTION_COUNTUP: |
db7244bd PH |
79 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
80 | return 0; | |
81 | ||
82 | case OPTION_SET_INT: | |
83 | *(int *)opt->value = unset ? 0 : opt->defval; | |
84 | return 0; | |
85 | ||
86 | case OPTION_SET_PTR: | |
87 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; | |
4a59fd13 PH |
88 | return 0; |
89 | ||
90 | case OPTION_STRING: | |
1cc6985c | 91 | if (unset) |
db7244bd | 92 | *(const char **)opt->value = NULL; |
1cc6985c | 93 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
ffe659f9 | 94 | *(const char **)opt->value = (const char *)opt->defval; |
1cc6985c PH |
95 | else |
96 | return get_arg(p, opt, flags, (const char **)opt->value); | |
4a59fd13 PH |
97 | return 0; |
98 | ||
df217ed6 SB |
99 | case OPTION_FILENAME: |
100 | err = 0; | |
101 | if (unset) | |
102 | *(const char **)opt->value = NULL; | |
103 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) | |
104 | *(const char **)opt->value = (const char *)opt->defval; | |
105 | else | |
106 | err = get_arg(p, opt, flags, (const char **)opt->value); | |
107 | ||
108 | if (!err) | |
109 | fix_filename(p->prefix, (const char **)opt->value); | |
110 | return err; | |
111 | ||
ffe659f9 | 112 | case OPTION_CALLBACK: |
db7244bd | 113 | if (unset) |
07fe54db | 114 | return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; |
db7244bd | 115 | if (opt->flags & PARSE_OPT_NOARG) |
07fe54db | 116 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
c43a2483 | 117 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
07fe54db | 118 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
1cc6985c PH |
119 | if (get_arg(p, opt, flags, &arg)) |
120 | return -1; | |
121 | return (*opt->callback)(opt, arg, 0) ? (-1) : 0; | |
ffe659f9 | 122 | |
4a59fd13 | 123 | case OPTION_INTEGER: |
db7244bd | 124 | if (unset) { |
4a59fd13 PH |
125 | *(int *)opt->value = 0; |
126 | return 0; | |
127 | } | |
c43a2483 | 128 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
ffe659f9 PH |
129 | *(int *)opt->value = opt->defval; |
130 | return 0; | |
131 | } | |
1cc6985c PH |
132 | if (get_arg(p, opt, flags, &arg)) |
133 | return -1; | |
134 | *(int *)opt->value = strtol(arg, (char **)&s, 10); | |
4a59fd13 PH |
135 | if (*s) |
136 | return opterror(opt, "expects a numerical value", flags); | |
137 | return 0; | |
138 | ||
139 | default: | |
140 | die("should not happen, someone must be hit on the forehead"); | |
141 | } | |
142 | } | |
143 | ||
7e7bbcb4 | 144 | static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) |
4a59fd13 | 145 | { |
e0319ff5 RS |
146 | const struct option *numopt = NULL; |
147 | ||
4a59fd13 PH |
148 | for (; options->type != OPTION_END; options++) { |
149 | if (options->short_name == *p->opt) { | |
150 | p->opt = p->opt[1] ? p->opt + 1 : NULL; | |
151 | return get_value(p, options, OPT_SHORT); | |
152 | } | |
e0319ff5 RS |
153 | |
154 | /* | |
155 | * Handle the numerical option later, explicit one-digit | |
156 | * options take precedence over it. | |
157 | */ | |
158 | if (options->type == OPTION_NUMBER) | |
159 | numopt = options; | |
160 | } | |
161 | if (numopt && isdigit(*p->opt)) { | |
162 | size_t len = 1; | |
163 | char *arg; | |
164 | int rc; | |
165 | ||
166 | while (isdigit(p->opt[len])) | |
167 | len++; | |
168 | arg = xmemdupz(p->opt, len); | |
169 | p->opt = p->opt[len] ? p->opt + len : NULL; | |
170 | rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; | |
171 | free(arg); | |
172 | return rc; | |
4a59fd13 | 173 | } |
07fe54db | 174 | return -2; |
4a59fd13 PH |
175 | } |
176 | ||
7e7bbcb4 | 177 | static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, |
4a59fd13 PH |
178 | const struct option *options) |
179 | { | |
7f275b91 | 180 | const char *arg_end = strchr(arg, '='); |
243e0614 JS |
181 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
182 | int abbrev_flags = 0, ambiguous_flags = 0; | |
7f275b91 JS |
183 | |
184 | if (!arg_end) | |
185 | arg_end = arg + strlen(arg); | |
186 | ||
4a59fd13 | 187 | for (; options->type != OPTION_END; options++) { |
0f1930c5 RS |
188 | const char *rest, *long_name = options->long_name; |
189 | int flags = 0, opt_flags = 0; | |
4a59fd13 | 190 | |
0f1930c5 | 191 | if (!long_name) |
4a59fd13 PH |
192 | continue; |
193 | ||
0f1930c5 RS |
194 | again: |
195 | rest = skip_prefix(arg, long_name); | |
580d5bff PH |
196 | if (options->type == OPTION_ARGUMENT) { |
197 | if (!rest) | |
198 | continue; | |
199 | if (*rest == '=') | |
200 | return opterror(options, "takes no value", flags); | |
201 | if (*rest) | |
202 | continue; | |
203 | p->out[p->cpidx++] = arg - 2; | |
204 | return 0; | |
205 | } | |
4a59fd13 | 206 | if (!rest) { |
7f275b91 | 207 | /* abbreviated? */ |
0f1930c5 | 208 | if (!strncmp(long_name, arg, arg_end - arg)) { |
7f275b91 | 209 | is_abbreviated: |
243e0614 JS |
210 | if (abbrev_option) { |
211 | /* | |
212 | * If this is abbreviated, it is | |
213 | * ambiguous. So when there is no | |
214 | * exact match later, we need to | |
215 | * error out. | |
216 | */ | |
217 | ambiguous_option = abbrev_option; | |
218 | ambiguous_flags = abbrev_flags; | |
219 | } | |
7f275b91 JS |
220 | if (!(flags & OPT_UNSET) && *arg_end) |
221 | p->opt = arg_end + 1; | |
222 | abbrev_option = options; | |
0f1930c5 | 223 | abbrev_flags = flags ^ opt_flags; |
7f275b91 JS |
224 | continue; |
225 | } | |
6bbfd1fa AS |
226 | /* negation allowed? */ |
227 | if (options->flags & PARSE_OPT_NONEG) | |
228 | continue; | |
7f275b91 JS |
229 | /* negated and abbreviated very much? */ |
230 | if (!prefixcmp("no-", arg)) { | |
231 | flags |= OPT_UNSET; | |
232 | goto is_abbreviated; | |
233 | } | |
234 | /* negated? */ | |
0f1930c5 RS |
235 | if (prefixcmp(arg, "no-")) { |
236 | if (!prefixcmp(long_name, "no-")) { | |
237 | long_name += 3; | |
238 | opt_flags |= OPT_UNSET; | |
239 | goto again; | |
240 | } | |
4a59fd13 | 241 | continue; |
0f1930c5 | 242 | } |
4a59fd13 | 243 | flags |= OPT_UNSET; |
0f1930c5 | 244 | rest = skip_prefix(arg + 3, long_name); |
7f275b91 | 245 | /* abbreviated and negated? */ |
0f1930c5 | 246 | if (!rest && !prefixcmp(long_name, arg + 3)) |
7f275b91 | 247 | goto is_abbreviated; |
4a59fd13 PH |
248 | if (!rest) |
249 | continue; | |
250 | } | |
251 | if (*rest) { | |
252 | if (*rest != '=') | |
253 | continue; | |
254 | p->opt = rest + 1; | |
255 | } | |
0f1930c5 | 256 | return get_value(p, options, flags ^ opt_flags); |
4a59fd13 | 257 | } |
243e0614 JS |
258 | |
259 | if (ambiguous_option) | |
260 | return error("Ambiguous option: %s " | |
261 | "(could be --%s%s or --%s%s)", | |
262 | arg, | |
263 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", | |
264 | ambiguous_option->long_name, | |
265 | (abbrev_flags & OPT_UNSET) ? "no-" : "", | |
266 | abbrev_option->long_name); | |
7f275b91 JS |
267 | if (abbrev_option) |
268 | return get_value(p, abbrev_option, abbrev_flags); | |
07fe54db | 269 | return -2; |
4a59fd13 PH |
270 | } |
271 | ||
51a9949e RS |
272 | static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg, |
273 | const struct option *options) | |
274 | { | |
275 | for (; options->type != OPTION_END; options++) { | |
276 | if (!(options->flags & PARSE_OPT_NODASH)) | |
277 | continue; | |
51a9949e RS |
278 | if (options->short_name == arg[0] && arg[1] == '\0') |
279 | return get_value(p, options, OPT_SHORT); | |
280 | } | |
281 | return -2; | |
282 | } | |
283 | ||
1121a878 | 284 | static void check_typos(const char *arg, const struct option *options) |
3a9f0f41 PH |
285 | { |
286 | if (strlen(arg) < 3) | |
287 | return; | |
288 | ||
289 | if (!prefixcmp(arg, "no-")) { | |
290 | error ("did you mean `--%s` (with two dashes ?)", arg); | |
291 | exit(129); | |
292 | } | |
293 | ||
294 | for (; options->type != OPTION_END; options++) { | |
295 | if (!options->long_name) | |
296 | continue; | |
297 | if (!prefixcmp(options->long_name, arg)) { | |
298 | error ("did you mean `--%s` (with two dashes ?)", arg); | |
299 | exit(129); | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
cb9d398c PH |
304 | static void parse_options_check(const struct option *opts) |
305 | { | |
306 | int err = 0; | |
307 | ||
308 | for (; opts->type != OPTION_END; opts++) { | |
309 | if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && | |
1e5ce570 JN |
310 | (opts->flags & PARSE_OPT_OPTARG)) |
311 | err |= optbug(opts, "uses incompatible flags " | |
312 | "LASTARG_DEFAULT and OPTARG"); | |
a02dd4ff JN |
313 | if (opts->flags & PARSE_OPT_NODASH && |
314 | ((opts->flags & PARSE_OPT_OPTARG) || | |
315 | !(opts->flags & PARSE_OPT_NOARG) || | |
316 | !(opts->flags & PARSE_OPT_NONEG) || | |
317 | opts->long_name)) | |
318 | err |= optbug(opts, "uses feature " | |
319 | "not supported for dashless options"); | |
5c400ed2 | 320 | switch (opts->type) { |
b04ba2bb | 321 | case OPTION_COUNTUP: |
5c400ed2 JN |
322 | case OPTION_BIT: |
323 | case OPTION_NEGBIT: | |
324 | case OPTION_SET_INT: | |
325 | case OPTION_SET_PTR: | |
326 | case OPTION_NUMBER: | |
327 | if ((opts->flags & PARSE_OPT_OPTARG) || | |
328 | !(opts->flags & PARSE_OPT_NOARG)) | |
329 | err |= optbug(opts, "should not accept an argument"); | |
330 | default: | |
331 | ; /* ok. (usually accepts an argument) */ | |
332 | } | |
cb9d398c | 333 | } |
cb9d398c | 334 | if (err) |
1e5ce570 | 335 | exit(128); |
cb9d398c PH |
336 | } |
337 | ||
7e7bbcb4 | 338 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
37782920 | 339 | int argc, const char **argv, const char *prefix, |
9ca1169f | 340 | const struct option *options, int flags) |
7e7bbcb4 PH |
341 | { |
342 | memset(ctx, 0, sizeof(*ctx)); | |
343 | ctx->argc = argc - 1; | |
344 | ctx->argv = argv + 1; | |
345 | ctx->out = argv; | |
37782920 | 346 | ctx->prefix = prefix; |
a32a4eaa | 347 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
7e7bbcb4 | 348 | ctx->flags = flags; |
0d260f9a RS |
349 | if ((flags & PARSE_OPT_KEEP_UNKNOWN) && |
350 | (flags & PARSE_OPT_STOP_AT_NON_OPTION)) | |
351 | die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); | |
9ca1169f | 352 | parse_options_check(options); |
7e7bbcb4 PH |
353 | } |
354 | ||
47e9cd28 TR |
355 | static int usage_with_options_internal(struct parse_opt_ctx_t *, |
356 | const char * const *, | |
9c7304e3 | 357 | const struct option *, int, int); |
dd3bf0f4 | 358 | |
ff43ec3e PH |
359 | int parse_options_step(struct parse_opt_ctx_t *ctx, |
360 | const struct option *options, | |
361 | const char * const usagestr[]) | |
4a59fd13 | 362 | { |
b92891f9 RS |
363 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
364 | ||
26141b5b PH |
365 | /* we must reset ->opt, unknown short option leave it dangling */ |
366 | ctx->opt = NULL; | |
367 | ||
ff43ec3e PH |
368 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
369 | const char *arg = ctx->argv[0]; | |
4a59fd13 PH |
370 | |
371 | if (*arg != '-' || !arg[1]) { | |
51a9949e RS |
372 | if (parse_nodash_opt(ctx, arg, options) == 0) |
373 | continue; | |
ff43ec3e | 374 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
979240fe | 375 | return PARSE_OPT_NON_OPTION; |
ff43ec3e | 376 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
4a59fd13 PH |
377 | continue; |
378 | } | |
379 | ||
380 | if (arg[1] != '-') { | |
ff43ec3e | 381 | ctx->opt = arg + 1; |
b92891f9 | 382 | if (internal_help && *ctx->opt == 'h') |
47e9cd28 | 383 | return parse_options_usage(ctx, usagestr, options, 0); |
07fe54db PH |
384 | switch (parse_short_opt(ctx, options)) { |
385 | case -1: | |
47e9cd28 | 386 | return parse_options_usage(ctx, usagestr, options, 1); |
07fe54db | 387 | case -2: |
38916c5b RS |
388 | if (ctx->opt) |
389 | check_typos(arg + 1, options); | |
b5ce3a54 | 390 | goto unknown; |
07fe54db | 391 | } |
ff43ec3e | 392 | if (ctx->opt) |
3a9f0f41 | 393 | check_typos(arg + 1, options); |
ff43ec3e | 394 | while (ctx->opt) { |
b92891f9 | 395 | if (internal_help && *ctx->opt == 'h') |
47e9cd28 | 396 | return parse_options_usage(ctx, usagestr, options, 0); |
07fe54db PH |
397 | switch (parse_short_opt(ctx, options)) { |
398 | case -1: | |
47e9cd28 | 399 | return parse_options_usage(ctx, usagestr, options, 1); |
07fe54db | 400 | case -2: |
26141b5b PH |
401 | /* fake a short option thing to hide the fact that we may have |
402 | * started to parse aggregated stuff | |
403 | * | |
404 | * This is leaky, too bad. | |
405 | */ | |
406 | ctx->argv[0] = xstrdup(ctx->opt - 1); | |
407 | *(char *)ctx->argv[0] = '-'; | |
b5ce3a54 | 408 | goto unknown; |
07fe54db | 409 | } |
3a9f0f41 | 410 | } |
4a59fd13 PH |
411 | continue; |
412 | } | |
413 | ||
414 | if (!arg[2]) { /* "--" */ | |
ff43ec3e PH |
415 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
416 | ctx->argc--; | |
417 | ctx->argv++; | |
4a59fd13 PH |
418 | } |
419 | break; | |
420 | } | |
421 | ||
b92891f9 | 422 | if (internal_help && !strcmp(arg + 2, "help-all")) |
47e9cd28 | 423 | return usage_with_options_internal(ctx, usagestr, options, 1, 0); |
b92891f9 | 424 | if (internal_help && !strcmp(arg + 2, "help")) |
47e9cd28 | 425 | return parse_options_usage(ctx, usagestr, options, 0); |
07fe54db PH |
426 | switch (parse_long_opt(ctx, arg + 2, options)) { |
427 | case -1: | |
47e9cd28 | 428 | return parse_options_usage(ctx, usagestr, options, 1); |
07fe54db | 429 | case -2: |
b5ce3a54 | 430 | goto unknown; |
07fe54db | 431 | } |
b5ce3a54 RS |
432 | continue; |
433 | unknown: | |
434 | if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN)) | |
435 | return PARSE_OPT_UNKNOWN; | |
436 | ctx->out[ctx->cpidx++] = ctx->argv[0]; | |
437 | ctx->opt = NULL; | |
ff43ec3e PH |
438 | } |
439 | return PARSE_OPT_DONE; | |
440 | } | |
441 | ||
442 | int parse_options_end(struct parse_opt_ctx_t *ctx) | |
443 | { | |
444 | memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); | |
445 | ctx->out[ctx->cpidx + ctx->argc] = NULL; | |
446 | return ctx->cpidx + ctx->argc; | |
447 | } | |
448 | ||
37782920 SB |
449 | int parse_options(int argc, const char **argv, const char *prefix, |
450 | const struct option *options, const char * const usagestr[], | |
451 | int flags) | |
ff43ec3e PH |
452 | { |
453 | struct parse_opt_ctx_t ctx; | |
454 | ||
9ca1169f | 455 | parse_options_start(&ctx, argc, argv, prefix, options, flags); |
ff43ec3e PH |
456 | switch (parse_options_step(&ctx, options, usagestr)) { |
457 | case PARSE_OPT_HELP: | |
458 | exit(129); | |
979240fe | 459 | case PARSE_OPT_NON_OPTION: |
ff43ec3e PH |
460 | case PARSE_OPT_DONE: |
461 | break; | |
462 | default: /* PARSE_OPT_UNKNOWN */ | |
07fe54db PH |
463 | if (ctx.argv[0][1] == '-') { |
464 | error("unknown option `%s'", ctx.argv[0] + 2); | |
b141a478 | 465 | } else if (isascii(*ctx.opt)) { |
07fe54db | 466 | error("unknown switch `%c'", *ctx.opt); |
b141a478 EFL |
467 | } else { |
468 | error("unknown non-ascii option in string: `%s'", | |
469 | ctx.argv[0]); | |
07fe54db PH |
470 | } |
471 | usage_with_options(usagestr, options); | |
4a59fd13 PH |
472 | } |
473 | ||
76759c7d | 474 | precompose_argv(argc, argv); |
7e7bbcb4 | 475 | return parse_options_end(&ctx); |
4a59fd13 | 476 | } |
d7a38c54 | 477 | |
9c7304e3 | 478 | static int usage_argh(const struct option *opts, FILE *outfile) |
29f25d49 SB |
479 | { |
480 | const char *s; | |
34aec9f5 | 481 | int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh; |
29f25d49 SB |
482 | if (opts->flags & PARSE_OPT_OPTARG) |
483 | if (opts->long_name) | |
484 | s = literal ? "[=%s]" : "[=<%s>]"; | |
485 | else | |
486 | s = literal ? "[%s]" : "[<%s>]"; | |
487 | else | |
488 | s = literal ? " %s" : " <%s>"; | |
c0821965 | 489 | return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); |
29f25d49 SB |
490 | } |
491 | ||
d7a38c54 PH |
492 | #define USAGE_OPTS_WIDTH 24 |
493 | #define USAGE_GAP 2 | |
494 | ||
47e9cd28 TR |
495 | static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, |
496 | const char * const *usagestr, | |
497 | const struct option *opts, int full, int err) | |
d7a38c54 | 498 | { |
9c7304e3 GS |
499 | FILE *outfile = err ? stderr : stdout; |
500 | ||
49b61802 RS |
501 | if (!usagestr) |
502 | return PARSE_OPT_HELP; | |
503 | ||
47e9cd28 TR |
504 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
505 | fprintf(outfile, "cat <<\\EOF\n"); | |
506 | ||
54e6dc7d | 507 | fprintf_ln(outfile, _("usage: %s"), _(*usagestr++)); |
f389c808 | 508 | while (*usagestr && **usagestr) |
54e6dc7d NTND |
509 | /* TRANSLATORS: the colon here should align with the |
510 | one in "usage: %s" translation */ | |
511 | fprintf_ln(outfile, _(" or: %s"), _(*usagestr++)); | |
44d86e91 | 512 | while (*usagestr) { |
54e6dc7d NTND |
513 | if (**usagestr) |
514 | fprintf_ln(outfile, _(" %s"), _(*usagestr)); | |
515 | else | |
516 | putchar('\n'); | |
44d86e91 JK |
517 | usagestr++; |
518 | } | |
d7a38c54 PH |
519 | |
520 | if (opts->type != OPTION_GROUP) | |
9c7304e3 | 521 | fputc('\n', outfile); |
d7a38c54 PH |
522 | |
523 | for (; opts->type != OPTION_END; opts++) { | |
524 | size_t pos; | |
525 | int pad; | |
526 | ||
527 | if (opts->type == OPTION_GROUP) { | |
9c7304e3 | 528 | fputc('\n', outfile); |
d7a38c54 | 529 | if (*opts->help) |
54e6dc7d | 530 | fprintf(outfile, "%s\n", _(opts->help)); |
d7a38c54 PH |
531 | continue; |
532 | } | |
dd3bf0f4 PH |
533 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
534 | continue; | |
d7a38c54 | 535 | |
9c7304e3 | 536 | pos = fprintf(outfile, " "); |
cbb08c2e | 537 | if (opts->short_name) { |
51a9949e | 538 | if (opts->flags & PARSE_OPT_NODASH) |
9c7304e3 | 539 | pos += fprintf(outfile, "%c", opts->short_name); |
51a9949e | 540 | else |
9c7304e3 | 541 | pos += fprintf(outfile, "-%c", opts->short_name); |
51a9949e | 542 | } |
d7a38c54 | 543 | if (opts->long_name && opts->short_name) |
9c7304e3 | 544 | pos += fprintf(outfile, ", "); |
d7a38c54 | 545 | if (opts->long_name) |
cbb08c2e | 546 | pos += fprintf(outfile, "--%s", opts->long_name); |
e0319ff5 | 547 | if (opts->type == OPTION_NUMBER) |
c0821965 | 548 | pos += utf8_fprintf(outfile, _("-NUM")); |
d7a38c54 | 549 | |
b57c68a6 JN |
550 | if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
551 | !(opts->flags & PARSE_OPT_NOARG)) | |
9c7304e3 | 552 | pos += usage_argh(opts, outfile); |
d7a38c54 | 553 | |
f389c808 AR |
554 | if (pos <= USAGE_OPTS_WIDTH) |
555 | pad = USAGE_OPTS_WIDTH - pos; | |
d7a38c54 | 556 | else { |
9c7304e3 | 557 | fputc('\n', outfile); |
d7a38c54 PH |
558 | pad = USAGE_OPTS_WIDTH; |
559 | } | |
54e6dc7d | 560 | fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help)); |
d7a38c54 | 561 | } |
9c7304e3 | 562 | fputc('\n', outfile); |
f389c808 | 563 | |
47e9cd28 TR |
564 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
565 | fputs("EOF\n", outfile); | |
566 | ||
ee68b87a | 567 | return PARSE_OPT_HELP; |
d7a38c54 | 568 | } |
0ce865b1 | 569 | |
c2e86add | 570 | void NORETURN usage_with_options(const char * const *usagestr, |
ff43ec3e | 571 | const struct option *opts) |
dd3bf0f4 | 572 | { |
47e9cd28 | 573 | usage_with_options_internal(NULL, usagestr, opts, 0, 1); |
ff43ec3e | 574 | exit(129); |
dd3bf0f4 PH |
575 | } |
576 | ||
c2e86add | 577 | void NORETURN usage_msg_opt(const char *msg, |
451bb210 CC |
578 | const char * const *usagestr, |
579 | const struct option *options) | |
580 | { | |
581 | fprintf(stderr, "%s\n\n", msg); | |
582 | usage_with_options(usagestr, options); | |
583 | } | |
584 | ||
47e9cd28 TR |
585 | static int parse_options_usage(struct parse_opt_ctx_t *ctx, |
586 | const char * const *usagestr, | |
9c7304e3 | 587 | const struct option *opts, int err) |
ee68b87a | 588 | { |
47e9cd28 | 589 | return usage_with_options_internal(ctx, usagestr, opts, 0, err); |
ee68b87a PH |
590 | } |
591 | ||
a469a101 JK |
592 | #undef opterror |
593 | int opterror(const struct option *opt, const char *reason, int flags) | |
594 | { | |
595 | if (flags & OPT_SHORT) | |
596 | return error("switch `%c' %s", opt->short_name, reason); | |
597 | if (flags & OPT_UNSET) | |
598 | return error("option `no-%s' %s", opt->long_name, reason); | |
599 | return error("option `%s' %s", opt->long_name, reason); | |
600 | } |