Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/parse-options.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "parse-options.h"
3
#include "abspath.h"
4
#include "parse.h"
5
#include "gettext.h"
6
#include "strbuf.h"
7
#include "string-list.h"
8
#include "strmap.h"
9
#include "utf8.h"
10
11
static int disallow_abbreviated_options;
12
13
enum opt_parsed {
14
  OPT_LONG  = 0,
15
  OPT_SHORT = 1<<0,
16
  OPT_UNSET = 1<<1,
17
};
18
19
static void optbug(const struct option *opt, const char *reason)
20
0
{
21
0
  if (opt->long_name && opt->short_name)
22
0
    bug("switch '%c' (--%s) %s", opt->short_name,
23
0
        opt->long_name, reason);
24
0
  else if (opt->long_name)
25
0
    bug("option '%s' %s", opt->long_name, reason);
26
0
  else
27
0
    bug("switch '%c' %s", opt->short_name, reason);
28
0
}
29
30
static const char *optname(const struct option *opt, enum opt_parsed flags)
31
0
{
32
0
  static struct strbuf sb = STRBUF_INIT;
33
34
0
  strbuf_reset(&sb);
35
0
  if (flags & OPT_SHORT)
36
0
    strbuf_addf(&sb, "switch `%c'", opt->short_name);
37
0
  else if (flags & OPT_UNSET)
38
0
    strbuf_addf(&sb, "option `no-%s'", opt->long_name);
39
0
  else if (flags == OPT_LONG)
40
0
    strbuf_addf(&sb, "option `%s'", opt->long_name);
41
0
  else
42
0
    BUG("optname() got unknown flags %d", flags);
43
44
0
  return sb.buf;
45
0
}
46
47
static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
48
             const struct option *opt,
49
             enum opt_parsed flags, const char **arg)
50
0
{
51
0
  if (p->opt) {
52
0
    *arg = p->opt;
53
0
    p->opt = NULL;
54
0
  } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
55
0
    *arg = (const char *)opt->defval;
56
0
  } else if (p->argc > 1) {
57
0
    p->argc--;
58
0
    *arg = *++p->argv;
59
0
  } else
60
0
    return error(_("%s requires a value"), optname(opt, flags));
61
0
  return 0;
62
0
}
63
64
static char *fix_filename(const char *prefix, const char *file)
65
0
{
66
0
  if (!file || !*file)
67
0
    return NULL;
68
0
  else
69
0
    return prefix_filename_except_for_dash(prefix, file);
70
0
}
71
72
static int do_get_int_value(const void *value, size_t precision, intmax_t *ret)
73
0
{
74
0
  switch (precision) {
75
0
  case sizeof(int8_t):
76
0
    *ret = *(int8_t *)value;
77
0
    return 0;
78
0
  case sizeof(int16_t):
79
0
    *ret = *(int16_t *)value;
80
0
    return 0;
81
0
  case sizeof(int32_t):
82
0
    *ret = *(int32_t *)value;
83
0
    return 0;
84
0
  case sizeof(int64_t):
85
0
    *ret = *(int64_t *)value;
86
0
    return 0;
87
0
  default:
88
0
    return -1;
89
0
  }
90
0
}
91
92
static intmax_t get_int_value(const struct option *opt, enum opt_parsed flags)
93
0
{
94
0
  intmax_t ret;
95
0
  if (do_get_int_value(opt->value, opt->precision, &ret))
96
0
    BUG("invalid precision for option %s", optname(opt, flags));
97
0
  return ret;
98
0
}
99
100
static enum parse_opt_result set_int_value(const struct option *opt,
101
             enum opt_parsed flags,
102
             intmax_t value)
103
0
{
104
0
  switch (opt->precision) {
105
0
  case sizeof(int8_t):
106
0
    *(int8_t *)opt->value = value;
107
0
    return 0;
108
0
  case sizeof(int16_t):
109
0
    *(int16_t *)opt->value = value;
110
0
    return 0;
111
0
  case sizeof(int32_t):
112
0
    *(int32_t *)opt->value = value;
113
0
    return 0;
114
0
  case sizeof(int64_t):
115
0
    *(int64_t *)opt->value = value;
116
0
    return 0;
117
0
  default:
118
0
    BUG("invalid precision for option %s", optname(opt, flags));
119
0
  }
120
0
}
121
122
static int signed_int_fits(intmax_t value, size_t precision)
123
0
{
124
0
  size_t bits = precision * CHAR_BIT;
125
0
  intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
126
0
  intmax_t lower_bound = -upper_bound - 1;
127
0
  return lower_bound <= value && value <= upper_bound;
128
0
}
129
130
static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
131
            const struct option *opt,
132
            enum opt_parsed flags,
133
            const char **argp)
134
0
{
135
0
  const char *arg;
136
0
  const int unset = flags & OPT_UNSET;
137
138
0
  if (unset && p->opt)
139
0
    return error(_("%s takes no value"), optname(opt, flags));
140
0
  if (unset && (opt->flags & PARSE_OPT_NONEG))
141
0
    return error(_("%s isn't available"), optname(opt, flags));
142
0
  if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
143
0
    return error(_("%s takes no value"), optname(opt, flags));
144
145
0
  switch (opt->type) {
146
0
  case OPTION_LOWLEVEL_CALLBACK:
147
0
    return opt->ll_callback(p, opt, NULL, unset);
148
149
0
  case OPTION_BIT:
150
0
  {
151
0
    intmax_t value = get_int_value(opt, flags);
152
0
    if (unset)
153
0
      value &= ~opt->defval;
154
0
    else
155
0
      value |= opt->defval;
156
0
    return set_int_value(opt, flags, value);
157
0
  }
158
159
0
  case OPTION_NEGBIT:
160
0
  {
161
0
    intmax_t value = get_int_value(opt, flags);
162
0
    if (unset)
163
0
      value |= opt->defval;
164
0
    else
165
0
      value &= ~opt->defval;
166
0
    return set_int_value(opt, flags, value);
167
0
  }
168
169
0
  case OPTION_BITOP:
170
0
  {
171
0
    intmax_t value = get_int_value(opt, flags);
172
0
    if (unset)
173
0
      BUG("BITOP can't have unset form");
174
0
    value &= ~opt->extra;
175
0
    value |= opt->defval;
176
0
    return set_int_value(opt, flags, value);
177
0
  }
178
179
0
  case OPTION_COUNTUP:
180
0
  {
181
0
    size_t bits = CHAR_BIT * opt->precision;
182
0
    intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
183
0
    intmax_t value = get_int_value(opt, flags);
184
185
0
    if (value < 0)
186
0
      value = 0;
187
0
    if (unset)
188
0
      value = 0;
189
0
    else if (value < upper_bound)
190
0
      value++;
191
0
    else
192
0
      return error(_("value for %s exceeds %"PRIdMAX),
193
0
             optname(opt, flags), upper_bound);
194
0
    return set_int_value(opt, flags, value);
195
0
  }
196
197
0
  case OPTION_SET_INT:
198
0
    return set_int_value(opt, flags, unset ? 0 : opt->defval);
199
200
0
  case OPTION_STRING:
201
0
    if (unset)
202
0
      *(const char **)opt->value = NULL;
203
0
    else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
204
0
      *(const char **)opt->value = (const char *)opt->defval;
205
0
    else
206
0
      return get_arg(p, opt, flags, (const char **)opt->value);
207
0
    return 0;
208
209
0
  case OPTION_FILENAME:
210
0
  {
211
0
    const char *value;
212
0
    bool is_optional;
213
214
0
    if (unset)
215
0
      value = NULL;
216
0
    else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
217
0
      value = (const char *)opt->defval;
218
0
    else {
219
0
      int err = get_arg(p, opt, flags, &value);
220
0
      if (err)
221
0
        return err;
222
0
    }
223
0
    if (!value)
224
0
      return 0;
225
226
0
    is_optional = skip_prefix(value, ":(optional)", &value);
227
0
    value = fix_filename(p->prefix, value);
228
0
    if (is_optional && is_missing_file(value)) {
229
0
      free((char *)value);
230
0
    } else {
231
0
      FREE_AND_NULL(*(char **)opt->value);
232
0
      *(const char **)opt->value = value;
233
0
    }
234
0
    return 0;
235
0
  }
236
0
  case OPTION_CALLBACK:
237
0
  {
238
0
    const char *p_arg = NULL;
239
0
    int p_unset;
240
241
0
    if (unset)
242
0
      p_unset = 1;
243
0
    else if (opt->flags & PARSE_OPT_NOARG)
244
0
      p_unset = 0;
245
0
    else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
246
0
      p_unset = 0;
247
0
    else if (get_arg(p, opt, flags, &arg))
248
0
      return -1;
249
0
    else {
250
0
      p_unset = 0;
251
0
      p_arg = arg;
252
0
    }
253
0
    if (opt->flags & PARSE_OPT_CMDMODE)
254
0
      *argp = p_arg;
255
0
    if (opt->callback)
256
0
      return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
257
0
    else
258
0
      return (*opt->ll_callback)(p, opt, p_arg, p_unset);
259
0
  }
260
0
  case OPTION_INTEGER:
261
0
  {
262
0
    intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - CHAR_BIT * opt->precision);
263
0
    intmax_t lower_bound = -upper_bound - 1;
264
0
    intmax_t value;
265
266
0
    if (unset) {
267
0
      value = 0;
268
0
    } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
269
0
      value = opt->defval;
270
0
    } else if (get_arg(p, opt, flags, &arg)) {
271
0
      return -1;
272
0
    } else if (!*arg) {
273
0
      return error(_("%s expects a numerical value"),
274
0
             optname(opt, flags));
275
0
    } else if (!git_parse_signed(arg, &value, upper_bound)) {
276
0
      if (errno == ERANGE)
277
0
        return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
278
0
               arg, optname(opt, flags), lower_bound, upper_bound);
279
280
0
      return error(_("%s expects an integer value with an optional k/m/g suffix"),
281
0
             optname(opt, flags));
282
0
    }
