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