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