283
284
0
    if (value < lower_bound)
285
0
      return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
286
0
             arg, optname(opt, flags), (intmax_t)lower_bound, (intmax_t)upper_bound);
287
288
0
    return set_int_value(opt, flags, value);
289
0
  }
290
0
  case OPTION_UNSIGNED:
291
0
  {
292
0
    uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
293
0
    uintmax_t value;
294
295
0
    if (unset) {
296
0
      value = 0;
297
0
    } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
298
0
      value = opt->defval;
299
0
    } else if (get_arg(p, opt, flags, &arg)) {
300
0
      return -1;
301
0
    } else if (!*arg) {
302
0
      return error(_("%s expects a numerical value"),
303
0
             optname(opt, flags));
304
0
    } else if (!git_parse_unsigned(arg, &value, upper_bound)) {
305
0
      if (errno == ERANGE)
306
0
        return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
307
0
               arg, optname(opt, flags), (uintmax_t) 0, upper_bound);
308
309
0
      return error(_("%s expects a non-negative integer value"
310
0
               " with an optional k/m/g suffix"),
311
0
             optname(opt, flags));
312
0
    }
313
314
0
    switch (opt->precision) {
315
0
    case 1:
316
0
      *(uint8_t *)opt->value = value;
317
0
      return 0;
318
0
    case 2:
319
0
      *(uint16_t *)opt->value = value;
320
0
      return 0;
321
0
    case 4:
322
0
      *(uint32_t *)opt->value = value;
323
0
      return 0;
324
0
    case 8:
325
0
      *(uint64_t *)opt->value = value;
326
0
      return 0;
327
0
    default:
328
0
      BUG("invalid precision for option %s",
329
0
          optname(opt, flags));
330
0
    }
331
0
  }
332
333
0
  default:
334
0
    BUG("opt->type %d should not happen", opt->type);
335
0
  }
336
0
}
337
338
struct parse_opt_cmdmode_list {
339
  intmax_t value;
340
  void *value_ptr;
341
  size_t precision;
342
  const struct option *opt;
343
  const char *arg;
344
  enum opt_parsed flags;
345
  struct parse_opt_cmdmode_list *next;
346
};
347
348
static void build_cmdmode_list(struct parse_opt_ctx_t *ctx,
349
             const struct option *opts)
350
0
{
351
0
  ctx->cmdmode_list = NULL;
352
353
0
  for (; opts->type != OPTION_END; opts++) {
354
0
    struct parse_opt_cmdmode_list *elem = ctx->cmdmode_list;
355
0
    void *value_ptr = opts->value;
356
357
0
    if (!(opts->flags & PARSE_OPT_CMDMODE) || !value_ptr)
358
0
      continue;
359
360
0
    while (elem && elem->value_ptr != value_ptr)
361
0
      elem = elem->next;
362
0
    if (elem)
363
0
      continue;
364
365
0
    CALLOC_ARRAY(elem, 1);
366
0
    elem->value_ptr = value_ptr;
367
0
    elem->precision = opts->precision;
368
0
    if (do_get_int_value(value_ptr, opts->precision, &elem->value))
369
0
      optbug(opts, "has invalid precision");
370
0
    elem->next = ctx->cmdmode_list;
371
0
    ctx->cmdmode_list = elem;
372
0
  }
373
0
  BUG_if_bug("invalid 'struct option'");
374
0
}
375
376
static char *optnamearg(const struct option *opt, const char *arg,
377
      enum opt_parsed flags)
378
0
{
379
0
  if (flags & OPT_SHORT)
380
0
    return xstrfmt("-%c%s", opt->short_name, arg ? arg : "");
381
0
  return xstrfmt("--%s%s%s%s", flags & OPT_UNSET ? "no-" : "",
382
0
           opt->long_name, arg ? "=" : "", arg ? arg : "");
383
0
}
384
385
static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
386
               const struct option *opt,
387
               enum opt_parsed flags)
388
0
{
389
0
  const char *arg = NULL;
390
0
  enum parse_opt_result result = do_get_value(p, opt, flags, &arg);
391
0
  struct parse_opt_cmdmode_list *elem = p->cmdmode_list;
392
0
  char *opt_name, *other_opt_name;
393
394
0
  for (; elem; elem = elem->next) {
395
0
    intmax_t new_value;
396
397
0
    if (do_get_int_value(elem->value_ptr, elem->precision,
398
0
             &new_value))
399
0
      BUG("impossible: invalid precision");
400
401
0
    if (new_value == elem->value)
402
0
      continue;
403
404
0
    if (elem->opt &&
405
0
        (elem->opt->flags | opt->flags) & PARSE_OPT_CMDMODE)
406
0
      break;
407
408
0
    elem->opt = opt;
409
0
    elem->arg = arg;
410
0
    elem->flags = flags;
411
0
    elem->value = new_value;
412
0
  }
413
414
0
  if (result || !elem)
415
0
    return result;
416
417
0
  opt_name = optnamearg(opt, arg, flags);
418
0
  other_opt_name = optnamearg(elem->opt, elem->arg, elem->flags);
419
0
  error(_("options '%s' and '%s' cannot be used together"),
420
0
        opt_name, other_opt_name);
421
0
  free(opt_name);
422
0
  free(other_opt_name);
423
0
  return -1;
424
0
}
425
426
static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
427
               const struct option *options)
428
0
{
429
0
  const struct option *numopt = NULL;
430
431
0
  for (; options->type != OPTION_END; options++) {
432
0
    if (options->short_name == *p->opt) {
433
0
      p->opt = p->opt[1] ? p->opt + 1 : NULL;
434
0
      return get_value(p, options, OPT_SHORT);
435
0
    }
436
437
    /*
438
     * Handle the numerical option later, explicit one-digit
439
     * options take precedence over it.
440
     */
441
0
    if (options->type == OPTION_NUMBER)
442
0
      numopt = options;
443
0
  }
444
0
  if (numopt && isdigit(*p->opt)) {
445
0
    size_t len = 1;
446
0
    char *arg;
447
0
    int rc;
448
449
0
    while (isdigit(p->opt[len]))
450
0
      len++;
451
0
    arg = xmemdupz(p->opt, len);
452
0
    p->opt = p->opt[len] ? p->opt + len : NULL;
453
0
    if (numopt->callback)
454
0
      rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
455
0
    else
456
0
      rc = (*numopt->ll_callback)(p, numopt, arg, 0);
457
0
    free(arg);
458
0
    return rc;
459
0
  }
460
0
  return PARSE_OPT_UNKNOWN;
461
0
}
462
463
static int has_string(const char *it, const char **array)
464
0
{
465
0
  while (*array)
466
0
    if (!strcmp(it, *(array++)))
467
0
      return 1;
468
0
  return 0;
469
0
}
470
471
static int is_alias(struct parse_opt_ctx_t *ctx,
472
        const struct option *one_opt,
473
        const struct option *another_opt)
474
0
{
475
0
  const char **group;
476
477
0
  if (!ctx->alias_groups)
478
0
    return 0;
479
480
0
  if (!one_opt->long_name || !another_opt->long_name)
481
0
    return 0;
482
483
0
  for (group = ctx->alias_groups; *group; group += 3) {
484
    /* it and other are from the same family? */
485
0
    if (has_string(one_opt->long_name, group) &&
486
0
        has_string(another_opt->long_name, group))
487
0
      return 1;
488
0
  }
489
0
  return 0;
490
0
}
491
492
struct parsed_option {
493
  const struct option *option;
494
  enum opt_parsed flags;
495
};
496
497
static void register_abbrev(struct parse_opt_ctx_t *p,
498
          const struct option *option, enum opt_parsed flags,
499
          struct parsed_option *abbrev,
500
          struct parsed_option *ambiguous)
