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