Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/parse-options.h
Line
Count
Source
1
#ifndef PARSE_OPTIONS_H
2
#define PARSE_OPTIONS_H
3
4
#include "gettext.h"
5
6
struct repository;
7
8
/**
9
 * Refer to Documentation/technical/api-parse-options.adoc for the API doc.
10
 */
11
12
enum parse_opt_type {
13
  /* special types */
14
  OPTION_END,
15
  OPTION_GROUP,
16
  OPTION_NUMBER,
17
  OPTION_ALIAS,
18
  OPTION_SUBCOMMAND,
19
  /* options with no arguments */
20
  OPTION_BIT,
21
  OPTION_NEGBIT,
22
  OPTION_BITOP,
23
  OPTION_COUNTUP,
24
  OPTION_SET_INT,
25
  /* options with arguments (usually) */
26
  OPTION_STRING,
27
  OPTION_INTEGER,
28
  OPTION_UNSIGNED,
29
  OPTION_CALLBACK,
30
  OPTION_LOWLEVEL_CALLBACK,
31
  OPTION_FILENAME
32
};
33
34
enum parse_opt_flags {
35
  PARSE_OPT_KEEP_DASHDASH = 1 << 0,
36
  PARSE_OPT_STOP_AT_NON_OPTION = 1 << 1,
37
  PARSE_OPT_KEEP_ARGV0 = 1 << 2,
38
  PARSE_OPT_KEEP_UNKNOWN_OPT = 1 << 3,
39
  PARSE_OPT_NO_INTERNAL_HELP = 1 << 4,
40
  PARSE_OPT_ONE_SHOT = 1 << 5,
41
  PARSE_OPT_SHELL_EVAL = 1 << 6,
42
  PARSE_OPT_SUBCOMMAND_OPTIONAL = 1 << 7,
43
};
44
45
enum parse_opt_option_flags {
46
  PARSE_OPT_OPTARG  = 1 << 0,
47
  PARSE_OPT_NOARG   = 1 << 1,
48
  PARSE_OPT_NONEG   = 1 << 2,
49
  PARSE_OPT_HIDDEN  = 1 << 3,
50
  PARSE_OPT_LASTARG_DEFAULT = 1 << 4,
51
  PARSE_OPT_NODASH = 1 << 5,
52
  PARSE_OPT_LITERAL_ARGHELP = 1 << 6,
53
  PARSE_OPT_FROM_ALIAS = 1 << 7,
54
  PARSE_OPT_EARLY = 1 << 8, /* only for early_scan_options() */
55
  PARSE_OPT_NOCOMPLETE = 1 << 9,
56
  PARSE_OPT_COMP_ARG = 1 << 10,
57
  PARSE_OPT_CMDMODE = 1 << 11,
58
};
59
60
enum parse_opt_result {
61
  PARSE_OPT_COMPLETE = -4,
62
  PARSE_OPT_HELP_ERROR = -3,
63
  PARSE_OPT_HELP = -2,
64
  PARSE_OPT_ERROR = -1, /* must be the same as error() */
65
  PARSE_OPT_DONE = 0, /* fixed so that "return 0" works */
66
  PARSE_OPT_NON_OPTION,
67
  PARSE_OPT_SUBCOMMAND,
68
  PARSE_OPT_UNKNOWN
69
};
70
71
struct option;
72
typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
73
74
struct parse_opt_ctx_t;
75
typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
76
                const struct option *opt,
77
                const char *arg, int unset);
78
79
typedef int parse_opt_subcommand_fn(int argc, const char **argv,
80
            const char *prefix, struct repository *repo);