501
0
{
502
0
  if (p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
503
0
    return;
504
0
  if (abbrev->option &&
505
0
      !(abbrev->flags == flags && is_alias(p, abbrev->option, option))) {
506
    /*
507
     * If this is abbreviated, it is
508
     * ambiguous. So when there is no
509
     * exact match later, we need to
510
     * error out.
511
     */
512
0
    ambiguous->option = abbrev->option;
513
0
    ambiguous->flags = abbrev->flags;
514
0
  }
515
0
  abbrev->option = option;
516
0
  abbrev->flags = flags;
517
0
}
518
519
enum long_opt_lookup {
520
  LONG_OPT_EXACT,      /* exact match found */
521
  LONG_OPT_ABBREV,     /* unambiguous abbreviation found */
522
  LONG_OPT_AMBIGUOUS,  /* more than one candidate abbreviation */
523
  LONG_OPT_UNKNOWN,    /* nothing matches */
524
};
525
526
/*
527
 * Look up the long option named by `arg` (without its leading "--")
528
 * without acting on it. (Neither prints nor dies.)
529
 *
530
 * Return value:
531
 * - On LONG_OPT_EXACT, `found` is the exact matching option and its flags
532
 *   (including OPT_UNSET for a negated form), and `stuck` points after
533
 *   the '=' or is NULL.
534
 * - On LONG_OPT_ABBREV, `found` is the unambiguous abbreviation, same as
535
 *   above for its flags and `stuck`.
536
 * - On LONG_OPT_AMBIGUOUS, `found` and `other` are two candidates of an
537
 *   ambiguous abbreviation.
538
 * - On LONG_OPT_UNKNOWN, nothing matches, no result.
539
 *
540
 * `other` is only meaningful for LONG_OPT_AMBIGUOUS.
541
 */
542
static enum long_opt_lookup lookup_long_opt(struct parse_opt_ctx_t *p,
543
              const char *arg,
544
              const struct option *options,
545
              struct parsed_option *found,
546
              struct parsed_option *other,
547
              const char **stuck)
548
0
{
549
0
  enum opt_parsed flags = OPT_LONG;
550
0
  const char *arg_end = strchrnul(arg, '=');
551
0
  const char *arg_start = arg;
552
0
  int arg_starts_with_no_no = 0;
553
554
0
  found->option = NULL;
555
0
  found->flags = OPT_LONG;
556
0
  other->option = NULL;
557
0
  other->flags = OPT_LONG;
558
0
  *stuck = NULL;
559
560
0
  if (skip_prefix(arg_start, "no-", &arg_start)) {
561
0
    if (skip_prefix(arg_start, "no-", &arg_start))
562
0
      arg_starts_with_no_no = 1;
563
0
    else
564
0
      flags |= OPT_UNSET;
565
0
  }
566
567
0
  for (; options->type != OPTION_END; options++) {
568
0
    const char *rest, *long_name = options->long_name;
569
0
    enum opt_parsed opt_flags = OPT_LONG;
570
0
    int allow_unset = !(options->flags & PARSE_OPT_NONEG);
571
572
0
    if (options->type == OPTION_SUBCOMMAND)
573
0
      continue;
574
0
    if (!long_name)
575
0
      continue;
576
577
0
    if (skip_prefix(long_name, "no-", &long_name))
578
0
      opt_flags |= OPT_UNSET;
579
0
    else if (arg_starts_with_no_no)
580
0
      continue;
581
582
0
    if (((flags ^ opt_flags) & OPT_UNSET) && !allow_unset)
583
0
      continue;
584
585
0
    if (skip_prefix(arg_start, long_name, &rest)) {
586
0
      if (*rest == '=')
587
0
        *stuck = rest + 1;
588
0
      else if (*rest)
589
0
        continue;
590
0
      found->option = options;
591
0
      found->flags = flags ^ opt_flags;
592
0
      return LONG_OPT_EXACT;
593
0
    }
594
595
    /* abbreviated? */
596
0
    if (!strncmp(long_name, arg_start, arg_end - arg_start))
597
0
      register_abbrev(p, options, flags ^ opt_flags,
598
0
          found, other);
599
600
    /* negated and abbreviated very much? */
601
0
    if (allow_unset && starts_with("no-", arg))
602
0
      register_abbrev(p, options, OPT_UNSET ^ opt_flags,
603
0
          found, other);
604
0
  }
605
606
0
  if (other->option)
607
0
    return LONG_OPT_AMBIGUOUS;
608
609
0
  if (found->option) {
610
0
    if (*arg_end)
611
0
      *stuck = arg_end + 1;
612
0
    return LONG_OPT_ABBREV;
613
0
  }
614
615
0
  return LONG_OPT_UNKNOWN;
616
0
}
617
618
static enum parse_opt_result parse_long_opt(struct parse_opt_ctx_t *p,
619
              const char *arg,
620
              const struct option *options)
621
0
{
622
0
  struct parsed_option found;
623
0
  struct parsed_option other;
624
0
  const char *stuck;
625
0
  enum long_opt_lookup result = lookup_long_opt(p, arg, options,
626
0
                  &found, &other, &stuck);
627
628
0
  switch (result) {
629
0
  case LONG_OPT_UNKNOWN:
630
0
    return PARSE_OPT_UNKNOWN;
631
0
  case LONG_OPT_ABBREV:
632
0
  case LONG_OPT_AMBIGUOUS:
633
0
    if (disallow_abbreviated_options) {
634
0
      const char *arg_end = strchrnul(arg, '=');
635
0
      die("disallowed abbreviated or ambiguous option '%.*s'",
636
0
          (int)(arg_end - arg), arg);
637
0
    }
638
0
    if (result == LONG_OPT_AMBIGUOUS) {
639
0
      error(_("ambiguous option: %s "
640
0
        "(could be --%s%s or --%s%s)"),
641
0
            arg,
642
0
            (other.flags & OPT_UNSET) ? "no-" : "",
643
0
            other.option->long_name,
644
0
            (found.flags & OPT_UNSET) ? "no-" : "",
645
0
            found.option->long_name);
646
0
      return PARSE_OPT_HELP_ERROR;
647
0
    }
648
    /* fallthru */
649
0
  case LONG_OPT_EXACT:
650
0
    p->opt = stuck;
651
0
    return get_value(p, found.option, found.flags);
652
0
  }
653
654
0
  BUG("unknown enum long_opt_lookup value '%d'", result);
655
0
}
656
657
static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
658
                const char *arg,
659
                const struct option *options)
660
0
{
661
0
  for (; options->type != OPTION_END; options++) {
662
0
    if (!(options->flags & PARSE_OPT_NODASH))
663
0
      continue;
664
0
    if (options->short_name == arg[0] && arg[1] == '\0')
665
0
      return get_value(p, options, OPT_SHORT);
666
0
  }
667
0
  return PARSE_OPT_ERROR;
668
0
}
669
670
static enum parse_opt_result parse_subcommand(const char *arg,
671
                const struct option *options)
