]>
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 | ||
4a59fd13 PH |
14 | static int opterror(const struct option *opt, const char *reason, int flags) |
15 | { | |
16 | if (flags & OPT_SHORT) | |
17 | return error("switch `%c' %s", opt->short_name, reason); | |
18 | if (flags & OPT_UNSET) | |
19 | return error("option `no-%s' %s", opt->long_name, reason); | |
20 | return error("option `%s' %s", opt->long_name, reason); | |
21 | } | |
22 | ||
1cc6985c PH |
23 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
24 | int flags, const char **arg) | |
25 | { | |
26 | if (p->opt) { | |
27 | *arg = p->opt; | |
28 | p->opt = NULL; | |
29 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { | |
30 | *arg = (const char *)opt->defval; | |
d5d745f9 | 31 | } else if (p->argc > 1) { |
1cc6985c PH |
32 | p->argc--; |
33 | *arg = *++p->argv; | |
34 | } else | |
35 | return opterror(opt, "requires a value", flags); | |
36 | return 0; | |
37 | } | |
38 | ||
df217ed6 SB |
39 | static void fix_filename(const char *prefix, const char **file) |
40 | { | |
41 | if (!file || !*file || !prefix || is_absolute_path(*file) | |
42 | || !strcmp("-", *file)) | |
43 | return; | |
44 | *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file)); | |
45 | } | |
46 | ||
7e7bbcb4 | 47 | static int get_value(struct parse_opt_ctx_t *p, |
1cc6985c | 48 | const struct option *opt, int flags) |
4a59fd13 | 49 | { |
ffe659f9 | 50 | const char *s, *arg; |
db7244bd | 51 | const int unset = flags & OPT_UNSET; |
df217ed6 | 52 | int err; |
4a59fd13 | 53 | |
db7244bd | 54 | if (unset && p->opt) |
4a59fd13 | 55 | return opterror(opt, "takes no value", flags); |
db7244bd PH |
56 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
57 | return opterror(opt, "isn't available", flags); | |
4a59fd13 | 58 | |
db7244bd PH |
59 | if (!(flags & OPT_SHORT) && p->opt) { |
60 | switch (opt->type) { | |
61 | case OPTION_CALLBACK: | |
62 | if (!(opt->flags & PARSE_OPT_NOARG)) | |
63 | break; | |
64 | /* FALLTHROUGH */ | |
65 | case OPTION_BOOLEAN: | |
66 | case OPTION_BIT: | |
2f4b97f9 | 67 | case OPTION_NEGBIT: |
db7244bd PH |
68 | case OPTION_SET_INT: |
69 | case OPTION_SET_PTR: | |
4a59fd13 | 70 | return opterror(opt, "takes no value", flags); |
db7244bd PH |
71 | default: |
72 | break; | |
73 | } | |
74 | } | |
75 | ||
db7244bd PH |
76 | switch (opt->type) { |
77 | case OPTION_BIT: | |
78 | if (unset) | |
79 | *(int *)opt->value &= ~opt->defval; | |
4a59fd13 | 80 | else |
db7244bd PH |
81 | *(int *)opt->value |= opt->defval; |
82 | return 0; | |
83 | ||
2f4b97f9 RS |
84 | case OPTION_NEGBIT: |
85 | if (unset) | |
86 | *(int *)opt->value |= opt->defval; | |
87 | else | |
88 | *(int *)opt->value &= ~opt->defval; | |
89 | return 0; | |
90 | ||
db7244bd PH |
91 | case OPTION_BOOLEAN: |
92 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; | |
93 | return 0; | |
94 | ||
95 | case OPTION_SET_INT: | |
96 | *(int *)opt->value = unset ? 0 : opt->defval; | |
97 | return 0; | |
98 | ||
99 | case OPTION_SET_PTR: | |
100 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; | |
4a59fd13 PH |
101 | return 0; |
102 | ||
103 | case OPTION_STRING: | |
1cc6985c | 104 | if (unset) |
db7244bd | 105 | *(const char **)opt->value = NULL; |
1cc6985c | 106 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
ffe659f9 | 107 | *(const char **)opt->value = (const char *)opt->defval; |
1cc6985c PH |
108 | else |
109 | return get_arg(p, opt, flags, (const char **)opt->value); | |
4a59fd13 PH |
110 | return 0; |
111 | ||
df217ed6 SB |
112 | case OPTION_FILENAME: |
113 | err = 0; | |
114 | if (unset) | |
115 | *(const char **)opt->value = NULL; | |
116 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) | |
117 | *(const char **)opt->value = (const char *)opt->defval; | |
118 | else | |
119 | err = get_arg(p, opt, flags, (const char **)opt->value); | |
120 | ||
121 | if (!err) | |
122 | fix_filename(p->prefix, (const char **)opt->value); | |
123 | return err; | |
124 | ||
ffe659f9 | 125 | case OPTION_CALLBACK: |
db7244bd | 126 | if (unset) |
07fe54db | 127 | return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; |
db7244bd | 128 | if (opt->flags & PARSE_OPT_NOARG) |
07fe54db | 129 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
c43a2483 | 130 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
07fe54db | 131 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
1cc6985c PH |
132 | if (get_arg(p, opt, flags, &arg)) |
133 | return -1; | |
134 | return (*opt->callback)(opt, arg, 0) ? (-1) : 0; | |
ffe659f9 | 135 | |
4a59fd13 | 136 | case OPTION_INTEGER: |
db7244bd | 137 | if (unset) { |
4a59fd13 PH |
138 | *(int *)opt->value = 0; |
139 | return 0; | |
140 | } | |
c43a2483 | 141 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
ffe659f9 PH |
142 | *(int *)opt->value = opt->defval; |
143 | return 0; | |
144 | } | |
1cc6985c PH |
145 | if (get_arg(p, opt, flags, &arg)) |
146 | return -1; | |
147 | *(int *)opt->value = strtol(arg, (char **)&s, 10); | |
4a59fd13 PH |
148 | if (*s) |
149 | return opterror(opt, "expects a numerical value", flags); | |
150 | return 0; | |
151 | ||
152 | default: | |
153 | die("should not happen, someone must be hit on the forehead"); | |
154 | } | |
155 | } | |
156 | ||
7e7bbcb4 | 157 | static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) |
4a59fd13 | 158 | { |
e0319ff5 RS |
159 | const struct option *numopt = NULL; |
160 | ||
4a59fd13 PH |
161 | for (; options->type != OPTION_END; options++) { |
162 | if (options->short_name == *p->opt) { | |
163 | p->opt = p->opt[1] ? p->opt + 1 : NULL; | |
164 | return get_value(p, options, OPT_SHORT); | |
165 | } | |
e0319ff5 RS |
166 | |
167 | /* | |
168 | * Handle the numerical option later, explicit one-digit | |
169 | * options take precedence over it. | |
170 | */ | |
171 | if (options->type == OPTION_NUMBER) | |
172 | numopt = options; | |
173 | } | |
174 | if (numopt && isdigit(*p->opt)) { | |
175 | size_t len = 1; | |
176 | char *arg; | |
177 | int rc; | |
178 | ||
179 | while (isdigit(p->opt[len])) | |
180 | len++; | |
181 | arg = xmemdupz(p->opt, len); | |
182 | p->opt = p->opt[len] ? p->opt + len : NULL; | |
183 | rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; | |
184 | free(arg); | |
185 | return rc; | |
4a59fd13 | 186 | } |
07fe54db | 187 | return -2; |
4a59fd13 PH |
188 | } |
189 | ||
7e7bbcb4 | 190 | static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, |
4a59fd13 PH |
191 | const struct option *options) |
192 | { | |
7f275b91 | 193 | const char *arg_end = strchr(arg, '='); |
243e0614 JS |
194 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
195 | int abbrev_flags = 0, ambiguous_flags = 0; | |
7f275b91 JS |
196 | |
197 | if (!arg_end) | |
198 | arg_end = arg + strlen(arg); | |
199 | ||
4a59fd13 PH |
200 | for (; options->type != OPTION_END; options++) { |
201 | const char *rest; | |
202 | int flags = 0; | |
203 | ||
204 | if (!options->long_name) | |
205 | continue; | |
206 | ||
207 | rest = skip_prefix(arg, options->long_name); | |
580d5bff PH |
208 | if (options->type == OPTION_ARGUMENT) { |
209 | if (!rest) | |
210 | continue; | |
211 | if (*rest == '=') | |
212 | return opterror(options, "takes no value", flags); | |
213 | if (*rest) | |
214 | continue; | |
215 | p->out[p->cpidx++] = arg - 2; | |
216 | return 0; | |
217 | } | |
4a59fd13 | 218 | if (!rest) { |
7f275b91 JS |
219 | /* abbreviated? */ |
220 | if (!strncmp(options->long_name, arg, arg_end - arg)) { | |
221 | is_abbreviated: | |
243e0614 JS |
222 | if (abbrev_option) { |
223 | /* | |
224 | * If this is abbreviated, it is | |
225 | * ambiguous. So when there is no | |
226 | * exact match later, we need to | |
227 | * error out. | |
228 | */ | |
229 | ambiguous_option = abbrev_option; | |
230 | ambiguous_flags = abbrev_flags; | |
231 | } | |
7f275b91 JS |
232 | if (!(flags & OPT_UNSET) && *arg_end) |
233 | p->opt = arg_end + 1; | |
234 | abbrev_option = options; | |
235 | abbrev_flags = flags; | |
236 | continue; | |
237 | } | |
6bbfd1fa AS |
238 | /* negation allowed? */ |
239 | if (options->flags & PARSE_OPT_NONEG) | |
240 | continue; | |
7f275b91 JS |
241 | /* negated and abbreviated very much? */ |
242 | if (!prefixcmp("no-", arg)) { | |
243 | flags |= OPT_UNSET; | |
244 | goto is_abbreviated; | |
245 | } | |
246 | /* negated? */ | |
4a59fd13 PH |
247 | if (strncmp(arg, "no-", 3)) |
248 | continue; | |
249 | flags |= OPT_UNSET; | |
250 | rest = skip_prefix(arg + 3, options->long_name); | |
7f275b91 JS |
251 | /* abbreviated and negated? */ |
252 | if (!rest && !prefixcmp(options->long_name, arg + 3)) | |
253 | goto is_abbreviated; | |
4a59fd13 PH |
254 | if (!rest) |
255 | continue; | |
256 | } | |
257 | if (*rest) { | |
258 | if (*rest != '=') | |
259 | continue; | |
260 | p->opt = rest + 1; | |
261 | } | |
262 | return get_value(p, options, flags); | |
263 | } | |
243e0614 JS |
264 | |
265 | if (ambiguous_option) | |
266 | return error("Ambiguous option: %s " | |
267 | "(could be --%s%s or --%s%s)", | |
268 | arg, | |
269 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", | |
270 | ambiguous_option->long_name, | |
271 | (abbrev_flags & OPT_UNSET) ? "no-" : "", | |
272 | abbrev_option->long_name); | |
7f275b91 JS |
273 | if (abbrev_option) |
274 | return get_value(p, abbrev_option, abbrev_flags); | |
07fe54db | 275 | return -2; |
4a59fd13 PH |
276 | } |
277 | ||
51a9949e RS |
278 | static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg, |
279 | const struct option *options) | |
280 | { | |
281 | for (; options->type != OPTION_END; options++) { | |
282 | if (!(options->flags & PARSE_OPT_NODASH)) | |
283 | continue; | |
284 | if ((options->flags & PARSE_OPT_OPTARG) || | |
285 | !(options->flags & PARSE_OPT_NOARG)) | |
286 | die("BUG: dashless options don't support arguments"); | |
287 | if (!(options->flags & PARSE_OPT_NONEG)) | |
288 | die("BUG: dashless options don't support negation"); | |
289 | if (options->long_name) | |
290 | die("BUG: dashless options can't be long"); | |
291 | if (options->short_name == arg[0] && arg[1] == '\0') | |
292 | return get_value(p, options, OPT_SHORT); | |
293 | } | |
294 | return -2; | |
295 | } | |
296 | ||
1121a878 | 297 | static void check_typos(const char *arg, const struct option *options) |
3a9f0f41 PH |
298 | { |
299 | if (strlen(arg) < 3) | |
300 | return; | |
301 | ||
302 | if (!prefixcmp(arg, "no-")) { | |
303 | error ("did you mean `--%s` (with two dashes ?)", arg); | |
304 | exit(129); | |
305 | } | |
306 | ||
307 | for (; options->type != OPTION_END; options++) { | |
308 | if (!options->long_name) | |
309 | continue; | |
310 | if (!prefixcmp(options->long_name, arg)) { | |
311 | error ("did you mean `--%s` (with two dashes ?)", arg); | |
312 | exit(129); | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
cb9d398c PH |
317 | static void parse_options_check(const struct option *opts) |
318 | { | |
319 | int err = 0; | |
320 | ||
321 | for (; opts->type != OPTION_END; opts++) { | |
322 | if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && | |
323 | (opts->flags & PARSE_OPT_OPTARG)) { | |
324 | if (opts->long_name) { | |
325 | error("`--%s` uses incompatible flags " | |
326 | "LASTARG_DEFAULT and OPTARG", opts->long_name); | |
327 | } else { | |
328 | error("`-%c` uses incompatible flags " | |
329 | "LASTARG_DEFAULT and OPTARG", opts->short_name); | |
330 | } | |
331 | err |= 1; | |
332 | } | |
333 | } | |
334 | ||
335 | if (err) | |
336 | exit(129); | |
337 | } | |
338 | ||
7e7bbcb4 | 339 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
37782920 SB |
340 | int argc, const char **argv, const char *prefix, |
341 | int flags) | |
7e7bbcb4 PH |
342 | { |
343 | memset(ctx, 0, sizeof(*ctx)); | |
344 | ctx->argc = argc - 1; | |
345 | ctx->argv = argv + 1; | |
346 | ctx->out = argv; | |
37782920 | 347 | ctx->prefix = prefix; |
a32a4eaa | 348 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
7e7bbcb4 | 349 | ctx->flags = flags; |
0d260f9a RS |
350 | if ((flags & PARSE_OPT_KEEP_UNKNOWN) && |
351 | (flags & PARSE_OPT_STOP_AT_NON_OPTION)) | |
352 | die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); | |
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 | ||
cb9d398c PH |
365 | parse_options_check(options); |
366 | ||
26141b5b PH |
367 | /* we must reset ->opt, unknown short option leave it dangling */ |
368 | ctx->opt = NULL; | |
369 | ||
ff43ec3e PH |
370 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
371 | const char *arg = ctx->argv[0]; | |
4a59fd13 PH |
372 | |
373 | if (*arg != '-' || !arg[1]) { | |
51a9949e RS |
374 | if (parse_nodash_opt(ctx, arg, options) == 0) |
375 | continue; | |
ff43ec3e | 376 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
a0ec9d25 | 377 | break; |
ff43ec3e | 378 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
4a59fd13 PH |
379 | continue; |
380 | } | |
381 | ||
382 | if (arg[1] != '-') { | |
ff43ec3e | 383 | ctx->opt = arg + 1; |
b92891f9 | 384 | if (internal_help && *ctx->opt == 'h') |
47e9cd28 | 385 | return parse_options_usage(ctx, usagestr, options, 0); |
07fe54db PH |
386 | switch (parse_short_opt(ctx, options)) { |
387 | case -1: | |
47e9cd28 | 388 | return parse_options_usage(ctx, usagestr, options, 1); |
07fe54db | 389 | case -2: |
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 | ||
37782920 | 455 | parse_options_start(&ctx, argc, argv, prefix, flags); |
ff43ec3e PH |
456 | switch (parse_options_step(&ctx, options, usagestr)) { |
457 | case PARSE_OPT_HELP: | |
458 | exit(129); | |
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 | ||
7e7bbcb4 | 470 | return parse_options_end(&ctx); |
4a59fd13 | 471 | } |
d7a38c54 | 472 | |
9c7304e3 | 473 | static int usage_argh(const struct option *opts, FILE *outfile) |
29f25d49 SB |
474 | { |
475 | const char *s; | |
34aec9f5 | 476 | int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh; |
29f25d49 SB |
477 | if (opts->flags & PARSE_OPT_OPTARG) |
478 | if (opts->long_name) | |
479 | s = literal ? "[=%s]" : "[=<%s>]"; | |
480 | else | |
481 | s = literal ? "[%s]" : "[<%s>]"; | |
482 | else | |
483 | s = literal ? " %s" : " <%s>"; | |
9c7304e3 | 484 | return fprintf(outfile, s, opts->argh ? opts->argh : "..."); |
29f25d49 SB |
485 | } |
486 | ||
d7a38c54 PH |
487 | #define USAGE_OPTS_WIDTH 24 |
488 | #define USAGE_GAP 2 | |
489 | ||
47e9cd28 TR |
490 | static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, |
491 | const char * const *usagestr, | |
492 | const struct option *opts, int full, int err) | |
d7a38c54 | 493 | { |
9c7304e3 GS |
494 | FILE *outfile = err ? stderr : stdout; |
495 | ||
49b61802 RS |
496 | if (!usagestr) |
497 | return PARSE_OPT_HELP; | |
498 | ||
47e9cd28 TR |
499 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
500 | fprintf(outfile, "cat <<\\EOF\n"); | |
501 | ||
9c7304e3 | 502 | fprintf(outfile, "usage: %s\n", *usagestr++); |
f389c808 | 503 | while (*usagestr && **usagestr) |
9c7304e3 | 504 | fprintf(outfile, " or: %s\n", *usagestr++); |
44d86e91 | 505 | while (*usagestr) { |
9c7304e3 | 506 | fprintf(outfile, "%s%s\n", |
44d86e91 JK |
507 | **usagestr ? " " : "", |
508 | *usagestr); | |
509 | usagestr++; | |
510 | } | |
d7a38c54 PH |
511 | |
512 | if (opts->type != OPTION_GROUP) | |
9c7304e3 | 513 | fputc('\n', outfile); |
d7a38c54 PH |
514 | |
515 | for (; opts->type != OPTION_END; opts++) { | |
516 | size_t pos; | |
517 | int pad; | |
518 | ||
519 | if (opts->type == OPTION_GROUP) { | |
9c7304e3 | 520 | fputc('\n', outfile); |
d7a38c54 | 521 | if (*opts->help) |
9c7304e3 | 522 | fprintf(outfile, "%s\n", opts->help); |
d7a38c54 PH |
523 | continue; |
524 | } | |
dd3bf0f4 PH |
525 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
526 | continue; | |
d7a38c54 | 527 | |
9c7304e3 | 528 | pos = fprintf(outfile, " "); |
86b5efb2 | 529 | if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) { |
51a9949e | 530 | if (opts->flags & PARSE_OPT_NODASH) |
9c7304e3 | 531 | pos += fprintf(outfile, "%c", opts->short_name); |
51a9949e | 532 | else |
9c7304e3 | 533 | pos += fprintf(outfile, "-%c", opts->short_name); |
51a9949e | 534 | } |
d7a38c54 | 535 | if (opts->long_name && opts->short_name) |
9c7304e3 | 536 | pos += fprintf(outfile, ", "); |
d7a38c54 | 537 | if (opts->long_name) |
9c7304e3 | 538 | pos += fprintf(outfile, "--%s%s", |
86b5efb2 JS |
539 | (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "", |
540 | opts->long_name); | |
e0319ff5 | 541 | if (opts->type == OPTION_NUMBER) |
9c7304e3 | 542 | pos += fprintf(outfile, "-NUM"); |
d7a38c54 | 543 | |
34aec9f5 | 544 | if (!(opts->flags & PARSE_OPT_NOARG)) |
9c7304e3 | 545 | pos += usage_argh(opts, outfile); |
d7a38c54 | 546 | |
f389c808 AR |
547 | if (pos <= USAGE_OPTS_WIDTH) |
548 | pad = USAGE_OPTS_WIDTH - pos; | |
d7a38c54 | 549 | else { |
9c7304e3 | 550 | fputc('\n', outfile); |
d7a38c54 PH |
551 | pad = USAGE_OPTS_WIDTH; |
552 | } | |
9c7304e3 | 553 | fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
d7a38c54 | 554 | } |
9c7304e3 | 555 | fputc('\n', outfile); |
f389c808 | 556 | |
47e9cd28 TR |
557 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
558 | fputs("EOF\n", outfile); | |
559 | ||
ee68b87a | 560 | return PARSE_OPT_HELP; |
d7a38c54 | 561 | } |
0ce865b1 | 562 | |
dd3bf0f4 | 563 | void usage_with_options(const char * const *usagestr, |
ff43ec3e | 564 | const struct option *opts) |
dd3bf0f4 | 565 | { |
47e9cd28 | 566 | usage_with_options_internal(NULL, usagestr, opts, 0, 1); |
ff43ec3e | 567 | exit(129); |
dd3bf0f4 PH |
568 | } |
569 | ||
451bb210 CC |
570 | void usage_msg_opt(const char *msg, |
571 | const char * const *usagestr, | |
572 | const struct option *options) | |
573 | { | |
574 | fprintf(stderr, "%s\n\n", msg); | |
575 | usage_with_options(usagestr, options); | |
576 | } | |
577 | ||
47e9cd28 TR |
578 | static int parse_options_usage(struct parse_opt_ctx_t *ctx, |
579 | const char * const *usagestr, | |
9c7304e3 | 580 | const struct option *opts, int err) |
ee68b87a | 581 | { |
47e9cd28 | 582 | return usage_with_options_internal(ctx, usagestr, opts, 0, err); |
ee68b87a PH |
583 | } |
584 | ||
585 | ||
0ce865b1 PH |
586 | /*----- some often used options -----*/ |
587 | #include "cache.h" | |
db7244bd | 588 | |
0ce865b1 PH |
589 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
590 | { | |
591 | int v; | |
592 | ||
593 | if (!arg) { | |
594 | v = unset ? 0 : DEFAULT_ABBREV; | |
595 | } else { | |
596 | v = strtol(arg, (char **)&arg, 10); | |
597 | if (*arg) | |
598 | return opterror(opt, "expects a numerical value", 0); | |
599 | if (v && v < MINIMUM_ABBREV) | |
600 | v = MINIMUM_ABBREV; | |
601 | else if (v > 40) | |
602 | v = 40; | |
603 | } | |
604 | *(int *)(opt->value) = v; | |
605 | return 0; | |
606 | } | |
1f4a711a MB |
607 | |
608 | int parse_opt_approxidate_cb(const struct option *opt, const char *arg, | |
609 | int unset) | |
610 | { | |
611 | *(unsigned long *)(opt->value) = approxidate(arg); | |
612 | return 0; | |
613 | } | |
dbd0f5c7 | 614 | |
73e9da01 ML |
615 | int parse_opt_color_flag_cb(const struct option *opt, const char *arg, |
616 | int unset) | |
617 | { | |
618 | int value; | |
619 | ||
620 | if (!arg) | |
621 | arg = unset ? "never" : (const char *)opt->defval; | |
622 | value = git_config_colorbool(NULL, arg, -1); | |
623 | if (value < 0) | |
624 | return opterror(opt, | |
625 | "expects \"always\", \"auto\", or \"never\"", 0); | |
626 | *(int *)opt->value = value; | |
627 | return 0; | |
628 | } | |
629 | ||
7f87aff2 TA |
630 | int parse_opt_verbosity_cb(const struct option *opt, const char *arg, |
631 | int unset) | |
632 | { | |
633 | int *target = opt->value; | |
634 | ||
635 | if (unset) | |
636 | /* --no-quiet, --no-verbose */ | |
637 | *target = 0; | |
638 | else if (opt->short_name == 'v') { | |
639 | if (*target >= 0) | |
640 | (*target)++; | |
641 | else | |
642 | *target = 1; | |
643 | } else { | |
644 | if (*target <= 0) | |
645 | (*target)--; | |
646 | else | |
647 | *target = -1; | |
648 | } | |
649 | return 0; | |
650 | } | |
651 | ||
269defdf JG |
652 | int parse_opt_with_commit(const struct option *opt, const char *arg, int unset) |
653 | { | |
654 | unsigned char sha1[20]; | |
655 | struct commit *commit; | |
656 | ||
657 | if (!arg) | |
658 | return -1; | |
659 | if (get_sha1(arg, sha1)) | |
660 | return error("malformed object name %s", arg); | |
661 | commit = lookup_commit_reference(sha1); | |
662 | if (!commit) | |
663 | return error("no such commit %s", arg); | |
664 | commit_list_insert(commit, opt->value); | |
665 | return 0; | |
666 | } | |
cb6020bb JH |
667 | |
668 | int parse_opt_tertiary(const struct option *opt, const char *arg, int unset) | |
669 | { | |
670 | int *target = opt->value; | |
671 | *target = unset ? 2 : 1; | |
672 | return 0; | |
673 | } | |
8b74d75c JH |
674 | |
675 | int parse_options_concat(struct option *dst, size_t dst_size, struct option *src) | |
676 | { | |
677 | int i, j; | |
678 | ||
679 | for (i = 0; i < dst_size; i++) | |
680 | if (dst[i].type == OPTION_END) | |
681 | break; | |
682 | for (j = 0; i < dst_size; i++, j++) { | |
683 | dst[i] = src[j]; | |
684 | if (src[j].type == OPTION_END) | |
685 | return 0; | |
686 | } | |
687 | return -1; | |
688 | } |