81
82
/*
83
 * `type`::
84
 *   holds the type of the option, you must have an OPTION_END last in your
85
 *   array.
86
 *
87
 * `short_name`::
88
 *   the character to use as a short option name, '\0' if none.
89
 *
90
 * `long_name`::
91
 *   the long option (without the leading dashes) or subcommand name,
92
 *   NULL if none.
93
 *
94
 * `value`::
95
 *   stores pointers to the values to be filled.
96
 *
97
 * `precision`::
98
 *   precision of the integer pointed to by `value` in number of bytes. Should
99
 *   typically be its `sizeof()`.
100
 *
101
 * `argh`::
102
 *   token to explain the kind of argument this option wants. Does not
103
 *   begin in capital letter, and does not end with a full stop.
104
 *   Should be wrapped by N_() for translation.
105
 *   Is automatically enclosed in brackets when printed, unless it
106
 *   contains any of the following characters: ()<>[]|
107
 *   E.g. "name" is shown as "<name>" to indicate that a name value
108
 *   needs to be supplied, not the literal string "name", but
109
 *   "<start>,<end>" and "(this|that)" are printed verbatim.
110
 *
111
 * `help`::
112
 *   the short help associated to what the option does.
113
 *   Must never be NULL (except for OPTION_END and OPTION_SUBCOMMAND).
114
 *   OPTION_GROUP uses this pointer to store the group header.
115
 *   Should be wrapped by N_() for translation.
116
 *
117
 * `flags`::
118
 *   mask of parse_opt_option_flags.
119
 *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
120
 *   PARSE_OPT_NOARG: says that this option does not take an argument
121
 *   PARSE_OPT_NONEG: says that this option cannot be negated
122
 *                   (i.e. rejects "--no-<option>")
123
 *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
124
 *                     shown only in the full usage.
125
 *   PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
126
 *        value if no argument is given when the option
127
 *        is last on the command line. If the option is
128
 *        not last it will require an argument.
129
 *        Should not be used with PARSE_OPT_OPTARG.
130
 *   PARSE_OPT_NODASH: this option doesn't start with a dash; can only be a
131
 *           short option and can't accept arguments.
132
 *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
133
 *        (i.e. '<argh>') in the help message.
134
 *        Useful for options with multiple parameters.
135
 *   PARSE_OPT_NOCOMPLETE: by default all visible options are completable
136
 *         by git-completion.bash. This option suppresses that.
137
 *   PARSE_OPT_COMP_ARG: this option forces to git-completion.bash to
138
 *       complete an option as --name= not --name even if
139
 *       the option takes optional argument.
140
 *
141
 * `callback`::
142
 *   pointer to the callback to use for OPTION_CALLBACK
143
 *
144
 * `defval`::
145
 *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
146
 *   OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
147
 *   CALLBACKS can use it like they want.
148
 *
149
 * `ll_callback`::
150
 *   pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
151
 *
152
 * `subcommand_fn`::
153
 *   pointer to a function to use for OPTION_SUBCOMMAND.
154
 *   It will be put in value when the subcommand is given on the command line.
155
 */
156
struct option {
157
  enum parse_opt_type type;
158
  int short_name;
159
  const char *long_name;
160
  void *value;
161
  size_t precision;
162
  const char *argh;
163
  const char *help;
164
165
  enum parse_opt_option_flags flags;
166
  parse_opt_cb *callback;
167
  intptr_t defval;
168
  parse_opt_ll_cb *ll_callback;
169
  intptr_t extra;
170
  parse_opt_subcommand_fn *subcommand_fn;
171
};
172
173
0
#define OPT_BIT_F(s, l, v, h, b, f) { \
174
0
  .type = OPTION_BIT, \
175
0
  .short_name = (s), \
176
0
  .long_name = (l), \
177
0
  .value = (v), \
178
0
  .precision = sizeof(*v), \
179
0
  .help = (h), \
180
0
  .flags = PARSE_OPT_NOARG|(f), \
181
0
  .callback = NULL, \
182
0
  .defval = (b), \
183
0
}
184
#define OPT_COUNTUP_F(s, l, v, h, f) { \
185
  .type = OPTION_COUNTUP, \
186
  .short_name = (s), \
187
  .long_name = (l), \
188
  .value = (v), \
189
  .precision = sizeof(*v), \
190
  .help = (h), \
191
  .flags = PARSE_OPT_NOARG|(f), \
192
}
193
0
#define OPT_SET_INT_F(s, l, v, h, i, f) { \
194
0
  .type = OPTION_SET_INT, \
195
0
  .short_name = (s), \
196
0
  .long_name = (l), \
197
0
  .value = (v), \
198
0
  .precision = sizeof(*v), \
199
0
  .help = (h), \
200
0
  .flags = PARSE_OPT_NOARG | (f), \
201
0
  .defval = (i), \
202
0
}
203
0
#define OPT_BOOL_F(s, l, v, h, f)   OPT_SET_INT_F(s, l, v, h, 1, f)
204
0
#define OPT_CALLBACK_F(s, l, v, a, h, f, cb) { \
205
0
  .type = OPTION_CALLBACK, \