672
0
{
673
0
  for (; options->type != OPTION_END; options++)
674
0
    if (options->type == OPTION_SUBCOMMAND &&
675
0
        !strcmp(options->long_name, arg)) {
676
0
      *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;
677
0
      return PARSE_OPT_SUBCOMMAND;
678
0
    }
679
680
0
  return PARSE_OPT_UNKNOWN;
681
0
}
682
683
static void check_typos(const char *arg, const struct option *options)
684
0
{
685
0
  if (strlen(arg) < 3)
686
0
    return;
687
688
0
  if (starts_with(arg, "no-")) {
689
0
    error(_("did you mean `--%s` (with two dashes)?"), arg);
690
0
    exit(129);
691
0
  }
692
693
0
  for (; options->type != OPTION_END; options++) {
694
0
    if (!options->long_name)
695
0
      continue;
696
0
    if (starts_with(options->long_name, arg)) {
697
0
      error(_("did you mean `--%s` (with two dashes)?"), arg);
698
0
      exit(129);
699
0
    }
700
0
  }
701
0
}
702
703
static void parse_options_check(const struct option *opts)
704
0
{
705
0
  char short_opts[128];
706
0
  bool saw_number_option = false;
707
0
  void *subcommand_value = NULL;
708
709
0
  memset(short_opts, '\0', sizeof(short_opts));
710
0
  for (; opts->type != OPTION_END; opts++) {
711
0
    if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
712
0
        (opts->flags & PARSE_OPT_OPTARG))
713
0
      optbug(opts, "uses incompatible flags "
714
0
             "LASTARG_DEFAULT and OPTARG");
715
0
    if (opts->short_name) {
716
0
      if (0x7F <= opts->short_name)
717
0
        optbug(opts, "invalid short name");
718
0
      else if (short_opts[opts->short_name]++)
719
0
        optbug(opts, "short name already used");
720
0
    }
721
0
    if (opts->type == OPTION_NUMBER) {
722
0
      if (saw_number_option)
723
0
        optbug(opts, "duplicate numerical option");
724
0
      saw_number_option = true;
725
0
    }
726
0
    if (opts->flags & PARSE_OPT_NODASH &&
727
0
        ((opts->flags & PARSE_OPT_OPTARG) ||
728
0
         !(opts->flags & PARSE_OPT_NOARG) ||
729
0
         !(opts->flags & PARSE_OPT_NONEG) ||
730
0
         opts->long_name))
731
0
      optbug(opts, "uses feature "
732
0
             "not supported for dashless options");
733
0
    if ((opts->flags & PARSE_OPT_EARLY) && !opts->long_name)
734
0
      optbug(opts, "uses PARSE_OPT_EARLY, which needs a long name");
735
0
    if (opts->type == OPTION_SET_INT && !opts->defval &&
736
0
        opts->long_name && !(opts->flags & PARSE_OPT_NONEG))
737
0
      optbug(opts, "OPTION_SET_INT 0 should not be negatable");
738
0
    switch (opts->type) {
739
0
    case OPTION_SET_INT:
740
0
    case OPTION_BIT:
741
0
    case OPTION_NEGBIT:
742
0
    case OPTION_BITOP:
743
0
    case OPTION_COUNTUP:
744
0
      if (!signed_int_fits(opts->defval, opts->precision))
745
0
        optbug(opts, "has invalid defval");
746
      /* fallthru */
747
0
    case OPTION_NUMBER:
748
0
      if ((opts->flags & PARSE_OPT_OPTARG) ||
749
0
          !(opts->flags & PARSE_OPT_NOARG))
750
0
        optbug(opts, "should not accept an argument");
751
0
      break;
752
0
    case OPTION_CALLBACK:
753
0
      if (!opts->callback && !opts->ll_callback)
754
0
        optbug(opts, "OPTION_CALLBACK needs one callback");
755
0
      else if (opts->callback && opts->ll_callback)
756
0
        optbug(opts, "OPTION_CALLBACK can't have two callbacks");
757
0
      break;
758
0
    case OPTION_LOWLEVEL_CALLBACK:
759
0
      if (!opts->ll_callback)
760
0
        optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback");
761
0
      if (opts->callback)
762
0
        optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
763
0
      break;
764
0
    case OPTION_ALIAS:
765
0
      optbug(opts, "OPT_ALIAS() should not remain at this point. "
766
0
             "Are you using parse_options_step() directly?\n"
767
0
             "That case is not supported yet.");
768
0
      break;
769
0
    case OPTION_SUBCOMMAND:
770
0
      if (!opts->value || !opts->subcommand_fn)
771
0
        optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
772
0
      if (opts->flags & PARSE_OPT_EARLY)
773
0
        optbug(opts, "OPTION_SUBCOMMAND does not support PARSE_OPT_EARLY");
774
0
      if (!subcommand_value)
775
0
        subcommand_value = opts->value;
776
0
      else if (subcommand_value != opts->value)
777
0
        optbug(opts, "all OPTION_SUBCOMMANDs need the same value");
778
0
      break;
779
0
    default:
780
0
      ; /* ok. (usually accepts an argument) */
781
0
    }
782
0
    if (opts->argh &&
783
0
        strcspn(opts->argh, " _") != strlen(opts->argh))
784
0
      optbug(opts, "multi-word argh should use dash to separate words");
785
0
  }
786
0
  BUG_if_bug("invalid 'struct option'");
787
0
}
788
789
static void parse_options_check_harder(const struct option *opts)
790
0
{
791
0
  struct strset long_names = STRSET_INIT;
792
793
0
  for (; opts->type != OPTION_END; opts++) {
794
0
    if (opts->long_name) {
795
0
      if (!strset_add(&long_names, opts->long_name))
796
0
        optbug(opts, "long name already used");
797
0
    }
798
0
  }
799
0
  BUG_if_bug("invalid 'struct option'");
800
0
  strset_clear(&long_names);
801
0
}
802
803
static int has_subcommands(const struct option *options)
804
0
{
805
0
  for (; options->type != OPTION_END; options++)
806
0
    if (options->type == OPTION_SUBCOMMAND)
807
0
      return 1;
808
0
  return 0;
809
0
}
810
811
static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
812
          int argc, const char **argv, const char *prefix,
813
          const struct option *options,
814
          enum parse_opt_flags flags)
815
0
{
816
0
  ctx->argc = argc;
817
0
  ctx->argv = argv;
818
0
  if (!(flags & PARSE_OPT_ONE_SHOT)) {
819
0
    ctx->argc--;
820
0
    ctx->argv++;
821
0
  }
822
0
  ctx->total = ctx->argc;
823
0
  ctx->out   = argv;
824
0
  ctx->prefix = prefix;
825
0
  ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
826
0
  ctx->flags = flags;
827
0
  ctx->has_subcommands = has_subcommands(options);
828
0
  if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL))
829
0
    BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
830
0
  if (ctx->has_subcommands) {
831
0
    if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
832
0
      BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
833
0
    if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
834
0
      if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
835
0
        BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
836
0
      if (flags & PARSE_OPT_KEEP_DASHDASH)
837
0
        BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
838
0
    }
839
0
  }
840
0
  if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
841
0
      (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
842
0
      !(flags & PARSE_OPT_ONE_SHOT))
843
0
    BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
844
0
  if ((flags & PARSE_OPT_ONE_SHOT) &&
845
0
      (flags & PARSE_OPT_KEEP_ARGV0))
846
0
    BUG("Can't keep argv0 if you don't have it");
847
0
  parse_options_check(options);
848
0
  build_cmdmode_list(ctx, options);
849
0
}
850
851
void parse_options_start(struct parse_opt_ctx_t *ctx,
852
       int argc, const char **argv, const char *prefix,
853
       const struct option *options,
854
       enum parse_opt_flags flags)
855
0
{
856
0
  memset(ctx, 0, sizeof(*ctx));
857
0
  parse_options_start_1(ctx, argc, argv, prefix, options, flags);
858
0
}
859
860
static void show_negated_gitcomp(const struct option *opts, int show_all,
861
         int nr_noopts)
