f1f246387cae57748a04efa139da7a1d4b8951be
   1#ifndef PARSE_OPTIONS_H
   2#define PARSE_OPTIONS_H
   3
   4enum parse_opt_type {
   5        /* special types */
   6        OPTION_END,
   7        OPTION_ARGUMENT,
   8        OPTION_GROUP,
   9        OPTION_NUMBER,
  10        /* options with no arguments */
  11        OPTION_BIT,
  12        OPTION_NEGBIT,
  13        OPTION_BITOP,
  14        OPTION_COUNTUP,
  15        OPTION_SET_INT,
  16        OPTION_CMDMODE,
  17        /* options with arguments (usually) */
  18        OPTION_STRING,
  19        OPTION_INTEGER,
  20        OPTION_MAGNITUDE,
  21        OPTION_CALLBACK,
  22        OPTION_LOWLEVEL_CALLBACK,
  23        OPTION_FILENAME
  24};
  25
  26enum parse_opt_flags {
  27        PARSE_OPT_KEEP_DASHDASH = 1,
  28        PARSE_OPT_STOP_AT_NON_OPTION = 2,
  29        PARSE_OPT_KEEP_ARGV0 = 4,
  30        PARSE_OPT_KEEP_UNKNOWN = 8,
  31        PARSE_OPT_NO_INTERNAL_HELP = 16,
  32        PARSE_OPT_ONE_SHOT = 32
  33};
  34
  35enum parse_opt_option_flags {
  36        PARSE_OPT_OPTARG  = 1,
  37        PARSE_OPT_NOARG   = 2,
  38        PARSE_OPT_NONEG   = 4,
  39        PARSE_OPT_HIDDEN  = 8,
  40        PARSE_OPT_LASTARG_DEFAULT = 16,
  41        PARSE_OPT_NODASH = 32,
  42        PARSE_OPT_LITERAL_ARGHELP = 64,
  43        PARSE_OPT_SHELL_EVAL = 256,
  44        PARSE_OPT_NOCOMPLETE = 512,
  45        PARSE_OPT_COMP_ARG = 1024
  46};
  47
  48struct option;
  49typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
  50
  51struct parse_opt_ctx_t;
  52typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
  53                                const struct option *opt, int unset);
  54
  55/*
  56 * `type`::
  57 *   holds the type of the option, you must have an OPTION_END last in your
  58 *   array.
  59 *
  60 * `short_name`::
  61 *   the character to use as a short option name, '\0' if none.
  62 *
  63 * `long_name`::
  64 *   the long option name, without the leading dashes, NULL if none.
  65 *
  66 * `value`::
  67 *   stores pointers to the values to be filled.
  68 *
  69 * `argh`::
  70 *   token to explain the kind of argument this option wants. Keep it
  71 *   homogeneous across the repository. Should be wrapped by N_() for
  72 *   translation.
  73 *
  74 * `help`::
  75 *   the short help associated to what the option does.
  76 *   Must never be NULL (except for OPTION_END).
  77 *   OPTION_GROUP uses this pointer to store the group header.
  78 *   Should be wrapped by N_() for translation.
  79 *
  80 * `flags`::
  81 *   mask of parse_opt_option_flags.
  82 *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
  83 *   PARSE_OPT_NOARG: says that this option does not take an argument
  84 *   PARSE_OPT_NONEG: says that this option cannot be negated
  85 *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
  86 *                     shown only in the full usage.
  87 *   PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
  88 *                              value if no argument is given when the option
  89 *                              is last on the command line. If the option is
  90 *                              not last it will require an argument.
  91 *                              Should not be used with PARSE_OPT_OPTARG.
  92 *   PARSE_OPT_NODASH: this option doesn't start with a dash.
  93 *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
  94 *                              (i.e. '<argh>') in the help message.
  95 *                              Useful for options with multiple parameters.
  96 *   PARSE_OPT_NOCOMPLETE: by default all visible options are completable
  97 *                         by git-completion.bash. This option suppresses that.
  98 *   PARSE_OPT_COMP_ARG: this option forces to git-completion.bash to
  99 *                       complete an option as --name= not --name even if
 100 *                       the option takes optional argument.
 101 *
 102 * `callback`::
 103 *   pointer to the callback to use for OPTION_CALLBACK
 104 *
 105 * `defval`::
 106 *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
 107 *   OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
 108 *   CALLBACKS can use it like they want.
 109 *
 110 * `ll_callback`::
 111 *   pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
 112 *
 113 */
 114struct option {
 115        enum parse_opt_type type;
 116        int short_name;
 117        const char *long_name;
 118        void *value;
 119        const char *argh;
 120        const char *help;
 121
 122        int flags;
 123        parse_opt_cb *callback;
 124        intptr_t defval;
 125        parse_opt_ll_cb *ll_callback;
 126        intptr_t extra;
 127};
 128
 129#define OPT_BIT_F(s, l, v, h, b, f) { OPTION_BIT, (s), (l), (v), NULL, (h), \
 130                                      PARSE_OPT_NOARG|(f), NULL, (b) }
 131#define OPT_COUNTUP_F(s, l, v, h, f) { OPTION_COUNTUP, (s), (l), (v), NULL, \
 132                                       (h), PARSE_OPT_NOARG|(f) }
 133#define OPT_SET_INT_F(s, l, v, h, i, f) { OPTION_SET_INT, (s), (l), (v), NULL, \
 134                                          (h), PARSE_OPT_NOARG | (f), NULL, (i) }
 135#define OPT_BOOL_F(s, l, v, h, f)   OPT_SET_INT_F(s, l, v, h, 1, f)
 136
 137#define OPT_END()                   { OPTION_END }
 138#define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
 139                                      (h), PARSE_OPT_NOARG}
 140#define OPT_GROUP(h)                { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
 141#define OPT_BIT(s, l, v, h, b)      OPT_BIT_F(s, l, v, h, b, 0)
 142#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
 143                                            PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
 144                                            (set), NULL, (clear) }
 145#define OPT_NEGBIT(s, l, v, h, b)   { OPTION_NEGBIT, (s), (l), (v), NULL, \
 146                                      (h), PARSE_OPT_NOARG, NULL, (b) }
 147#define OPT_COUNTUP(s, l, v, h)     OPT_COUNTUP_F(s, l, v, h, 0)
 148#define OPT_SET_INT(s, l, v, h, i)  OPT_SET_INT_F(s, l, v, h, i, 0)
 149#define OPT_BOOL(s, l, v, h)        OPT_BOOL_F(s, l, v, h, 0)
 150#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
 151                                      (h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
 152#define OPT_CMDMODE(s, l, v, h, i)  { OPTION_CMDMODE, (s), (l), (v), NULL, \
 153                                      (h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
 154#define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
 155#define OPT_MAGNITUDE(s, l, v, h)   { OPTION_MAGNITUDE, (s), (l), (v), \
 156                                      N_("n"), (h), PARSE_OPT_NONEG }
 157#define OPT_STRING(s, l, v, a, h)   { OPTION_STRING,  (s), (l), (v), (a), (h) }
 158#define OPT_STRING_LIST(s, l, v, a, h) \
 159                                    { OPTION_CALLBACK, (s), (l), (v), (a), \
 160                                      (h), 0, &parse_opt_string_list }
 161#define OPT_UYN(s, l, v, h)         { OPTION_CALLBACK, (s), (l), (v), NULL, \
 162                                      (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
 163#define OPT_EXPIRY_DATE(s, l, v, h) \
 164        { OPTION_CALLBACK, (s), (l), (v), N_("expiry-date"),(h), 0,     \
 165          parse_opt_expiry_date_cb }
 166#define OPT_CALLBACK(s, l, v, a, h, f) \
 167        { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
 168#define OPT_NUMBER_CALLBACK(v, h, f) \
 169        { OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
 170          PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
 171#define OPT_FILENAME(s, l, v, h)    { OPTION_FILENAME, (s), (l), (v), \
 172                                       N_("file"), (h) }
 173#define OPT_COLOR_FLAG(s, l, v, h) \
 174        { OPTION_CALLBACK, (s), (l), (v), N_("when"), (h), PARSE_OPT_OPTARG, \
 175                parse_opt_color_flag_cb, (intptr_t)"always" }
 176
 177#define OPT_NOOP_NOARG(s, l) \
 178        { OPTION_CALLBACK, (s), (l), NULL, NULL, \
 179          N_("no-op (backward compatibility)"),         \
 180          PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
 181
 182/*
 183 * parse_options() will filter out the processed options and leave the
 184 * non-option arguments in argv[]. argv0 is assumed program name and
 185 * skipped.
 186 *
 187 * usagestr strings should be marked for translation with N_().
 188 *
 189 * Returns the number of arguments left in argv[].
 190 *
 191 * In one-shot mode, argv0 is not a program name, argv[] is left
 192 * untouched and parse_options() returns the number of options
 193 * processed.
 194 */
 195int parse_options(int argc, const char **argv, const char *prefix,
 196                  const struct option *options,
 197                  const char * const usagestr[], int flags);
 198
 199NORETURN void usage_with_options(const char * const *usagestr,
 200                                 const struct option *options);
 201
 202NORETURN void usage_msg_opt(const char *msg,
 203                            const char * const *usagestr,
 204                            const struct option *options);
 205
 206int optbug(const struct option *opt, const char *reason);
 207const char *optname(const struct option *opt, int flags);
 208
 209/*
 210 * Use these assertions for callbacks that expect to be called with NONEG and
 211 * NOARG respectively, and do not otherwise handle the "unset" and "arg"
 212 * parameters.
 213 */
 214#define BUG_ON_OPT_NEG(unset) do { \
 215        if ((unset)) \
 216                BUG("option callback does not expect negation"); \
 217} while (0)
 218#define BUG_ON_OPT_ARG(arg) do { \
 219        if ((arg)) \
 220                BUG("option callback does not expect an argument"); \
 221} while (0)
 222
 223/*----- incremental advanced APIs -----*/
 224
 225enum {
 226        PARSE_OPT_COMPLETE = -2,
 227        PARSE_OPT_HELP = -1,
 228        PARSE_OPT_DONE,
 229        PARSE_OPT_NON_OPTION,
 230        PARSE_OPT_ERROR,
 231        PARSE_OPT_UNKNOWN
 232};
 233
 234/*
 235 * It's okay for the caller to consume argv/argc in the usual way.
 236 * Other fields of that structure are private to parse-options and should not
 237 * be modified in any way.
 238 */
 239struct parse_opt_ctx_t {
 240        const char **argv;
 241        const char **out;
 242        int argc, cpidx, total;
 243        const char *opt;
 244        int flags;
 245        const char *prefix;
 246};
 247
 248void parse_options_start(struct parse_opt_ctx_t *ctx,
 249                         int argc, const char **argv, const char *prefix,
 250                         const struct option *options, int flags);
 251
 252int parse_options_step(struct parse_opt_ctx_t *ctx,
 253                       const struct option *options,
 254                       const char * const usagestr[]);
 255
 256int parse_options_end(struct parse_opt_ctx_t *ctx);
 257
 258struct option *parse_options_concat(struct option *a, struct option *b);
 259
 260/*----- some often used options -----*/
 261int parse_opt_abbrev_cb(const struct option *, const char *, int);
 262int parse_opt_expiry_date_cb(const struct option *, const char *, int);
 263int parse_opt_color_flag_cb(const struct option *, const char *, int);
 264int parse_opt_verbosity_cb(const struct option *, const char *, int);
 265int parse_opt_object_name(const struct option *, const char *, int);
 266int parse_opt_commits(const struct option *, const char *, int);
 267int parse_opt_tertiary(const struct option *, const char *, int);
 268int parse_opt_string_list(const struct option *, const char *, int);
 269int parse_opt_noop_cb(const struct option *, const char *, int);
 270int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx, const struct option *, int);
 271int parse_opt_passthru(const struct option *, const char *, int);
 272int parse_opt_passthru_argv(const struct option *, const char *, int);
 273
 274#define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
 275#define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))
 276#define OPT__VERBOSITY(var) \
 277        { OPTION_CALLBACK, 'v', "verbose", (var), NULL, N_("be more verbose"), \
 278          PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
 279        { OPTION_CALLBACK, 'q', "quiet", (var), NULL, N_("be more quiet"), \
 280          PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
 281#define OPT__DRY_RUN(var, h)  OPT_BOOL('n', "dry-run", (var), (h))
 282#define OPT__FORCE(var, h, f) OPT_COUNTUP_F('f', "force",   (var), (h), (f))
 283#define OPT__ABBREV(var)  \
 284        { OPTION_CALLBACK, 0, "abbrev", (var), N_("n"), \
 285          N_("use <n> digits to display SHA-1s"),       \
 286          PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
 287#define OPT__COLOR(var, h) \
 288        OPT_COLOR_FLAG(0, "color", (var), (h))
 289#define OPT_COLUMN(s, l, v, h) \
 290        { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
 291#define OPT_PASSTHRU(s, l, v, a, h, f) \
 292        { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru }
 293#define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) \
 294        { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv }
 295#define _OPT_CONTAINS_OR_WITH(name, variable, help, flag) \
 296        { OPTION_CALLBACK, 0, name, (variable), N_("commit"), (help), \
 297          PARSE_OPT_LASTARG_DEFAULT | flag, \
 298          parse_opt_commits, (intptr_t) "HEAD" \
 299        }
 300#define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG)
 301#define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG)
 302#define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
 303#define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
 304
 305#endif