206
0
  .short_name = (s), \
207
0
  .long_name = (l), \
208
0
  .value = (v), \
209
0
  .argh = (a), \
210
0
  .help = (h), \
211
0
  .flags = (f), \
212
0
  .callback = (cb), \
213
0
}
214
0
#define OPT_STRING_F(s, l, v, a, h, f) { \
215
0
  .type = OPTION_STRING, \
216
0
  .short_name = (s), \
217
0
  .long_name = (l), \
218
0
  .value = (v), \
219
0
  .argh = (a), \
220
0
  .help = (h), \
221
0
  .flags = (f), \
222
0
}
223
0
#define OPT_INTEGER_F(s, l, v, h, f) { \
224
0
  .type = OPTION_INTEGER, \
225
0
  .short_name = (s), \
226
0
  .long_name = (l), \
227
0
  .value = (v) + BARF_UNLESS_SIGNED(*(v)), \
228
0
  .precision = sizeof(*v), \
229
0
  .argh = N_("n"), \
230
0
  .help = (h), \
231
0
  .flags = (f), \
232
0
}
233
234
0
#define OPT_END() { \
235
0
  .type = OPTION_END, \
236
0
}
237
0
#define OPT_GROUP(h) { \
238
0
  .type = OPTION_GROUP, \
239
0
  .help = (h), \
240
0
}
241
#define OPT_HIDDEN_GROUP(h) { \
242
  .type = OPTION_GROUP, \
243
  .help = (h), \
244
  .flags = PARSE_OPT_HIDDEN, \
245
}
246
0
#define OPT_BIT(s, l, v, h, b)      OPT_BIT_F(s, l, v, h, b, 0)
247
0
#define OPT_BITOP(s, l, v, h, set, clear) { \
248
0
  .type = OPTION_BITOP, \
249
0
  .short_name = (s), \
250
0
  .long_name = (l), \
251
0
  .value = (v), \
252
0
  .precision = sizeof(*v), \
253
0
  .help = (h), \
254
0
  .flags = PARSE_OPT_NOARG|PARSE_OPT_NONEG, \
255
0
  .defval = (set), \
256
0
  .extra = (clear), \
257
0
}
258
#define OPT_NEGBIT(s, l, v, h, b) { \
259
  .type = OPTION_NEGBIT, \
260
  .short_name = (s), \
261
  .long_name = (l), \
262
  .value = (v), \
263
  .precision = sizeof(*v), \
264
  .help = (h), \
265
  .flags = PARSE_OPT_NOARG, \
266
  .defval = (b), \
267
}
268
#define OPT_COUNTUP(s, l, v, h)     OPT_COUNTUP_F(s, l, v, h, 0)
269
0
#define OPT_SET_INT(s, l, v, h, i)  OPT_SET_INT_F(s, l, v, h, i, 0)
270
0
#define OPT_BOOL(s, l, v, h)        OPT_BOOL_F(s, l, v, h, 0)
271
#define OPT_HIDDEN_BOOL(s, l, v, h) { \
272
  .type = OPTION_SET_INT, \
273
  .short_name = (s), \
274
  .long_name = (l), \
275
  .value = (v), \
276
  .precision = sizeof(*v), \
277
  .help = (h), \
278
  .flags = PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, \
279
  .defval = 1, \
280
}
281
#define OPT_CMDMODE_F(s, l, v, h, i, f) { \
282
  .type = OPTION_SET_INT, \
283
  .short_name = (s), \
284
  .long_name = (l), \
285
  .value = (v), \
286
  .precision = sizeof(*v), \
287
  .help = (h), \
288
  .flags = PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), \
289
  .defval = (i), \
290
}
291
#define OPT_CMDMODE(s, l, v, h, i)  OPT_CMDMODE_F(s, l, v, h, i, 0)
292
293
0
#define OPT_INTEGER(s, l, v, h)     OPT_INTEGER_F(s, l, v, h, 0)
294
0
#define OPT_UNSIGNED(s, l, v, h) { \
295
0
  .type = OPTION_UNSIGNED, \
296
0
  .short_name = (s), \
297
0
  .long_name = (l), \
298
0
  .value = (v) + BARF_UNLESS_UNSIGNED(*(v)), \
299
0
  .precision = sizeof(*v), \
300
0
  .argh = N_("n"), \
301
0
  .help = (h), \
302
0
  .flags = PARSE_OPT_NONEG, \