862
0
{
863
0
  int printed_dashdash = 0;
864
865
0
  for (; opts->type != OPTION_END; opts++) {
866
0
    int has_unset_form = 0;
867
0
    const char *name;
868
869
0
    if (!opts->long_name)
870
0
      continue;
871
0
    if (!show_all &&
872
0
      (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
873
0
      continue;
874
0
    if (opts->flags & PARSE_OPT_NONEG)
875
0
      continue;
876
877
0
    switch (opts->type) {
878
0
    case OPTION_STRING:
879
0
    case OPTION_FILENAME:
880
0
    case OPTION_INTEGER:
881
0
    case OPTION_UNSIGNED:
882
0
    case OPTION_CALLBACK:
883
0
    case OPTION_BIT:
884
0
    case OPTION_NEGBIT:
885
0
    case OPTION_COUNTUP:
886
0
    case OPTION_SET_INT:
887
0
      has_unset_form = 1;
888
0
      break;
889
0
    default:
890
0
      break;
891
0
    }
892
0
    if (!has_unset_form)
893
0
      continue;
894
895
0
    if (skip_prefix(opts->long_name, "no-", &name)) {
896
0
      if (nr_noopts < 0)
897
0
        printf(" --%s", name);
898
0
    } else if (nr_noopts >= 0) {
899
0
      if (nr_noopts && !printed_dashdash) {
900
0
        printf(" --");
901
0
        printed_dashdash = 1;
902
0
      }
903
0
      printf(" --no-%s", opts->long_name);
904
0
      nr_noopts++;
905
0
    }
906
0
  }
907
0
}
908
909
int parse_options_takes_argument(const struct option *opt)
910
0
{
911
0
  switch (opt->type) {
912
0
  case OPTION_STRING:
913
0
  case OPTION_FILENAME:
914
0
  case OPTION_INTEGER:
915
0
  case OPTION_UNSIGNED:
916
0
  case OPTION_CALLBACK:
917
0
    break;
918
0
  default:
919
0
    return 0;
920
0
  }
921
922
0
  if (opt->flags & (PARSE_OPT_NOARG | PARSE_OPT_OPTARG))
923
0
    return 0;
924
925
0
  return 1;
926
0
}
927
928
static int show_gitcomp(const struct option *opts, int show_all)
929
0
{
930
0
  const struct option *original_opts = opts;
931
0
  int nr_noopts = 0;
932
0
  bool shown = false;
933
934
0
  for (; opts->type != OPTION_END; opts++) {
935
0
    const char *prefix = "--";
936
0
    const char *suffix = "";
937
938
0
    if (!opts->long_name)
939
0
      continue;
940
0
    if (!show_all &&
941
0
      (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
942
0
      continue;
943
944
0
    switch (opts->type) {
945
0
    case OPTION_SUBCOMMAND:
946
0
      prefix = "";
947
0
      break;
948
0
    case OPTION_GROUP:
949
0
      continue;
950
0
    default:
951
0
      if (parse_options_takes_argument(opts) &&
952
0
          !(opts->flags & PARSE_OPT_LASTARG_DEFAULT))
953
0
        suffix = "=";
954
0
      break;
955
0
    }
956
0
    if (opts->flags & PARSE_OPT_COMP_ARG)
957
0
      suffix = "=";
958
0
    if (starts_with(opts->long_name, "no-"))
959
0
      nr_noopts++;
960
0
    printf("%s%s%s%s", shown ? " " : "",
961
0
           prefix, opts->long_name, suffix);
962
0
    shown = true;
963
0
  }
964
0
  show_negated_gitcomp(original_opts, show_all, -1);
965
0
  show_negated_gitcomp(original_opts, show_all, nr_noopts);
966
0
  fputc('\n', stdout);
967
0
  return PARSE_OPT_COMPLETE;
968
0
}
969
970
/*
971
 * Scan and may produce a new option[] array, which should be used
972
 * instead of the original 'options'.
973
 *
974
 * Right now this is only used to preprocess and substitute
975
 * OPTION_ALIAS.
976
 *
977
 * The returned options should be freed using free_preprocessed_options.
978
 */
979
static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
980
           const struct option *options)
981
0
{
982
0
  struct option *newopt;
983
0
  int i, nr, alias;
984
0
  int nr_aliases = 0;
985
986
0
  for (nr = 0; options[nr].type != OPTION_END; nr++) {
987
0
    if (options[nr].type == OPTION_ALIAS)
988
0
      nr_aliases++;
989
0
  }
990
991
0
  if (!nr_aliases)
992
0
    return NULL;
993
994
0
  DUP_ARRAY(newopt, options, nr + 1);
995
996
  /* each alias has two string pointers and NULL */
997
0
  CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
998
999
0
  for (alias = 0, i = 0; i < nr; i++) {
1000
0
    int short_name;
1001
0
    const char *long_name;
1002
0
    const char *source;
1003
0
    struct strbuf help = STRBUF_INIT;
1004
0
    enum parse_opt_option_flags flags;
1005
0
    int j;
1006
1007
0
    if (newopt[i].type != OPTION_ALIAS)
1008
0
      continue;
1009
1010
0
    short_name = newopt[i].short_name;
1011
0
    long_name = newopt[i].long_name;
1012
0
    source = newopt[i].value;
1013
0
    flags = newopt[i].flags;
1014
1015
0
    if (!long_name)
1016
0
      BUG("An alias must have long option name");
1017
0
    strbuf_addf(&help, _("alias of --%s"), source);
1018
1019
0
    for (j = 0; j < nr; j++) {
1020
0
      const char *name = options[j].long_name;
1021
1022
0
      if (!name || strcmp(name, source))
1023
0
        continue;
1024
1025
0
      if (options[j].type == OPTION_ALIAS)
1026
0
        BUG("No please. Nested aliases are not supported.");
1027
1028
0
      memcpy(newopt + i, options + j, sizeof(*newopt));
1029
0
      newopt[i].short_name = short_name;
1030
0
      newopt[i].long_name = long_name;
1031
0
      newopt[i].help = strbuf_detach(&help, NULL);
1032
0
      newopt[i].flags |= flags | PARSE_OPT_FROM_ALIAS;
1033
0
      break;
1034
0
    }
1035
1036
0
    if (j == nr)
1037
0
      BUG("could not find source option '%s' of alias '%s'",
1038
0
          source, newopt[i].long_name);
1039
0
    ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
1040
0
    ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
1041
0
    ctx->alias_groups[alias * 3 + 2] = NULL;
1042
0
    alias++;
1043
0
  }
1044
1045
0
  return newopt;
1046
0
}
1047
1048
static void free_preprocessed_options(struct option *options)
1049
0
{
1050
0
  int i;
1051
1052
0
  if (!options)
1053
0
    return;
1054
1055
0
  for (i = 0; options[i].type != OPTION_END; i++) {
1056
0
    if (options[i].flags & PARSE_OPT_FROM_ALIAS)
1057
0
      free((void *)options[i].help);
1058
0
  }
1059
0
  free(options);
1060
0
}
1061
1062
0
#define USAGE_NORMAL 0
1063
0
#define USAGE_FULL 1
1064
0
#define USAGE_TO_STDOUT 0
1065
0
#define USAGE_TO_STDERR 1
1066
1067
static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
1068
               const char * const *,
1069
               const struct option *,
1070
               int full_usage,
1071
               int usage_to_stderr);
1072
1073
enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
1074
           const struct option *options,
1075
           const char * const usagestr[])
1076
0
{
1077
0
  int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
1078
1079
  /* we must reset ->opt, unknown short option leave it dangling */
1080
0
  ctx->opt = NULL;
1081
1082
0
  for (; ctx->argc; ctx->argc--, ctx->argv++) {
1083
0
    const char *arg = ctx->argv[0];
1084
1085
0
    if (ctx->flags & PARSE_OPT_ONE_SHOT &&
1086
0
        ctx->argc != ctx->total)
1087
0
      break;
1088
1089
0
    if (*arg != '-' || !arg[1]) {
1090
0
      if (parse_nodash_opt(ctx, arg, options) == 0)
1091
0
        continue;
1092
0
      if (!ctx->has_subcommands) {
1093
0
        if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
1094
0
          return PARSE_OPT_NON_OPTION;
1095
0
        ctx->out[ctx->cpidx++] = ctx->argv[0];
1096
0
        continue;
1097
0
      }
1098
0
      switch (parse_subcommand(arg, options)) {
1099
0
      case PARSE_OPT_SUBCOMMAND:
1100
0
        return PARSE_OPT_SUBCOMMAND;
1101
0
      case PARSE_OPT_UNKNOWN:
1102
0
        if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
1103
          /*
1104
           * arg is neither a short or long
1105
           * option nor a subcommand.  Since
1106
           * this command has a default
1107
           * operation mode, we have to treat
1108
           * this arg and all remaining args
1109
           * as args meant to that default
1110
           * operation mode.
1111
           * So we are done parsing.
1112
           */
1113
0
          return PARSE_OPT_DONE;
1114
0
        error(_("unknown subcommand: `%s'"), arg);
1115
0
        usage_with_options(usagestr, options);
1116
0
      case PARSE_OPT_COMPLETE:
1117
0
      case PARSE_OPT_HELP:
1118
0
      case PARSE_OPT_HELP_ERROR:
1119
0
      case PARSE_OPT_ERROR:
1120
0
      case PARSE_OPT_DONE:
1121
0
      case PARSE_OPT_NON_OPTION:
1122
        /* Impossible. */
1123
0
        BUG("parse_subcommand() cannot return these");
1124
0
      }
1125
0
    }
1126
1127
    /* lone -h asks for help */
1128
0
    if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
1129
0
      goto show_usage;
1130
1131
    /*
1132
     * lone --git-completion-helper and --git-completion-helper-all
1133
     * are asked by git-completion.bash
1134
     */
1135
0
    if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
1136
0
      return show_gitcomp(options, 0);
1137
0
    if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
1138
0
      return show_gitcomp(options, 1);
1139
1140
0
    if (arg[1] != '-') {
1141
0
      ctx->opt = arg + 1;
1142
0
      switch (parse_short_opt(ctx, options)) {
1143
0
      case PARSE_OPT_ERROR:
1144
0
        return PARSE_OPT_ERROR;
1145
0
      case PARSE_OPT_UNKNOWN:
1146
0
        if (ctx->opt)
1147
0
          check_typos(arg + 1, options);
1148
0
        if (internal_help && *ctx->opt == 'h')
1149
0
          goto show_usage;
1150
0
        goto unknown;
1151
0
      case PARSE_OPT_NON_OPTION:
1152
0
      case PARSE_OPT_SUBCOMMAND:
1153
0
      case PARSE_OPT_HELP:
1154
0
      case PARSE_OPT_HELP_ERROR:
1155
0
      case PARSE_OPT_COMPLETE:
1156
0
        BUG("parse_short_opt() cannot return these");
1157
0
      case PARSE_OPT_DONE:
1158
0
        break;
1159
0
      }
1160
0
      if (ctx->opt)
1161
0
        check_typos(arg + 1, options);
1162
0
      while (ctx->opt) {
1163
0
        switch (parse_short_opt(ctx, options)) {
1164
0
        case PARSE_OPT_ERROR:
1165
0
          return PARSE_OPT_ERROR;
1166
0
        case PARSE_OPT_UNKNOWN:
1167
0
          if (internal_help && *ctx->opt == 'h')
1168
0
            goto show_usage;
1169
1170
          /* fake a short option thing to hide the fact that we may have
1171
           * started to parse aggregated stuff
1172
           *
1173
           * This is leaky, too bad.
1174
           */
1175
0
          ctx->argv[0] = xstrdup(ctx->opt - 1);
1176
0
          *(char *)ctx->argv[0] = '-';
1177
0
          goto unknown;
1178
0
        case PARSE_OPT_NON_OPTION:
1179
0
        case PARSE_OPT_SUBCOMMAND:
1180
0
        case PARSE_OPT_COMPLETE:
1181
0
        case PARSE_OPT_HELP:
1182
0
        case PARSE_OPT_HELP_ERROR:
1183
0
          BUG("parse_short_opt() cannot return these");
1184
0
        case PARSE_OPT_DONE:
1185
0
          break;
1186
0
        }
1187
0
      }
1188
0
      continue;
1189
0
    }
1190
1191
0
    if (!arg[2] /* "--" */) {
1192
0
      if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
1193
0
        ctx->argc--;
1194
0
        ctx->argv++;
1195
0
      }
1196
0
      break;
1197
0
    } else if (!strcmp(arg + 2, "end-of-options")) {
1198
0
      if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
1199
0
        ctx->argc--;
1200
0
        ctx->argv++;
1201
0
      }
1202
0
      break;
1203
0
    }
1204
1205
0
    if (internal_help && !strcmp(arg + 2, "help-all"))
1206
0
      return usage_with_options_internal(ctx, usagestr, options,
1207
0
                 USAGE_FULL, USAGE_TO_STDOUT);
1208
0
    if (internal_help && !strcmp(arg + 2, "help"))
1209
0
      goto show_usage;
1210
0
    switch (parse_long_opt(ctx, arg + 2, options)) {
1211
0
    case PARSE_OPT_ERROR:
1212
0
      return PARSE_OPT_ERROR;
1213
0
    case PARSE_OPT_UNKNOWN:
1214
0
      goto unknown;
1215
0
    case PARSE_OPT_HELP:
1216
0
      goto show_usage;
1217
0
    case PARSE_OPT_HELP_ERROR:
1218
0
      goto show_usage_stderr;
1219
0
    case PARSE_OPT_NON_OPTION:
1220
0
    case PARSE_OPT_SUBCOMMAND:
1221
0
    case PARSE_OPT_COMPLETE:
1222
0
      BUG("parse_long_opt() cannot return these");
1223
0
    case PARSE_OPT_DONE:
1224
0
      break;
1225
0
    }
1226
0
    continue;
1227
0
unknown:
1228
0
    if (ctx->flags & PARSE_OPT_ONE_SHOT)
1229
0
      break;
1230
0
    if (ctx->has_subcommands &&
1231
0
        (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) &&
1232
0
        (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
1233
      /*
1234
       * Found an unknown option given to a command with
1235
       * subcommands that have a default operation mode:
1236
       * we treat this option and all remaining args as
1237
       * arguments meant to that default operation mode.
1238
       * So we are done parsing.
1239
       */
1240
0
      return PARSE_OPT_DONE;
1241
0
    }
1242
0
    if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
1243
0
      return PARSE_OPT_UNKNOWN;
1244
0
    ctx->out[ctx->cpidx++] = ctx->argv[0];
1245
0
    ctx->opt = NULL;
1246
0
  }
1247
0
  return PARSE_OPT_DONE;
1248
1249
0
 show_usage:
1250
0
  return usage_with_options_internal(ctx, usagestr, options,
1251
0
             USAGE_NORMAL, USAGE_TO_STDOUT);
1252
0
 show_usage_stderr:
1253
0
  return usage_with_options_internal(ctx, usagestr, options,
1254
0
             USAGE_NORMAL, USAGE_TO_STDERR);
1255
0
}
1256
1257
int parse_options_end(struct parse_opt_ctx_t *ctx)
1258
0
{
1259
0
  if (ctx->flags & PARSE_OPT_ONE_SHOT)
1260
0
    return ctx->total - ctx->argc;
1261
1262
0
  MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
1263
0
  ctx->out[ctx->cpidx + ctx->argc] = NULL;
1264
0
  return ctx->cpidx + ctx->argc;
1265
0
}
1266
1267
int parse_options(int argc, const char **argv,
1268
      const char *prefix,
1269
      const struct option *options,
1270
      const char * const usagestr[],
1271
      enum parse_opt_flags flags)
