1 #include "git-compat-util.h"
2 #include "parse-options.h"
6 #include "string-list.h"
7 #include "argv-array.h"
9 /*----- some often used options -----*/
11 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
16 v = unset ? 0 : DEFAULT_ABBREV;
18 v = strtol(arg, (char **)&arg, 10);
20 return opterror(opt, "expects a numerical value", 0);
21 if (v && v < MINIMUM_ABBREV)
26 *(int *)(opt->value) = v;
30 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
33 *(unsigned long *)(opt->value) = approxidate(arg);
37 int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
40 return parse_expiry_date(arg, (unsigned long *)opt->value);
43 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
49 arg = unset ? "never" : (const char *)opt->defval;
50 value = git_config_colorbool(NULL, arg);
53 "expects \"always\", \"auto\", or \"never\"", 0);
54 *(int *)opt->value = value;
58 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
61 int *target = opt->value;
64 /* --no-quiet, --no-verbose */
66 else if (opt->short_name == 'v') {
80 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
82 unsigned char sha1[20];
83 struct commit *commit;
87 if (get_sha1(arg, sha1))
88 return error("malformed object name %s", arg);
89 commit = lookup_commit_reference(sha1);
91 return error("no such commit %s", arg);
92 commit_list_insert(commit, opt->value);
96 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
98 int *target = opt->value;
99 *target = unset ? 2 : 1;
103 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
107 for (i = 0; i < dst_size; i++)
108 if (dst[i].type == OPTION_END)
110 for (j = 0; i < dst_size; i++, j++) {
112 if (src[j].type == OPTION_END)
118 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
120 struct string_list *v = opt->value;
123 string_list_clear(v, 0);
130 string_list_append(v, xstrdup(arg));
134 int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
140 * Recreates the command-line option in the strbuf.
142 static int recreate_opt(struct strbuf *sb, const struct option *opt,
143 const char *arg, int unset)
147 if (opt->long_name) {
148 strbuf_addstr(sb, unset ? "--no-" : "--");
149 strbuf_addstr(sb, opt->long_name);
151 strbuf_addch(sb, '=');
152 strbuf_addstr(sb, arg);
154 } else if (opt->short_name && !unset) {
155 strbuf_addch(sb, '-');
156 strbuf_addch(sb, opt->short_name);
158 strbuf_addstr(sb, arg);
166 * For an option opt, recreates the command-line option in opt->value which
167 * must be an char* initialized to NULL. This is useful when we need to pass
168 * the command-line option to another command. Since any previous value will be
169 * overwritten, this callback should only be used for options where the last
172 int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
174 static struct strbuf sb = STRBUF_INIT;
175 char **opt_value = opt->value;
177 if (recreate_opt(&sb, opt, arg, unset) < 0)
183 *opt_value = strbuf_detach(&sb, NULL);
189 * For an option opt, recreate the command-line option, appending it to
190 * opt->value which must be a argv_array. This is useful when we need to pass
191 * the command-line option, which can be specified multiple times, to another
194 int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
196 static struct strbuf sb = STRBUF_INIT;
197 struct argv_array *opt_value = opt->value;
199 if (recreate_opt(&sb, opt, arg, unset) < 0)
202 argv_array_push(opt_value, sb.buf);