303
0
}
304
#define OPT_STRING(s, l, v, a, h)   OPT_STRING_F(s, l, v, a, h, 0)
305
#define OPT_STRING_LIST(s, l, v, a, h) { \
306
  .type = OPTION_CALLBACK, \
307
  .short_name = (s), \
308
  .long_name = (l), \
309
  .value = (v), \
310
  .argh = (a), \
311
  .help = (h), \
312
  .callback = &parse_opt_string_list, \
313
}
314
#define OPT_STRVEC(s, l, v, a, h) { \
315
  .type = OPTION_CALLBACK, \
316
  .short_name = (s), \
317
  .long_name = (l), \
318
  .value = (v), \
319
  .argh = (a), \
320
  .help = (h), \
321
  .callback = &parse_opt_strvec, \
322
}
323
#define OPT_UYN(s, l, v, h) { \
324
  .type = OPTION_CALLBACK, \
325
  .short_name = (s), \
326
  .long_name = (l), \
327
  .value = (v), \
328
  .help = (h), \
329
  .flags = PARSE_OPT_NOARG, \
330
  .callback = &parse_opt_tertiary, \
331
}
332
#define OPT_EXPIRY_DATE(s, l, v, h) { \
333
  .type = OPTION_CALLBACK, \
334
  .short_name = (s), \
335
  .long_name = (l), \
336
  .value = (v), \
337
  .argh = N_("expiry-date"), \
338
  .help = (h), \
339
  .callback = parse_opt_expiry_date_cb, \
340
}
341
0
#define OPT_CALLBACK(s, l, v, a, h, cb) OPT_CALLBACK_F(s, l, v, a, h, 0, cb)
342
#define OPT_NUMBER_CALLBACK(v, h, cb) { \
343
  .type = OPTION_NUMBER, \
344
  .value = (v), \
345
  .help = (h), \
346
  .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \
347
  .callback = (cb), \
348
}
349
0
#define OPT_FILENAME(s, l, v, h) { \
350
0
  .type = OPTION_FILENAME, \
351
0
  .short_name = (s), \
352
0
  .long_name = (l), \
353
0
  .value = (v), \
354
0
  .argh = N_("file"), \
355
0
  .help = (h), \
356
0
}
357
0
#define OPT_COLOR_FLAG(s, l, v, h) { \
358
0
  .type = OPTION_CALLBACK, \
359
0
  .short_name = (s), \
360
0
  .long_name = (l), \
361
0
  .value = (v), \
362
0
  .argh = N_("when"), \
363
0
  .help = (h), \
364
0
  .flags = PARSE_OPT_OPTARG, \
365
0
  .callback = parse_opt_color_flag_cb, \
366
0
  .defval = (intptr_t)"always", \
367
0
}
368
369
0
#define OPT_NOOP_NOARG(s, l) { \
370
0
  .type = OPTION_CALLBACK, \
371
0
  .short_name = (s), \
372
0
  .long_name = (l), \
373
0
  .help = N_("no-op (backward compatibility)"), \
374
0
  .flags = PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, \
375
0
  .callback = parse_opt_noop_cb, \
376
0
}
377
378
static char *parse_options_noop_ignored_value MAYBE_UNUSED;
379
#define OPT_NOOP_ARG(s, l) { \
380
  .type = OPTION_CALLBACK, \
381
  .short_name = (s), \
382
  .long_name = (l), \
383
  .value = &parse_options_noop_ignored_value, \
384
  .argh = "ignored", \
385
  .help = N_("no-op (backward compatibility)"), \
386
  .flags = PARSE_OPT_HIDDEN, \
387
  .callback = parse_opt_noop_cb, \
388
}
389
390
#define OPT_ALIAS_F(s, l, source_long_name, f) { \
391
  .type = OPTION_ALIAS, \
392
  .short_name = (s), \
393
  .long_name = (l), \
394
  .value = (char *)(source_long_name), \
395
  .flags = (f), \
396
}
397
398
#define OPT_ALIAS(s, l, source_long_name) OPT_ALIAS_F(s, l, source_long_name, 0)
399
400
#define OPT_SUBCOMMAND_F(l, v, fn, h, f) { \
401
  .type = OPTION_SUBCOMMAND, \
402
  .long_name = (l), \
403
  .value = (v), \
404
  .help = (h), \
405
  .flags = (f), \