1272
0
{
1273
0
  struct parse_opt_ctx_t ctx;
1274
0
  struct option *real_options;
1275
1276
0
  disallow_abbreviated_options =
1277
0
    git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
1278
1279
0
  memset(&ctx, 0, sizeof(ctx));
1280
0
  real_options = preprocess_options(&ctx, options);
1281
0
  if (real_options)
1282
0
    options = real_options;
1283
0
  parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
1284
0
  switch (parse_options_step(&ctx, options, usagestr)) {
1285
0
  case PARSE_OPT_HELP:
1286
0
    exit(0);
1287
0
  case PARSE_OPT_HELP_ERROR:
1288
0
  case PARSE_OPT_ERROR:
1289
0
    exit(129);
1290
0
  case PARSE_OPT_COMPLETE:
1291
0
    exit(0);
1292
0
  case PARSE_OPT_NON_OPTION:
1293
0
  case PARSE_OPT_SUBCOMMAND:
1294
0
    break;
1295
0
  case PARSE_OPT_DONE:
1296
0
    if (ctx.has_subcommands &&
1297
0
        !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
1298
0
      error(_("need a subcommand"));
1299
0
      usage_with_options(usagestr, options);
1300
0
    }
1301
0
    break;
1302
0
  case PARSE_OPT_UNKNOWN:
1303
0
    if (ctx.argv[0][1] == '-') {
1304
0
      error(_("unknown option `%s'"), ctx.argv[0] + 2);
1305
0
    } else if (isascii(*ctx.opt)) {
1306
0
      error(_("unknown switch `%c'"), *ctx.opt);
1307
0
    } else {
1308
0
      error(_("unknown non-ascii option in string: `%s'"),
1309
0
            ctx.argv[0]);
1310
0
    }
1311
0
    usage_with_options(usagestr, options);
1312
0
  }
1313
1314
0
  precompose_argv_prefix(argc, argv, NULL);
1315
0
  free_preprocessed_options(real_options);
1316
0
  free(ctx.alias_groups);
1317
0
  for (struct parse_opt_cmdmode_list *elem = ctx.cmdmode_list; elem;) {
1318
0
    struct parse_opt_cmdmode_list *next = elem->next;
1319
0
    free(elem);
1320
0
    elem = next;
1321
0
  }
1322
0
  return parse_options_end(&ctx);
1323
0
}
1324
1325
/*
1326
 * Look for `name`, a long option without its leading "--", among
1327
 * `option`, the way parse_options() would. Return true if it names an
1328
 * option exactly, or as an unambiguous abbreviation that is not
1329
 * disallowed, with `found` set to that option and its flags, and
1330
 * `stuck` to the value stuck to it, if any, or to NULL.
1331
 */
1332
static bool find_early_scan_option(struct parse_opt_ctx_t *p,
1333
           const char *name,
1334
           const struct option *option,
1335
           bool disallow_abbrev,
1336
           struct parsed_option *found,
1337
           const char **stuck)
1338
0
{
1339
0
  struct parsed_option other;
1340
0
  enum long_opt_lookup result = lookup_long_opt(p, name, option,
1341
0
                  found, &other, stuck);
1342
1343
0
  switch (result) {
1344
0
  case LONG_OPT_EXACT:
1345
0
    return true;
1346
0
  case LONG_OPT_ABBREV:
1347
0
    return !disallow_abbrev;
1348
0
  case LONG_OPT_AMBIGUOUS:
1349
0
  case LONG_OPT_UNKNOWN:
1350
0
    return false;
1351
0
  }
1352
1353
0
  BUG("unknown enum long_opt_lookup value '%d'", result);
1354
0
}
1355
1356
struct early_scan_state {
1357
  struct parse_opt_ctx_t ctx;
1358
  const struct option *option;
1359
  enum early_scan_flags flags;
1360
  early_scan_fn *fn;
1361
  void *data;
1362
  bool disallow_abbrev;
1363
};
1364
1365
/*
1366
 * Look at argv[*i]. Return 'true' to go on scanning, with *i on the
1367
 * last argument consumed. Return 'false' to stop, with *i set to the
1368
 * first argument not consumed.
1369
 */
1370
static bool early_scan_one_option(struct early_scan_state *s,
1371
          int argc, const char **argv, int *i)
