parse-options-cb.con commit Merge branch 'ds/find-unique-abbrev-optim' into next (0b6d4f9)
   1#include "git-compat-util.h"
   2#include "parse-options.h"
   3#include "cache.h"
   4#include "commit.h"
   5#include "color.h"
   6#include "string-list.h"
   7#include "argv-array.h"
   8#include "sha1-array.h"
   9
  10/*----- some often used options -----*/
  11
  12int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
  13{
  14        int v;
  15
  16        if (!arg) {
  17                v = unset ? 0 : DEFAULT_ABBREV;
  18        } else {
  19                v = strtol(arg, (char **)&arg, 10);
  20                if (*arg)
  21                        return opterror(opt, "expects a numerical value", 0);
  22                if (v && v < MINIMUM_ABBREV)
  23                        v = MINIMUM_ABBREV;
  24                else if (v > 40)
  25                        v = 40;
  26        }
  27        *(int *)(opt->value) = v;
  28        return 0;
  29}
  30
  31int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
  32                             int unset)
  33{
  34        *(timestamp_t *)(opt->value) = approxidate(arg);
  35        return 0;
  36}
  37
  38int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
  39                             int unset)
  40{
  41        return parse_expiry_date(arg, (timestamp_t *)opt->value);
  42}
  43
  44int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
  45                            int unset)
  46{
  47        int value;
  48
  49        if (!arg)
  50                arg = unset ? "never" : (const char *)opt->defval;
  51        value = git_config_colorbool(NULL, arg);
  52        if (value < 0)
  53                return opterror(opt,
  54                        "expects \"always\", \"auto\", or \"never\"", 0);
  55        *(int *)opt->value = value;
  56        return 0;
  57}
  58
  59int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
  60                           int unset)
  61{
  62        int *target = opt->value;
  63
  64        if (unset)
  65                /* --no-quiet, --no-verbose */
  66                *target = 0;
  67        else if (opt->short_name == 'v') {
  68                if (*target >= 0)
  69                        (*target)++;
  70                else
  71                        *target = 1;
  72        } else {
  73                if (*target <= 0)
  74                        (*target)--;
  75                else
  76                        *target = -1;
  77        }
  78        return 0;
  79}
  80
  81int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
  82{
  83        struct object_id oid;
  84
  85        if (unset) {
  86                oid_array_clear(opt->value);
  87                return 0;
  88        }
  89        if (!arg)
  90                return -1;
  91        if (get_oid(arg, &oid))
  92                return error(_("malformed object name '%s'"), arg);
  93        oid_array_append(opt->value, &oid);
  94        return 0;
  95}
  96
  97int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
  98{
  99        int *target = opt->value;
 100        *target = unset ? 2 : 1;
 101        return 0;
 102}
 103
 104struct option *parse_options_concat(struct option *a, struct option *b)
 105{
 106        struct option *ret;
 107        size_t i, a_len = 0, b_len = 0;
 108
 109        for (i = 0; a[i].type != OPTION_END; i++)
 110                a_len++;
 111        for (i = 0; b[i].type != OPTION_END; i++)
 112                b_len++;
 113
 114        ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
 115        for (i = 0; i < a_len; i++)
 116                ret[i] = a[i];
 117        for (i = 0; i < b_len; i++)
 118                ret[a_len + i] = b[i];
 119        ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
 120
 121        return ret;
 122}
 123
 124int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
 125{
 126        struct string_list *v = opt->value;
 127
 128        if (unset) {
 129                string_list_clear(v, 0);
 130                return 0;
 131        }
 132
 133        if (!arg)
 134                return -1;
 135
 136        string_list_append(v, arg);
 137        return 0;
 138}
 139
 140int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
 141{
 142        return 0;
 143}
 144
 145/**
 146 * Report that the option is unknown, so that other code can handle
 147 * it. This can be used as a callback together with
 148 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
 149 * "-h" output even if it's not being handled directly by
 150 * parse_options().
 151 */
 152int parse_opt_unknown_cb(const struct option *opt, const char *arg, int unset)
 153{
 154        return -2;
 155}
 156
 157/**
 158 * Recreates the command-line option in the strbuf.
 159 */
 160static int recreate_opt(struct strbuf *sb, const struct option *opt,
 161                const char *arg, int unset)
 162{
 163        strbuf_reset(sb);
 164
 165        if (opt->long_name) {
 166                strbuf_addstr(sb, unset ? "--no-" : "--");
 167                strbuf_addstr(sb, opt->long_name);
 168                if (arg) {
 169                        strbuf_addch(sb, '=');
 170                        strbuf_addstr(sb, arg);
 171                }
 172        } else if (opt->short_name && !unset) {
 173                strbuf_addch(sb, '-');
 174                strbuf_addch(sb, opt->short_name);
 175                if (arg)
 176                        strbuf_addstr(sb, arg);
 177        } else
 178                return -1;
 179
 180        return 0;
 181}
 182
 183/**
 184 * For an option opt, recreates the command-line option in opt->value which
 185 * must be an char* initialized to NULL. This is useful when we need to pass
 186 * the command-line option to another command. Since any previous value will be
 187 * overwritten, this callback should only be used for options where the last
 188 * one wins.
 189 */
 190int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
 191{
 192        static struct strbuf sb = STRBUF_INIT;
 193        char **opt_value = opt->value;
 194
 195        if (recreate_opt(&sb, opt, arg, unset) < 0)
 196                return -1;
 197
 198        free(*opt_value);
 199
 200        *opt_value = strbuf_detach(&sb, NULL);
 201
 202        return 0;
 203}
 204
 205/**
 206 * For an option opt, recreate the command-line option, appending it to
 207 * opt->value which must be a argv_array. This is useful when we need to pass
 208 * the command-line option, which can be specified multiple times, to another
 209 * command.
 210 */
 211int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
 212{
 213        static struct strbuf sb = STRBUF_INIT;
 214        struct argv_array *opt_value = opt->value;
 215
 216        if (recreate_opt(&sb, opt, arg, unset) < 0)
 217                return -1;
 218
 219        argv_array_push(opt_value, sb.buf);
 220
 221        return 0;
 222}