406
  .subcommand_fn = (fn), \
407
}
408
#define OPT_SUBCOMMAND(l, v, fn)    OPT_SUBCOMMAND_F((l), (v), (fn), NULL, 0)
409
410
/*
411
 * parse_options() will filter out the processed options and leave the
412
 * non-option arguments in argv[]. argv0 is assumed program name and
413
 * skipped.
414
 *
415
 * usagestr strings should be marked for translation with N_().
416
 *
417
 * Returns the number of arguments left in argv[].
418
 *
419
 * In one-shot mode, argv0 is not a program name, argv[] is left
420
 * untouched and parse_options() returns the number of options
421
 * processed.
422
 */
423
int parse_options(int argc, const char **argv, const char *prefix,
424
      const struct option *options,
425
      const char * const usagestr[],
426
      enum parse_opt_flags flags);
427
428
/*
429
 * Return non-zero if `opt` takes a value, which means that it consumes
430
 * the next argument when that value is not stuck to it with an '='.
431
 *
432
 * Note that an option with PARSE_OPT_LASTARG_DEFAULT is considered as
433
 * taking a value here, even though it only consumes the next argument
434
 * when it isn't the last one. Callers that care have to check for that
435
 * flag themselves.
436
 */
437
int parse_options_takes_argument(const struct option *opt);
438
439
NORETURN void usage_with_options(const char * const *usagestr,
440
         const struct option *options);
441
442
/*
443
 * Programs that call parse_options() early do not need this function.
444
 * If your program has to answer help requests (-h and --help-all) or
445
 * the completion helper before parse_options() runs, call this
446
 * function to answer them.
447
 */
448
void show_usage_with_options_if_asked(int ac, const char **av,
449
              const char * const *usage,
450
              const struct option *options);
451
452
NORETURN void usage_msg_opt(const char *msg,
453
          const char * const *usagestr,
454
          const struct option *options);
455
456
/**
457
 * usage_msg_optf() is like usage_msg_opt() except that the first
458
 * argument is a format string, and optional format arguments follow
459
 * after the 3rd option.
460
 */
461
__attribute__((format (printf,1,4)))
462
void NORETURN usage_msg_optf(const char *fmt,
463
           const char * const *usagestr,
464
           const struct option *options, ...);
465
466
void die_for_incompatible_opt4(int opt1, const char *opt1_name,
467
             int opt2, const char *opt2_name,
468
             int opt3, const char *opt3_name,
469
             int opt4, const char *opt4_name);
470
471
472
static inline void die_for_incompatible_opt3(int opt1, const char *opt1_name,
473
               int opt2, const char *opt2_name,
474
               int opt3, const char *opt3_name)
475
0
{
476
0
  die_for_incompatible_opt4(opt1, opt1_name,
477
0
          opt2, opt2_name,
478
0
          opt3, opt3_name,
479
0
          0, "");
480
0
}
Unexecuted instantiation: remote.c:die_for_incompatible_opt3
Unexecuted instantiation: revision.c:die_for_incompatible_opt3
Unexecuted instantiation: submodule-config.c:die_for_incompatible_opt3
Unexecuted instantiation: submodule.c:die_for_incompatible_opt3
Unexecuted instantiation: column.c:die_for_incompatible_opt3
Unexecuted instantiation: diff.c:die_for_incompatible_opt3
Unexecuted instantiation: list-objects-filter-options.c:die_for_incompatible_opt3
Unexecuted instantiation: parse-options-cb.c:die_for_incompatible_opt3
Unexecuted instantiation: parse-options.c:die_for_incompatible_opt3
Unexecuted instantiation: send-pack.c:die_for_incompatible_opt3
Unexecuted instantiation: apply.c:die_for_incompatible_opt3
481
482
static inline void die_for_incompatible_opt2(int opt1, const char *opt1_name,
483
               int opt2, const char *opt2_name)