1372
0
{
1373
0
  const char *name;
1374
0
  const char *value;
1375
0
  struct parsed_option found;
1376
0
  bool unset;
1377
0
  int pos = *i;
1378
0
  const char *arg = argv[pos];
1379
1380
  /*
1381
   * parse_options() always stops parsing options at these,
1382
   * whatever its flags, so nothing after them is an option.
1383
   */
1384
0
  if (!strcmp(arg, "--") || !strcmp(arg, "--end-of-options"))
1385
0
    return false;
1386
1387
  /* non-option */
1388
0
  if (*arg != '-' || !arg[1])
1389
0
    return !(s->flags & EARLY_SCAN_STOP_AT_NON_OPTION);
1390
1391
  /* short option */
1392
0
  if (!skip_prefix(arg, "--", &name))
1393
0
    return false;
1394
1395
  /* unknown, ambiguous or disallowed abbreviation */
1396
0
  if (!find_early_scan_option(&s->ctx, name, s->option,
1397
0
            s->disallow_abbrev, &found, &value))
1398
0
    return false;
1399
1400
  /* A low-level callback may consume any number of arguments. */
1401
0
  if (found.option->ll_callback)
1402
0
    return false;
1403
1404
0
  unset = !!(found.flags & OPT_UNSET);
1405
1406
  /* parse_options() rejects these, so don't go further. */
1407
0
  if (value && (unset || (found.option->flags & PARSE_OPT_NOARG)))
1408
0
    return false;
1409
1410
  /*
1411
   * Like get_arg(), take the next argument as the value when it is
1412
   * not stuck to the option. When there is no next argument,
1413
   * parse_options() uses the default value of an option with
1414
   * PARSE_OPT_LASTARG_DEFAULT, but errors out for other options.
1415
   */
1416
0
  if (!value && !unset && parse_options_takes_argument(found.option)) {
1417
0
    if (pos + 1 < argc)
1418
0
      value = argv[++*i];
1419
0
    else if (!(found.option->flags & PARSE_OPT_LASTARG_DEFAULT))
1420
0
      return false;
1421
0
  }
1422
1423
0
  if ((found.option->flags & PARSE_OPT_EARLY) &&
1424
0
      s->fn(found.option, value, unset, pos, s->data)) {
1425
    /* the option, and its value if any, have been consumed */
1426
0
    ++*i;
1427
0
    return false;
1428
0
  }
1429
1430
0
  return true;
1431
0
}
1432
1433
int early_scan_options(int argc, const char **argv,
1434
           const struct option *option,
1435
           enum early_scan_flags flags,
1436
           early_scan_fn *fn, void *data)
1437
0
{
1438
0
  struct early_scan_state s = {
1439
0
    .flags = flags,
1440
0
    .fn = fn,
1441
0
    .data = data,
1442
0
    .disallow_abbrev =
1443
0
      git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0),
1444
0
  };
1445
0
  struct option *real_option = preprocess_options(&s.ctx, option);
1446
0
  int i;
1447
1448
0
  s.option = real_option ? real_option : option;
1449
0
  parse_options_check(s.option);
1450
1451
0
  for (i = 0; i < argc; i++)
1452
0
    if (!early_scan_one_option(&s, argc, argv, &i))
1453
0
      break;
1454
1455
0
  free_preprocessed_options(real_option);
1456
0
  free(s.ctx.alias_groups);
1457
0
  return i;
1458
0
}
1459
1460
static int usage_argh(const struct option *opts, FILE *outfile)
1461
0
{
1462
0
  const char *s;
1463
0
  int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1464
0
    !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
1465
0
  if (opts->flags & PARSE_OPT_OPTARG)
1466
0
    if (opts->long_name)
1467
      /*
1468
       * TRANSLATORS: The "<%s>" part of this string
1469
       * stands for an optional value given to a command
1470
       * line option in the long form, and "<>" is there
1471
       * as a convention to signal that it is a
1472
       * placeholder (i.e. the user should substitute it
1473
       * with the real value).  If your language uses a
1474
       * different convention, you can change "<%s>" part
1475
       * to match yours, e.g. it might use "|%s|" instead,
1476
       * or if the alphabet is different enough it may use
1477
       * "%s" without any placeholder signal.  Most
1478
       * translations leave this message as is.
1479
       */
1480
0
      s = literal ? "[=%s]" : _("[=<%s>]");
1481
0
    else
1482
      /*
1483
       * TRANSLATORS: The "<%s>" part of this string
1484
       * stands for an optional value given to a command
1485
       * line option in the short form, and "<>" is there
1486
       * as a convention to signal that it is a
1487
       * placeholder (i.e. the user should substitute it
1488
       * with the real value).  If your language uses a
1489
       * different convention, you can change "<%s>" part
1490
       * to match yours, e.g. it might use "|%s|" instead,
1491
       * or if the alphabet is different enough it may use
1492
       * "%s" without any placeholder signal.  Most
1493
       * translations leave this message as is.
1494
       */
1495
0
      s = literal ? "[%s]" : _("[<%s>]");
1496
0
  else
1497
    /*
1498
     * TRANSLATORS: The "<%s>" part of this string stands for a
1499
     * value given to a command line option, and "<>" is there
1500
     * as a convention to signal that it is a placeholder
1501
     * (i.e. the user should substitute it with the real value).
1502
     * If your language uses a different convention, you can
1503
     * change "<%s>" part to match yours, e.g. it might use
1504
     * "|%s|" instead, or if the alphabet is different enough it
1505
     * may use "%s" without any placeholder signal.  Most
1506
     * translations leave this message as is.
1507
     */
1508
0
    s = literal ? " %s" : _(" <%s>");
1509
0
  return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
1510
0
}
1511
1512
static int usage_indent(FILE *outfile)
1513
0
{
1514
0
  return fprintf(outfile, "    ");
1515
0
}
1516
1517
0
#define USAGE_OPTS_WIDTH 26
1518
1519
static void usage_padding(FILE *outfile, size_t pos)
1520
0
{
1521
0
  if (pos < USAGE_OPTS_WIDTH)
1522
0
    fprintf(outfile, "%*s", USAGE_OPTS_WIDTH - (int)pos, "");
1523
0
  else
1524
0
    fprintf(outfile, "\n%*s", USAGE_OPTS_WIDTH, "");
1525
0
}
1526
1527
static const struct option *find_option_by_long_name(const struct option *opts,
1528
                 const char *long_name)
1529
0
{
1530
0
  for (; opts->type != OPTION_END; opts++) {
1531
0
    if (opts->long_name && !strcmp(opts->long_name, long_name))
1532
0
      return opts;
1533
0
  }
1534
0
  return NULL;
1535
0
}
1536
1537
static int usage_print_flag(const struct option *opt,
1538
          FILE *outfile,
1539
          const char **positive_name)
1540
0
{
1541
0
  int off = 0;
1542
1543
0
  if (opt->short_name) {
1544
0
    if (opt->flags & PARSE_OPT_NODASH)
1545
0
      off += fprintf(outfile, "%c", opt->short_name);
1546
0
    else
1547
0
      off += fprintf(outfile, "-%c", opt->short_name);
1548
0
  }
1549
0
  if (opt->long_name && opt->short_name)
1550
0
    off += fprintf(outfile, ", ");
1551
0
  if (opt->long_name) {
1552
0
    const char *long_name = opt->long_name;
1553
0
    if ((opt->flags & PARSE_OPT_NONEG) ||
1554
0
        skip_prefix(long_name, "no-", positive_name))
1555
0
      off += fprintf(outfile, "--%s", long_name);
1556
0
    else
1557
0
      off += fprintf(outfile, "--[no-]%s", long_name);
1558
0
  }
1559
1560
0
  if (opt->type == OPTION_NUMBER)
1561
0
    off += utf8_fprintf(outfile, _("-NUM"));
1562
1563
0
  if ((opt->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1564
0
      !(opt->flags & PARSE_OPT_NOARG))
1565
0
    off += usage_argh(opt, outfile);
1566
1567
0
  return off;
1568
0
}
1569
1570
static void usage_print_option(const struct option *opt,
1571
             const struct option *all_opts,
1572
             int full,
1573
             int *need_newline,
1574
             FILE *outfile)
1575
0
{
1576
0
  const char *positive_name = NULL;
1577
0
  const char *cp, *np;
1578
0
  size_t pos;
1579
1580
0
  if (opt->type == OPTION_SUBCOMMAND && !opt->help)
1581
0
    return;
1582
0
  if (!full && (opt->flags & PARSE_OPT_HIDDEN))
1583
0
    return;
1584
0
  if (opt->type == OPTION_GROUP) {
1585
0
    fputc('\n', outfile);
1586
0
    *need_newline = 0;
1587
0
    if (*opt->help)
1588
0
      fprintf(outfile, "%s\n", _(opt->help));
1589
0
    return;
1590
0
  }
1591
1592
0
  if (*need_newline) {
1593
0
    fputc('\n', outfile);
1594
0
    *need_newline = 0;
1595
0
  }
1596
1597
0
  pos = usage_indent(outfile);
1598
0
  if (opt->type == OPTION_SUBCOMMAND)
1599
0
    pos += fprintf(outfile, "%s", opt->long_name);
1600
0
  else
1601
0
    pos += usage_print_flag(opt, outfile, &positive_name);
1602
1603
0
  if (opt->type == OPTION_ALIAS) {
1604
0
    usage_padding(outfile, pos);
1605
0
    fprintf_ln(outfile, _("alias of --%s"),
1606
0
         (const char *)opt->value);
1607
0
    return;
1608
0
  }
1609
1610
0
  for (cp = opt->help ? _(opt->help) : ""; *cp; cp = np) {
1611
0
    np = strchrnul(cp, '\n');
1612
0
    if (*np)
1613
0
      np++;
1614
0
    usage_padding(outfile, pos);
1615
0
    fwrite(cp, 1, np - cp, outfile);
1616
0
    pos = 0;
1617
0
  }
1618
0
  fputc('\n', outfile);
1619
1620
0
  if (positive_name) {
1621
0
    if (find_option_by_long_name(all_opts, positive_name))
1622
0
      return;
1623
0
    pos = usage_indent(outfile);
1624
0
    pos += fprintf(outfile, "--%s", positive_name);
1625
0
    usage_padding(outfile, pos);
1626
0
    fprintf_ln(outfile, _("opposite of --no-%s"),
1627
0
         positive_name);
1628
0
  }
1629
0
}
1630
1631
static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
1632
               const char * const *usagestr,
1633
               const struct option *opts,
1634
               int full, int err)
