| 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_NOCOMPLETE = 1 << 9, |
| 55 | PARSE_OPT_COMP_ARG = 1 << 10, |
| 56 | PARSE_OPT_CMDMODE = 1 << 11, |
| 57 | }; |
| 58 | |
| 59 | enum parse_opt_result { |
| 60 | PARSE_OPT_COMPLETE = -4, |
| 61 | PARSE_OPT_HELP_ERROR = -3, |
| 62 | PARSE_OPT_HELP = -2, |
| 63 | PARSE_OPT_ERROR = -1, /* must be the same as error() */ |
| 64 | PARSE_OPT_DONE = 0, /* fixed so that "return 0" works */ |
| 65 | PARSE_OPT_NON_OPTION, |
| 66 | PARSE_OPT_SUBCOMMAND, |
| 67 | PARSE_OPT_UNKNOWN |
| 68 | }; |
| 69 | |
| 70 | struct option; |
| 71 | typedef int parse_opt_cb(const struct option *, const char *arg, int unset); |
| 72 | |
| 73 | struct parse_opt_ctx_t; |
| 74 | typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx, |
| 75 | const struct option *opt, |
| 76 | const char *arg, int unset); |
| 77 | |
| 78 | typedef int parse_opt_subcommand_fn(int argc, const char **argv, |
| 79 | const char *prefix, struct repository *repo); |
| 80 | |
| 81 | /* |
| 82 | * `type`:: |
| 83 | * holds the type of the option, you must have an OPTION_END last in your |
| 84 | * array. |
| 85 | * |
| 86 | * `short_name`:: |
| 87 | * the character to use as a short option name, '\0' if none. |
| 88 | * |
| 89 | * `long_name`:: |
| 90 | * the long option (without the leading dashes) or subcommand name, |
| 91 | * NULL if none. |
| 92 | * |
| 93 | * `value`:: |
| 94 | * stores pointers to the values to be filled. |
| 95 | * |
| 96 | * `precision`:: |
| 97 | * precision of the integer pointed to by `value` in number of bytes. Should |
| 98 | * typically be its `sizeof()`. |
| 99 | * |
| 100 | * `argh`:: |
| 101 | * token to explain the kind of argument this option wants. Does not |
| 102 | * begin in capital letter, and does not end with a full stop. |
| 103 | * Should be wrapped by N_() for translation. |
| 104 | * Is automatically enclosed in brackets when printed, unless it |
| 105 | * contains any of the following characters: ()<>[]| |
| 106 | * E.g. "name" is shown as "<name>" to indicate that a name value |
| 107 | * needs to be supplied, not the literal string "name", but |
| 108 | * "<start>,<end>" and "(this|that)" are printed verbatim. |
| 109 | * |
| 110 | * `help`:: |
| 111 | * the short help associated to what the option does. |
| 112 | * Must never be NULL (except for OPTION_END and OPTION_SUBCOMMAND). |
| 113 | * OPTION_GROUP uses this pointer to store the group header. |
| 114 | * Should be wrapped by N_() for translation. |
| 115 | * |
| 116 | * `flags`:: |
| 117 | * mask of parse_opt_option_flags. |
| 118 | * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs) |
| 119 | * PARSE_OPT_NOARG: says that this option does not take an argument |
| 120 | * PARSE_OPT_NONEG: says that this option cannot be negated |
| 121 | * (i.e. rejects "--no-<option>") |
| 122 | * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and |
| 123 | * shown only in the full usage. |
| 124 | * PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default |
| 125 | * value if no argument is given when the option |
| 126 | * is last on the command line. If the option is |
| 127 | * not last it will require an argument. |
| 128 | * Should not be used with PARSE_OPT_OPTARG. |
| 129 | * PARSE_OPT_NODASH: this option doesn't start with a dash; can only be a |
| 130 | * short option and can't accept arguments. |
| 131 | * PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets |
| 132 | * (i.e. '<argh>') in the help message. |
| 133 | * Useful for options with multiple parameters. |
| 134 | * PARSE_OPT_NOCOMPLETE: by default all visible options are completable |
| 135 | * by git-completion.bash. This option suppresses that. |
| 136 | * PARSE_OPT_COMP_ARG: this option forces to git-completion.bash to |
| 137 | * complete an option as --name= not --name even if |
| 138 | * the option takes optional argument. |
| 139 | * |
| 140 | * `callback`:: |
| 141 | * pointer to the callback to use for OPTION_CALLBACK |
| 142 | * |
| 143 | * `defval`:: |
| 144 | * default value to fill (*->value) with for PARSE_OPT_OPTARG. |
| 145 | * OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met. |
| 146 | * CALLBACKS can use it like they want. |
| 147 | * |
| 148 | * `ll_callback`:: |
| 149 | * pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK |
| 150 | * |
| 151 | * `subcommand_fn`:: |
| 152 | * pointer to a function to use for OPTION_SUBCOMMAND. |
| 153 | * It will be put in value when the subcommand is given on the command line. |
| 154 | */ |
| 155 | struct option { |
| 156 | enum parse_opt_type type; |
| 157 | int short_name; |
| 158 | const char *long_name; |
| 159 | void *value; |
| 160 | size_t precision; |
| 161 | const char *argh; |
| 162 | const char *help; |
| 163 | |
| 164 | enum parse_opt_option_flags flags; |
| 165 | parse_opt_cb *callback; |
| 166 | intptr_t defval; |
| 167 | parse_opt_ll_cb *ll_callback; |
| 168 | intptr_t extra; |
| 169 | parse_opt_subcommand_fn *subcommand_fn; |
| 170 | }; |
| 171 | |
| 172 | #define OPT_BIT_F(s, l, v, h, b, f) { \ |
| 173 | .type = OPTION_BIT, \ |
| 174 | .short_name = (s), \ |
| 175 | .long_name = (l), \ |
| 176 | .value = (v), \ |
| 177 | .precision = sizeof(*v), \ |
| 178 | .help = (h), \ |
| 179 | .flags = PARSE_OPT_NOARG|(f), \ |
| 180 | .callback = NULL, \ |
| 181 | .defval = (b), \ |
| 182 | } |
| 183 | #define OPT_COUNTUP_F(s, l, v, h, f) { \ |
| 184 | .type = OPTION_COUNTUP, \ |
| 185 | .short_name = (s), \ |
| 186 | .long_name = (l), \ |
| 187 | .value = (v), \ |
| 188 | .precision = sizeof(*v), \ |
| 189 | .help = (h), \ |
| 190 | .flags = PARSE_OPT_NOARG|(f), \ |
| 191 | } |
| 192 | #define OPT_SET_INT_F(s, l, v, h, i, f) { \ |
| 193 | .type = OPTION_SET_INT, \ |
| 194 | .short_name = (s), \ |
| 195 | .long_name = (l), \ |
| 196 | .value = (v), \ |
| 197 | .precision = sizeof(*v), \ |
| 198 | .help = (h), \ |
| 199 | .flags = PARSE_OPT_NOARG | (f), \ |
| 200 | .defval = (i), \ |
| 201 | } |
| 202 | #define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f) |
| 203 | #define OPT_CALLBACK_F(s, l, v, a, h, f, cb) { \ |
| 204 | .type = OPTION_CALLBACK, \ |
| 205 | .short_name = (s), \ |
| 206 | .long_name = (l), \ |
| 207 | .value = (v), \ |
| 208 | .argh = (a), \ |
| 209 | .help = (h), \ |
| 210 | .flags = (f), \ |
| 211 | .callback = (cb), \ |
| 212 | } |
| 213 | #define OPT_STRING_F(s, l, v, a, h, f) { \ |
| 214 | .type = OPTION_STRING, \ |
| 215 | .short_name = (s), \ |
| 216 | .long_name = (l), \ |
| 217 | .value = (v), \ |
| 218 | .argh = (a), \ |
| 219 | .help = (h), \ |
| 220 | .flags = (f), \ |
| 221 | } |
| 222 | #define OPT_INTEGER_F(s, l, v, h, f) { \ |
| 223 | .type = OPTION_INTEGER, \ |
| 224 | .short_name = (s), \ |
| 225 | .long_name = (l), \ |
| 226 | .value = (v) + BARF_UNLESS_SIGNED(*(v)), \ |
| 227 | .precision = sizeof(*v), \ |
| 228 | .argh = N_("n"), \ |
| 229 | .help = (h), \ |
| 230 | .flags = (f), \ |
| 231 | } |
| 232 | |
| 233 | #define OPT_END() { \ |
| 234 | .type = OPTION_END, \ |
| 235 | } |
| 236 | #define OPT_GROUP(h) { \ |
| 237 | .type = OPTION_GROUP, \ |
| 238 | .help = (h), \ |
| 239 | } |
| 240 | #define OPT_HIDDEN_GROUP(h) { \ |
| 241 | .type = OPTION_GROUP, \ |
| 242 | .help = (h), \ |
| 243 | .flags = PARSE_OPT_HIDDEN, \ |
| 244 | } |
| 245 | #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0) |
| 246 | #define OPT_BITOP(s, l, v, h, set, clear) { \ |
| 247 | .type = OPTION_BITOP, \ |
| 248 | .short_name = (s), \ |
| 249 | .long_name = (l), \ |
| 250 | .value = (v), \ |
| 251 | .precision = sizeof(*v), \ |
| 252 | .help = (h), \ |
| 253 | .flags = PARSE_OPT_NOARG|PARSE_OPT_NONEG, \ |
| 254 | .defval = (set), \ |
| 255 | .extra = (clear), \ |
| 256 | } |
| 257 | #define OPT_NEGBIT(s, l, v, h, b) { \ |
| 258 | .type = OPTION_NEGBIT, \ |
| 259 | .short_name = (s), \ |
| 260 | .long_name = (l), \ |
| 261 | .value = (v), \ |
| 262 | .precision = sizeof(*v), \ |
| 263 | .help = (h), \ |
| 264 | .flags = PARSE_OPT_NOARG, \ |
| 265 | .defval = (b), \ |
| 266 | } |
| 267 | #define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0) |
| 268 | #define OPT_SET_INT(s, l, v, h, i) OPT_SET_INT_F(s, l, v, h, i, 0) |
| 269 | #define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0) |
| 270 | #define OPT_HIDDEN_BOOL(s, l, v, h) { \ |
| 271 | .type = OPTION_SET_INT, \ |
| 272 | .short_name = (s), \ |
| 273 | .long_name = (l), \ |
| 274 | .value = (v), \ |
| 275 | .precision = sizeof(*v), \ |
| 276 | .help = (h), \ |
| 277 | .flags = PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, \ |
| 278 | .defval = 1, \ |
| 279 | } |
| 280 | #define OPT_CMDMODE_F(s, l, v, h, i, f) { \ |
| 281 | .type = OPTION_SET_INT, \ |
| 282 | .short_name = (s), \ |
| 283 | .long_name = (l), \ |
| 284 | .value = (v), \ |
| 285 | .precision = sizeof(*v), \ |
| 286 | .help = (h), \ |
| 287 | .flags = PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), \ |
| 288 | .defval = (i), \ |
| 289 | } |
| 290 | #define OPT_CMDMODE(s, l, v, h, i) OPT_CMDMODE_F(s, l, v, h, i, 0) |
| 291 | |
| 292 | #define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0) |
| 293 | #define OPT_UNSIGNED(s, l, v, h) { \ |
| 294 | .type = OPTION_UNSIGNED, \ |
| 295 | .short_name = (s), \ |
| 296 | .long_name = (l), \ |
| 297 | .value = (v) + BARF_UNLESS_UNSIGNED(*(v)), \ |
| 298 | .precision = sizeof(*v), \ |
| 299 | .argh = N_("n"), \ |
| 300 | .help = (h), \ |
| 301 | .flags = PARSE_OPT_NONEG, \ |
| 302 | } |
| 303 | #define OPT_STRING(s, l, v, a, h) OPT_STRING_F(s, l, v, a, h, 0) |
| 304 | #define OPT_STRING_LIST(s, l, v, a, h) { \ |
| 305 | .type = OPTION_CALLBACK, \ |
| 306 | .short_name = (s), \ |
| 307 | .long_name = (l), \ |
| 308 | .value = (v), \ |
| 309 | .argh = (a), \ |
| 310 | .help = (h), \ |
| 311 | .callback = &parse_opt_string_list, \ |
| 312 | } |
| 313 | #define OPT_STRVEC(s, l, v, a, h) { \ |
| 314 | .type = OPTION_CALLBACK, \ |
| 315 | .short_name = (s), \ |
| 316 | .long_name = (l), \ |
| 317 | .value = (v), \ |
| 318 | .argh = (a), \ |
| 319 | .help = (h), \ |
| 320 | .callback = &parse_opt_strvec, \ |
| 321 | } |
| 322 | #define OPT_UYN(s, l, v, h) { \ |
| 323 | .type = OPTION_CALLBACK, \ |
| 324 | .short_name = (s), \ |
| 325 | .long_name = (l), \ |
| 326 | .value = (v), \ |
| 327 | .help = (h), \ |
| 328 | .flags = PARSE_OPT_NOARG, \ |
| 329 | .callback = &parse_opt_tertiary, \ |
| 330 | } |
| 331 | #define OPT_EXPIRY_DATE(s, l, v, h) { \ |
| 332 | .type = OPTION_CALLBACK, \ |
| 333 | .short_name = (s), \ |
| 334 | .long_name = (l), \ |
| 335 | .value = (v), \ |
| 336 | .argh = N_("expiry-date"), \ |
| 337 | .help = (h), \ |
| 338 | .callback = parse_opt_expiry_date_cb, \ |
| 339 | } |
| 340 | #define OPT_CALLBACK(s, l, v, a, h, cb) OPT_CALLBACK_F(s, l, v, a, h, 0, cb) |
| 341 | #define OPT_NUMBER_CALLBACK(v, h, cb) { \ |
| 342 | .type = OPTION_NUMBER, \ |
| 343 | .value = (v), \ |
| 344 | .help = (h), \ |
| 345 | .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \ |
| 346 | .callback = (cb), \ |
| 347 | } |
| 348 | #define OPT_FILENAME(s, l, v, h) { \ |
| 349 | .type = OPTION_FILENAME, \ |
| 350 | .short_name = (s), \ |
| 351 | .long_name = (l), \ |
| 352 | .value = (v), \ |
| 353 | .argh = N_("file"), \ |
| 354 | .help = (h), \ |
| 355 | } |
| 356 | #define OPT_COLOR_FLAG(s, l, v, h) { \ |
| 357 | .type = OPTION_CALLBACK, \ |
| 358 | .short_name = (s), \ |
| 359 | .long_name = (l), \ |
| 360 | .value = (v), \ |
| 361 | .argh = N_("when"), \ |
| 362 | .help = (h), \ |
| 363 | .flags = PARSE_OPT_OPTARG, \ |
| 364 | .callback = parse_opt_color_flag_cb, \ |
| 365 | .defval = (intptr_t)"always", \ |
| 366 | } |
| 367 | |
| 368 | #define OPT_NOOP_NOARG(s, l) { \ |
| 369 | .type = OPTION_CALLBACK, \ |
| 370 | .short_name = (s), \ |
| 371 | .long_name = (l), \ |
| 372 | .help = N_("no-op (backward compatibility)"), \ |
| 373 | .flags = PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, \ |
| 374 | .callback = parse_opt_noop_cb, \ |
| 375 | } |
| 376 | |
| 377 | static char *parse_options_noop_ignored_value MAYBE_UNUSED; |
| 378 | #define OPT_NOOP_ARG(s, l) { \ |
| 379 | .type = OPTION_CALLBACK, \ |
| 380 | .short_name = (s), \ |
| 381 | .long_name = (l), \ |
| 382 | .value = &parse_options_noop_ignored_value, \ |
| 383 | .argh = "ignored", \ |
| 384 | .help = N_("no-op (backward compatibility)"), \ |
| 385 | .flags = PARSE_OPT_HIDDEN, \ |
| 386 | .callback = parse_opt_noop_cb, \ |
| 387 | } |
| 388 | |
| 389 | #define OPT_ALIAS(s, l, source_long_name) { \ |
| 390 | .type = OPTION_ALIAS, \ |
| 391 | .short_name = (s), \ |
| 392 | .long_name = (l), \ |
| 393 | .value = (char *)(source_long_name), \ |
| 394 | } |
| 395 | |
| 396 | #define OPT_SUBCOMMAND_F(l, v, fn, f) { \ |
| 397 | .type = OPTION_SUBCOMMAND, \ |
| 398 | .long_name = (l), \ |
| 399 | .value = (v), \ |
| 400 | .flags = (f), \ |
| 401 | .subcommand_fn = (fn), \ |
| 402 | } |
| 403 | #define OPT_SUBCOMMAND(l, v, fn) OPT_SUBCOMMAND_F((l), (v), (fn), 0) |
| 404 | |
| 405 | /* |
| 406 | * parse_options() will filter out the processed options and leave the |
| 407 | * non-option arguments in argv[]. argv0 is assumed program name and |
| 408 | * skipped. |
| 409 | * |
| 410 | * usagestr strings should be marked for translation with N_(). |
| 411 | * |
| 412 | * Returns the number of arguments left in argv[]. |
| 413 | * |
| 414 | * In one-shot mode, argv0 is not a program name, argv[] is left |
| 415 | * untouched and parse_options() returns the number of options |
| 416 | * processed. |
| 417 | */ |
| 418 | int parse_options(int argc, const char **argv, const char *prefix, |
| 419 | const struct option *options, |
| 420 | const char * const usagestr[], |
| 421 | enum parse_opt_flags flags); |
| 422 | |
| 423 | NORETURN void usage_with_options(const char * const *usagestr, |
| 424 | const struct option *options); |
| 425 | |
| 426 | void show_usage_with_options_if_asked(int ac, const char **av, |
| 427 | const char * const *usage, |
| 428 | const struct option *options); |
| 429 | |
| 430 | NORETURN void usage_msg_opt(const char *msg, |
| 431 | const char * const *usagestr, |
| 432 | const struct option *options); |
| 433 | |
| 434 | /** |
| 435 | * usage_msg_optf() is like usage_msg_opt() except that the first |
| 436 | * argument is a format string, and optional format arguments follow |
| 437 | * after the 3rd option. |
| 438 | */ |
| 439 | __attribute__((format (printf,1,4))) |
| 440 | void NORETURN usage_msg_optf(const char *fmt, |
| 441 | const char * const *usagestr, |
| 442 | const struct option *options, ...); |
| 443 | |
| 444 | void die_for_incompatible_opt4(int opt1, const char *opt1_name, |
| 445 | int opt2, const char *opt2_name, |
| 446 | int opt3, const char *opt3_name, |
| 447 | int opt4, const char *opt4_name); |
| 448 | |
| 449 | |
| 450 | static inline void die_for_incompatible_opt3(int opt1, const char *opt1_name, |
| 451 | int opt2, const char *opt2_name, |
| 452 | int opt3, const char *opt3_name) |
| 453 | { |
| 454 | die_for_incompatible_opt4(opt1, opt1_name, |
| 455 | opt2, opt2_name, |
| 456 | opt3, opt3_name, |
| 457 | 0, ""); |
| 458 | } |
| 459 | |
| 460 | static inline void die_for_incompatible_opt2(int opt1, const char *opt1_name, |
| 461 | int opt2, const char *opt2_name) |
| 462 | { |
| 463 | die_for_incompatible_opt4(opt1, opt1_name, |
| 464 | opt2, opt2_name, |
| 465 | 0, "", |
| 466 | 0, ""); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Use these assertions for callbacks that expect to be called with NONEG and |
| 471 | * NOARG respectively, and do not otherwise handle the "unset" and "arg" |
| 472 | * parameters. |
| 473 | */ |
| 474 | #define BUG_ON_OPT_NEG(unset) do { \ |
| 475 | if ((unset)) \ |
| 476 | BUG("option callback does not expect negation"); \ |
| 477 | } while (0) |
| 478 | #define BUG_ON_OPT_ARG(arg) do { \ |
| 479 | if ((arg)) \ |
| 480 | BUG("option callback does not expect an argument"); \ |
| 481 | } while (0) |
| 482 | |
| 483 | /* |
| 484 | * Similar to the assertions above, but checks that "arg" is always non-NULL. |
| 485 | * This assertion also implies BUG_ON_OPT_NEG(), letting you declare both |
| 486 | * assertions in a single line. |
| 487 | */ |
| 488 | #define BUG_ON_OPT_NEG_NOARG(unset, arg) do { \ |
| 489 | BUG_ON_OPT_NEG(unset); \ |
| 490 | if(!(arg)) \ |
| 491 | BUG("option callback expects an argument"); \ |
| 492 | } while(0) |
| 493 | |
| 494 | /*----- incremental advanced APIs -----*/ |
| 495 | |
| 496 | struct parse_opt_cmdmode_list; |
| 497 | |
| 498 | /* |
| 499 | * It's okay for the caller to consume argv/argc in the usual way. |
| 500 | * Other fields of that structure are private to parse-options and should not |
| 501 | * be modified in any way. |
| 502 | */ |
| 503 | struct parse_opt_ctx_t { |
| 504 | const char **argv; |
| 505 | const char **out; |
| 506 | int argc, cpidx, total; |
| 507 | const char *opt; |
| 508 | enum parse_opt_flags flags; |
| 509 | unsigned has_subcommands; |
| 510 | const char *prefix; |
| 511 | const char **alias_groups; /* must be in groups of 3 elements! */ |
| 512 | struct parse_opt_cmdmode_list *cmdmode_list; |
| 513 | }; |
| 514 | |
| 515 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
| 516 | int argc, const char **argv, const char *prefix, |
| 517 | const struct option *options, |
| 518 | enum parse_opt_flags flags); |
| 519 | |
| 520 | enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, |
| 521 | const struct option *options, |
| 522 | const char * const usagestr[]); |
| 523 | |
| 524 | int parse_options_end(struct parse_opt_ctx_t *ctx); |
| 525 | |
| 526 | struct option *parse_options_dup(const struct option *a); |
| 527 | struct option *parse_options_concat(const struct option *a, const struct option *b); |
| 528 | |
| 529 | /*----- some often used options -----*/ |
| 530 | int parse_opt_abbrev_cb(const struct option *, const char *, int); |
| 531 | int parse_opt_expiry_date_cb(const struct option *, const char *, int); |
| 532 | int parse_opt_color_flag_cb(const struct option *, const char *, int); |
| 533 | int parse_opt_verbosity_cb(const struct option *, const char *, int); |
| 534 | /* value is struct oid_array* */ |
| 535 | int parse_opt_object_name(const struct option *, const char *, int); |
| 536 | /* value is struct object_id* */ |
| 537 | int parse_opt_object_id(const struct option *, const char *, int); |
| 538 | int parse_opt_commits(const struct option *, const char *, int); |
| 539 | int parse_opt_commit(const struct option *, const char *, int); |
| 540 | int parse_opt_tertiary(const struct option *, const char *, int); |
| 541 | int parse_opt_string_list(const struct option *, const char *, int); |
| 542 | int parse_opt_strvec(const struct option *, const char *, int); |
| 543 | int parse_opt_noop_cb(const struct option *, const char *, int); |
| 544 | int parse_opt_passthru(const struct option *, const char *, int); |
| 545 | int parse_opt_passthru_argv(const struct option *, const char *, int); |
| 546 | /* value is enum branch_track* */ |
| 547 | int parse_opt_tracking_mode(const struct option *, const char *, int); |
| 548 | |
| 549 | #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h)) |
| 550 | #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h)) |
| 551 | #define OPT__VERBOSITY(var) { \ |
| 552 | .type = OPTION_CALLBACK, \ |
| 553 | .short_name = 'v', \ |
| 554 | .long_name = "verbose", \ |
| 555 | .value = (var), \ |
| 556 | .help = N_("be more verbose"), \ |
| 557 | .flags = PARSE_OPT_NOARG, \ |
| 558 | .callback = &parse_opt_verbosity_cb, \ |
| 559 | }, { \ |
| 560 | .type = OPTION_CALLBACK, \ |
| 561 | .short_name = 'q', \ |
| 562 | .long_name = "quiet", \ |
| 563 | .value = (var), \ |
| 564 | .help = N_("be more quiet"), \ |
| 565 | .flags = PARSE_OPT_NOARG, \ |
| 566 | .callback = &parse_opt_verbosity_cb, \ |
| 567 | } |
| 568 | #define OPT__DRY_RUN(var, h) OPT_BOOL('n', "dry-run", (var), (h)) |
| 569 | #define OPT__FORCE(var, h, f) OPT_COUNTUP_F('f', "force", (var), (h), (f)) |
| 570 | #define OPT__ABBREV(var) { \ |
| 571 | .type = OPTION_CALLBACK, \ |
| 572 | .long_name = "abbrev", \ |
| 573 | .value = (var), \ |
| 574 | .argh = N_("n"), \ |
| 575 | .help = N_("use <n> digits to display object names"), \ |
| 576 | .flags = PARSE_OPT_OPTARG, \ |
| 577 | .callback = &parse_opt_abbrev_cb, \ |
| 578 | } |
| 579 | #define OPT__SUPER_PREFIX(var) \ |
| 580 | OPT_STRING_F(0, "super-prefix", (var), N_("prefix"), \ |
| 581 | N_("prefixed path to initial superproject"), PARSE_OPT_HIDDEN) |
| 582 | |
| 583 | #define OPT__COLOR(var, h) \ |
| 584 | OPT_COLOR_FLAG(0, "color", (var), (h)) |
| 585 | #define OPT_COLUMN(s, l, v, h) { \ |
| 586 | .type = OPTION_CALLBACK, \ |
| 587 | .short_name = (s), \ |
| 588 | .long_name = (l), \ |
| 589 | .value = (v), \ |
| 590 | .argh = N_("style"), \ |
| 591 | .help = (h), \ |
| 592 | .flags = PARSE_OPT_OPTARG, \ |
| 593 | .callback = parseopt_column_callback, \ |
| 594 | } |
| 595 | #define OPT_PASSTHRU(s, l, v, a, h, f) { \ |
| 596 | .type = OPTION_CALLBACK, \ |
| 597 | .short_name = (s), \ |
| 598 | .long_name = (l), \ |
| 599 | .value = (v), \ |
| 600 | .argh = (a), \ |
| 601 | .help = (h), \ |
| 602 | .flags = (f), \ |
| 603 | .callback = parse_opt_passthru, \ |
| 604 | } |
| 605 | #define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) { \ |
| 606 | .type = OPTION_CALLBACK, \ |
| 607 | .short_name = (s), \ |
| 608 | .long_name = (l), \ |
| 609 | .value = (v), \ |
| 610 | .argh = (a), \ |
| 611 | .help = (h), \ |
| 612 | .flags = (f), \ |
| 613 | .callback = parse_opt_passthru_argv, \ |
| 614 | } |
| 615 | #define _OPT_CONTAINS_OR_WITH(l, v, h, f) { \ |
| 616 | .type = OPTION_CALLBACK, \ |
| 617 | .long_name = (l), \ |
| 618 | .value = (v), \ |
| 619 | .argh = N_("commit"), \ |
| 620 | .help = (h), \ |
| 621 | .flags = PARSE_OPT_LASTARG_DEFAULT | (f), \ |
| 622 | .callback = parse_opt_commits, \ |
| 623 | .defval = (intptr_t) "HEAD", \ |
| 624 | } |
| 625 | #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG) |
| 626 | #define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG) |
| 627 | #define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) |
| 628 | #define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) |
| 629 | #define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message")) |
| 630 | #define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file")) |
| 631 | #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")) |
| 632 | #define OPT_AUTOSTASH(v) OPT_BOOL(0, "autostash", v, N_("automatically stash/stash pop before and after")) |
| 633 | #define OPT_DIFF_UNIFIED(v) OPT_INTEGER_F('U', "unified", v, N_("generate diffs with <n> lines context"), PARSE_OPT_NONEG) |
| 634 | #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) |
| 635 | |
| 636 | #define OPT_IPVERSION(v) \ |
| 637 | OPT_SET_INT_F('4', "ipv4", (v), N_("use IPv4 addresses only"), \ |
| 638 | TRANSPORT_FAMILY_IPV4, PARSE_OPT_NONEG), \ |
| 639 | OPT_SET_INT_F('6', "ipv6", (v), N_("use IPv6 addresses only"), \ |
| 640 | TRANSPORT_FAMILY_IPV6, PARSE_OPT_NONEG) |
| 641 | |
| 642 | #endif |