484
0
{
485
0
  die_for_incompatible_opt4(opt1, opt1_name,
486
0
          opt2, opt2_name,
487
0
          0, "",
488
0
          0, "");
489
0
}
Unexecuted instantiation: remote.c:die_for_incompatible_opt2
Unexecuted instantiation: revision.c:die_for_incompatible_opt2
Unexecuted instantiation: submodule-config.c:die_for_incompatible_opt2
Unexecuted instantiation: submodule.c:die_for_incompatible_opt2
Unexecuted instantiation: column.c:die_for_incompatible_opt2
Unexecuted instantiation: diff.c:die_for_incompatible_opt2
Unexecuted instantiation: list-objects-filter-options.c:die_for_incompatible_opt2
Unexecuted instantiation: parse-options-cb.c:die_for_incompatible_opt2
Unexecuted instantiation: parse-options.c:die_for_incompatible_opt2
Unexecuted instantiation: send-pack.c:die_for_incompatible_opt2
Unexecuted instantiation: apply.c:die_for_incompatible_opt2
490
491
/*
492
 * Use these assertions for callbacks that expect to be called with NONEG and
493
 * NOARG respectively, and do not otherwise handle the "unset" and "arg"
494
 * parameters.
495
 */
496
0
#define BUG_ON_OPT_NEG(unset) do { \
497
0
  if ((unset)) \
498
0
    BUG("option callback does not expect negation"); \
499
0
} while (0)
500
0
#define BUG_ON_OPT_ARG(arg) do { \
501
0
  if ((arg)) \
502
0
    BUG("option callback does not expect an argument"); \
503
0
} while (0)
504
505
/*
506
 * Similar to the assertions above, but checks that "arg" is always non-NULL.
507
 * This assertion also implies BUG_ON_OPT_NEG(), letting you declare both
508
 * assertions in a single line.
509
 */
510
#define BUG_ON_OPT_NEG_NOARG(unset, arg) do { \
511
  BUG_ON_OPT_NEG(unset); \
512
  if(!(arg)) \
513
    BUG("option callback expects an argument"); \
514
} while(0)
515
516
/*----- Early scan: scanning argv before the actual option parsing -----*/
517
518
/*
519
 * Some commands need to look at a few options before they can parse
520
 * their command line for real, for example because the result decides
521
 * whether a repository is needed at all.
522
 *
523
 * Such an early scan has to know which options take their value as a
524
 * separate argument, or it could mistake such a value for an
525
 * option. The functions below allow performing such early scans
526
 * without being fooled by option values.
527
 */
528
529
/*
530
 * Called by early_scan_options() for each argument matching a
531
 * `struct option` with PARSE_OPT_EARLY set.
532
 *
533
 * - `option` is the matching option, (may point into a temporary
534
 *   copy of the options, so must not be used after the scan,)
535
 * - `value` its value, or NULL if it doesn't take one or if its value
536
 *   is optional and wasn't given, as with PARSE_OPT_OPTARG, or with
537
 *   PARSE_OPT_LASTARG_DEFAULT when the option is the last argument,
538
 * - `unset` is 'true' for the negated form of the option (like
539
 *   for parse_opt_cb()), in which case `value` is always NULL, and
540
 * - `pos` the index of the option in argv.
541
 *
542
 * Returning a non-zero value stops the scan right after the option and
543
 * its value, if any.
544
 */
545
typedef int early_scan_fn(const struct option *option, const char *value,
546
        bool unset, int pos, void *data);
547
548
enum early_scan_flags {
549
  EARLY_SCAN_STOP_AT_NON_OPTION = 1 << 0, /* Stop at any non option */
550
};
551
552
/*
553
 * Scan `argv` for the options described by `option`, calling `fn` for
554
 * each of those that have PARSE_OPT_EARLY set. `argv` is not
555
 * modified.
556
 *
557
 * `fn` may be NULL when no option has PARSE_OPT_EARLY set, which is
558
 * useful to only find out where the scan stops.
559
 *
560
 * The scan always stops at "--" and at "--end-of-options", as
561
 * parse_options() always stops parsing options there too, whatever its
562
 * flags. PARSE_OPT_KEEP_DASHDASH and PARSE_OPT_KEEP_UNKNOWN_OPT only
563
 * decide if the terminator is left in argv, not if it terminates.
564
 *
565
 * Returns the index of the first argument not consumed by the scan,
566
 * which is `argc` when the whole array was scanned.
567
 *
568
 * Long options are looked up the same way as parse_options() does,
569
 * so their negated forms, their unambiguous abbreviations, unless
570
 * GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS is set, and the OPTION_ALIAS
571
 * entries standing for them are all recognized, and their value is
572
 * skipped when they take one.
573
 *
574
 * To never misinterpret an option value as an option though, the scan
575
 * stops at any argument it cannot reliably skip, that is:
576
 *
577
 *  - a short option, or a bundle of them,
578
 *
579
 *  - an unknown option, or an ambiguous or disallowed abbreviation,
580
 *
581
 *  - an option with a low-level callback, as such a callback may
582
 *    consume any number of arguments,
583
 *
584
 *  - an option with a value stuck to it that parse_options() would
585
 *    reject, like a negated option or a PARSE_OPT_NOARG one,
586
 *
587
 *  - an option requiring a value when it is the last argument, which
588
 *    parse_options() would also reject.
589
 *
590
 * In all these cases, the scan can fail to see an option that
591
 * parse_options() would accept, but it doesn't report an option that
592
 * parse_options() would not, so that callers err on the safe side.
593
 *
594
 * This requires the scan to be told about the way the later
595
 * parse_options() call treats its arguments though:
596
 *
597
 *  - when parse_options() is called with PARSE_OPT_STOP_AT_NON_OPTION
598
 *    or with subcommands, callers should pass
599
 *    EARLY_SCAN_STOP_AT_NON_OPTION,
600
 *
601
 *  - when parse_options() is called with PARSE_OPT_KEEP_UNKNOWN_OPT,
602
 *    which disables abbreviations there but not here, callers should
603
 *    not use the scan.
604
 *
605
 * Passing EARLY_SCAN_STOP_AT_NON_OPTION in other cases is safe, and
606
 * useful when the caller rejects non-option arguments itself.
607
 */