1635
0
{
1636
0
  const struct option *all_opts = opts;
1637
0
  FILE *outfile = err ? stderr : stdout;
1638
0
  int need_newline;
1639
1640
0
  const char *usage_prefix = _("usage: %s");
1641
  /*
1642
   * The translation could be anything, but we can count on
1643
   * msgfmt(1)'s --check option to have asserted that "%s" is in
1644
   * the translation. So compute the length of the "usage: "
1645
   * part. We are assuming that the translator wasn't overly
1646
   * clever and used e.g. "%1$s" instead of "%s", there's only
1647
   * one "%s" in "usage_prefix" above, so there's no reason to
1648
   * do so even with a RTL language.
1649
   */
1650
0
  size_t usage_len = strlen(usage_prefix) - strlen("%s");
1651
  /*
1652
   * TRANSLATORS: the colon here should align with the
1653
   * one in "usage: %s" translation.
1654
   */
1655
0
  const char *or_prefix = _("   or: %s");
1656
  /*
1657
   * TRANSLATORS: You should only need to translate this format
1658
   * string if your language is a RTL language (e.g. Arabic,
1659
   * Hebrew etc.), not if it's a LTR language (e.g. German,
1660
   * Russian, Chinese etc.).
1661
   *
1662
   * When a translated usage string has an embedded "\n" it's
1663
   * because options have wrapped to the next line. The line
1664
   * after the "\n" will then be padded to align with the
1665
   * command name, such as N_("git cmd [opt]\n<8
1666
   * spaces>[opt2]"), where the 8 spaces are the same length as
1667
   * "git cmd ".
1668
   *
1669
   * This format string prints out that already-translated
1670
   * line. The "%*s" is whitespace padding to account for the
1671
   * padding at the start of the line that we add in this
1672
   * function. The "%s" is a line in the (hopefully already
1673
   * translated) N_() usage string, which contained embedded
1674
   * newlines before we split it up.
1675
   */
1676
0
  const char *usage_continued = _("%*s%s");
1677
0
  const char *prefix = usage_prefix;
1678
0
  int saw_empty_line = 0;
1679
1680
0
  parse_options_check_harder(opts);
1681
1682
0
  if (!usagestr)
1683
0
    return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP;
1684
1685
0
  if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1686
0
    fprintf(outfile, "cat <<\\EOF\n");
1687
1688
0
  while (*usagestr) {
1689
0
    const char *str = _(*usagestr++);
1690
0
    struct string_list list = STRING_LIST_INIT_DUP;
1691
0
    unsigned int j;
1692
1693
0
    if (!saw_empty_line && !*str)
1694
0
      saw_empty_line = 1;
1695
1696
0
    string_list_split(&list, str, "\n", -1);
1697
0
    for (j = 0; j < list.nr; j++) {
1698
0
      const char *line = list.items[j].string;
1699
1700
0
      if (saw_empty_line && *line)
1701
0
        fprintf_ln(outfile, _("    %s"), line);
1702
0
      else if (saw_empty_line)
1703
0
        fputc('\n', outfile);
1704
0
      else if (!j)
1705
0
        fprintf_ln(outfile, prefix, line);
1706
0
      else
1707
0
        fprintf_ln(outfile, usage_continued,
1708
0
             (int)usage_len, "", line);
1709
0
    }
1710
0
    string_list_clear(&list, 0);
1711
1712
0
    prefix = or_prefix;
1713
0
  }
1714
1715
0
  need_newline = 1;
1716
0
  for (; opts->type != OPTION_END; opts++)
1717
0
    usage_print_option(opts, all_opts, full,
1718
0
           &need_newline, outfile);
1719
0
  fputc('\n', outfile);
1720
1721
0
  if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1722
0
    fputs("EOF\nexit 0\n", outfile);
1723
1724
0
  return err ? PARSE_OPT_HELP_ERROR : PARSE_OPT_HELP;
1725
0
}
1726
1727
void NORETURN usage_with_options(const char * const *usagestr,
1728
      const struct option *opts)
1729
0
{
1730
0
  usage_with_options_internal(NULL, usagestr, opts,
1731
0
            USAGE_NORMAL, USAGE_TO_STDERR);
1732
0
  exit(129);
1733
0
}
1734
1735
void show_usage_with_options_if_asked(int ac, const char **av,
1736
              const char * const *usagestr,
1737
              const struct option *opts)
1738
0
{
1739
0
  if (ac == 2) {
1740
0
    if (!strcmp(av[1], "-h")) {
1741
0
      usage_with_options_internal(NULL, usagestr, opts,
1742
0
                USAGE_NORMAL, USAGE_TO_STDOUT);
1743
0
      exit(0);
1744
0
    } else if (!strcmp(av[1], "--help-all")) {
1745
0
      usage_with_options_internal(NULL, usagestr, opts,
1746
0
                USAGE_FULL, USAGE_TO_STDOUT);
1747
0
      exit(0);
1748
0
    } else if (!strcmp(av[1], "--git-completion-helper")) {
1749
0
      show_gitcomp(opts, 0);
1750
0
      exit(0);
1751
0
    } else if (!strcmp(av[1], "--git-completion-helper-all")) {
1752
0
      show_gitcomp(opts, 1);
1753
0
      exit(0);
1754
0
    }
1755
0
  }
1756
0
}
1757
1758
void NORETURN usage_msg_opt(const char *msg,
1759
       const char * const *usagestr,
1760
       const struct option *options)
1761
0
{
1762
0
  die_message("%s\n", msg); /* The extra \n is intentional */
1763
0
  usage_with_options(usagestr, options);
1764
0
}
1765
1766
void NORETURN usage_msg_optf(const char * const fmt,
1767
           const char * const *usagestr,
1768
           const struct option *options, ...)
1769
0
{
1770
0
  struct strbuf msg = STRBUF_INIT;
1771
0
  va_list ap;
1772
0
  va_start(ap, options);
1773
0
  strbuf_vaddf(&msg, fmt, ap);
1774
0
  va_end(ap);
1775
1776
0
  usage_msg_opt(msg.buf, usagestr, options);
1777
0
}
1778
1779
void die_for_incompatible_opt4(int opt1, const char *opt1_name,
1780
             int opt2, const char *opt2_name,
1781
             int opt3, const char *opt3_name,
1782
             int opt4, const char *opt4_name)
1783
0
{
1784
0
  int count = 0;
1785
0
  const char *options[4];
1786
1787
0
  if (opt1)
1788
0
    options[count++] = opt1_name;
1789
0
  if (opt2)
1790
0
    options[count++] = opt2_name;
1791
0
  if (opt3)
1792
0
    options[count++] = opt3_name;
1793
0
  if (opt4)
1794
0
    options[count++] = opt4_name;
1795
0
  switch (count) {
1796
0
  case 4:
1797
0
    die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1798
0
        opt1_name, opt2_name, opt3_name, opt4_name);
1799
0
    break;
1800
0
  case 3:
1801
0
    die(_("options '%s', '%s', and '%s' cannot be used together"),
1802
0
        options[0], options[1], options[2]);
1803
0
    break;
1804
0
  case 2:
1805
0
    die(_("options '%s' and '%s' cannot be used together"),
1806
0
        options[0], options[1]);
1807
0
    break;
1808
0
  default:
1809
0
    break;
1810
0
  }
1811
0
}