608
int early_scan_options(int argc, const char **argv,
609
           const struct option *option,
610
           enum early_scan_flags flags,
611
           early_scan_fn *fn, void *data);
612
613
/*----- incremental advanced APIs -----*/
614
615
struct parse_opt_cmdmode_list;
616
617
/*
618
 * It's okay for the caller to consume argv/argc in the usual way.
619
 * Other fields of that structure are private to parse-options and should not
620
 * be modified in any way.
621
 */
622
struct parse_opt_ctx_t {
623
  const char **argv;
624
  const char **out;
625
  int argc, cpidx, total;
626
  const char *opt;
627
  enum parse_opt_flags flags;
628
  unsigned has_subcommands;
629
  const char *prefix;
630
  const char **alias_groups; /* must be in groups of 3 elements! */
631
  struct parse_opt_cmdmode_list *cmdmode_list;
632
};
633
634
void parse_options_start(struct parse_opt_ctx_t *ctx,
635
       int argc, const char **argv, const char *prefix,
636
       const struct option *options,
637
       enum parse_opt_flags flags);
638
639
enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
640
           const struct option *options,
641
           const char * const usagestr[]);
642
643
int parse_options_end(struct parse_opt_ctx_t *ctx);
644
645
struct option *parse_options_dup(const struct option *a);
646
struct option *parse_options_concat(const struct option *a, const struct option *b);
647
648
/*----- some often used options -----*/
649
int parse_opt_abbrev_cb(const struct option *, const char *, int);
650
int parse_opt_expiry_date_cb(const struct option *, const char *, int);
651
int parse_opt_color_flag_cb(const struct option *, const char *, int);
652
int parse_opt_verbosity_cb(const struct option *, const char *, int);
653
/* value is struct oid_array* */
654
int parse_opt_object_name(const struct option *, const char *, int);
655
/* value is struct object_id* */
656
int parse_opt_object_id(const struct option *, const char *, int);
657
int parse_opt_commits(const struct option *, const char *, int);
658
int parse_opt_commit(const struct option *, const char *, int);
659
int parse_opt_tertiary(const struct option *, const char *, int);
660
int parse_opt_string_list(const struct option *, const char *, int);
661
int parse_opt_strvec(const struct option *, const char *, int);
662
int parse_opt_noop_cb(const struct option *, const char *, int);
663
int parse_opt_passthru(const struct option *, const char *, int);
664
int parse_opt_passthru_argv(const struct option *, const char *, int);
665
/* value is enum branch_track* */
666
int parse_opt_tracking_mode(const struct option *, const char *, int);
667
668
#define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
669
#define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))
670
0
#define OPT__VERBOSITY(var) { \
671
0
  .type = OPTION_CALLBACK, \
672
0
  .short_name = 'v', \
673
0
  .long_name = "verbose", \
674
0
  .value = (var), \
675
0
  .help = N_("be more verbose"), \
676
0
  .flags = PARSE_OPT_NOARG, \
677
0
  .callback = &parse_opt_verbosity_cb, \
678
0
}, { \
679
0
  .type = OPTION_CALLBACK, \
680
0
  .short_name = 'q', \
681
0
  .long_name = "quiet", \
682
0
  .value = (var), \
683
0
  .help = N_("be more quiet"), \
684
0
  .flags = PARSE_OPT_NOARG, \
685
0
  .callback = &parse_opt_verbosity_cb, \
686
0
}
687
#define OPT__DRY_RUN(var, h)  OPT_BOOL('n', "dry-run", (var), (h))
688
#define OPT__FORCE(var, h, f) OPT_COUNTUP_F('f', "force",   (var), (h), (f))
689
0
#define OPT__ABBREV(var) { \
690
0
  .type = OPTION_CALLBACK, \
691
0
  .long_name = "abbrev", \
692
0
  .value = (var), \
693
0
  .argh = N_("n"), \
694
0
  .help = N_("use <n> digits to display object names"), \
695
0
  .flags = PARSE_OPT_OPTARG, \
696
0
  .callback = &parse_opt_abbrev_cb, \
697
0
}
698
#define OPT__SUPER_PREFIX(var) \
699
  OPT_STRING_F(0, "super-prefix", (var), N_("prefix"), \
700
    N_("prefixed path to initial superproject"), PARSE_OPT_HIDDEN)
701
702
#define OPT__COLOR(var, h) \
703
  OPT_COLOR_FLAG(0, "color", (var), (h))
704
#define OPT_COLUMN(s, l, v, h) { \
705
  .type = OPTION_CALLBACK, \
706
  .short_name = (s), \
707
  .long_name = (l), \
708
  .value = (v), \
709
  .argh = N_("style"), \
710
  .help = (h), \
711
  .flags = PARSE_OPT_OPTARG, \
712
  .callback = parseopt_column_callback, \
713
}
714
#define OPT_PASSTHRU(s, l, v, a, h, f) { \
715
  .type = OPTION_CALLBACK, \
716
  .short_name = (s), \
717
  .long_name = (l), \
718
  .value = (v), \
719
  .argh = (a), \
720
  .help = (h), \
721
  .flags = (f), \
722
  .callback = parse_opt_passthru, \
723
}
724
#define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) { \
725
  .type = OPTION_CALLBACK, \
726
  .short_name = (s), \
727
  .long_name = (l), \
728
  .value = (v), \
729
  .argh = (a), \
730
  .help = (h), \
731
  .flags = (f), \
732
  .callback = parse_opt_passthru_argv, \
733
}
734
#define _OPT_CONTAINS_OR_WITH(l, v, h, f) { \
735
  .type = OPTION_CALLBACK, \
736
  .long_name = (l), \
737
  .value = (v), \
738
  .argh = N_("commit"), \
739
  .help = (h), \
740
  .flags = PARSE_OPT_LASTARG_DEFAULT | (f), \
741
  .callback = parse_opt_commits, \
742
  .defval = (intptr_t) "HEAD", \
743
}
744
#define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG)
745
#define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG)
746
#define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
747
#define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
748
#define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
749
#define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file"))
750
#define OPT_PATHSPEC_FILE_NUL(v)  OPT_BOOL(0, "pathspec-file-nul", v, N_("with --pathspec-from-file, pathspec elements are separated with NUL character"))
751
#define OPT_AUTOSTASH(v) OPT_BOOL(0, "autostash", v, N_("automatically stash/stash pop before and after"))
752
#define OPT_DIFF_UNIFIED(v) OPT_INTEGER_F('U', "unified", v, N_("generate diffs with <n> lines context"), PARSE_OPT_NONEG)
753
#define OPT_DIFF_INTERHUNK_CONTEXT(v) OPT_INTEGER_F(0, "inter-hunk-context", v, N_("show context between diff hunks up to the specified number of lines"), PARSE_OPT_NONEG)
754
755
#define OPT_IPVERSION(v) \
756
  OPT_SET_INT_F('4', "ipv4", (v), N_("use IPv4 addresses only"), \
757
    TRANSPORT_FAMILY_IPV4, PARSE_OPT_NONEG), \
758
  OPT_SET_INT_F('6', "ipv6", (v), N_("use IPv6 addresses only"), \
759
    TRANSPORT_FAMILY_IPV6, PARSE_OPT_NONEG)
760
761
#endif