1 #include "git-compat-util.h"
2 #include "parse-options.h"
8 #include "string-list.h"
10 #include "oid-array.h"
12 /*----- some often used options -----*/
14 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
19 v
= unset
? 0 : DEFAULT_ABBREV
;
22 return error(_("option `%s' expects a numerical value"),
24 v
= strtol(arg
, (char **)&arg
, 10);
26 return error(_("option `%s' expects a numerical value"),
28 if (v
&& v
< MINIMUM_ABBREV
)
30 else if (v
> the_hash_algo
->hexsz
)
31 v
= the_hash_algo
->hexsz
;
33 *(int *)(opt
->value
) = v
;
37 int parse_opt_expiry_date_cb(const struct option
*opt
, const char *arg
,
42 if (parse_expiry_date(arg
, (timestamp_t
*)opt
->value
))
43 die(_("malformed expiration date '%s'"), arg
);
47 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
53 arg
= unset
? "never" : (const char *)opt
->defval
;
54 value
= git_config_colorbool(NULL
, arg
);
56 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
58 *(int *)opt
->value
= value
;
62 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
65 int *target
= opt
->value
;
70 /* --no-quiet, --no-verbose */
72 else if (opt
->short_name
== 'v') {
86 int parse_opt_commits(const struct option
*opt
, const char *arg
, int unset
)
89 struct commit
*commit
;
91 BUG_ON_OPT_NEG(unset
);
95 if (get_oid(arg
, &oid
))
96 return error("malformed object name %s", arg
);
97 commit
= lookup_commit_reference(the_repository
, &oid
);
99 return error("no such commit %s", arg
);
100 commit_list_insert(commit
, opt
->value
);
104 int parse_opt_commit(const struct option
*opt
, const char *arg
, int unset
)
106 struct object_id oid
;
107 struct commit
*commit
;
108 struct commit
**target
= opt
->value
;
110 BUG_ON_OPT_NEG(unset
);
114 if (get_oid(arg
, &oid
))
115 return error("malformed object name %s", arg
);
116 commit
= lookup_commit_reference(the_repository
, &oid
);
118 return error("no such commit %s", arg
);
123 int parse_opt_object_name(const struct option
*opt
, const char *arg
, int unset
)
125 struct object_id oid
;
128 oid_array_clear(opt
->value
);
133 if (get_oid(arg
, &oid
))
134 return error(_("malformed object name '%s'"), arg
);
135 oid_array_append(opt
->value
, &oid
);
139 int parse_opt_object_id(const struct option
*opt
, const char *arg
, int unset
)
141 struct object_id oid
;
142 struct object_id
*target
= opt
->value
;
145 oidcpy(target
, null_oid());
150 if (get_oid(arg
, &oid
))
151 return error(_("malformed object name '%s'"), arg
);
156 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
158 int *target
= opt
->value
;
162 *target
= unset
? 2 : 1;
166 static size_t parse_options_count(const struct option
*opt
)
170 for (; opt
&& opt
->type
!= OPTION_END
; opt
++)
175 struct option
*parse_options_dup(const struct option
*o
)
177 struct option no_options
[] = { OPT_END() };
179 return parse_options_concat(o
, no_options
);
182 struct option
*parse_options_concat(const struct option
*a
,
183 const struct option
*b
)
186 size_t a_len
= parse_options_count(a
);
187 size_t b_len
= parse_options_count(b
);
189 ALLOC_ARRAY(ret
, st_add3(a_len
, b_len
, 1));
190 COPY_ARRAY(ret
, a
, a_len
);
191 COPY_ARRAY(ret
+ a_len
, b
, b_len
+ 1); /* + 1 for final OPTION_END */
196 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
198 struct string_list
*v
= opt
->value
;
201 string_list_clear(v
, 0);
208 string_list_append(v
, arg
);
212 int parse_opt_noop_cb(const struct option
*opt
, const char *arg
, int unset
)
218 * Report that the option is unknown, so that other code can handle
219 * it. This can be used as a callback together with
220 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
221 * "-h" output even if it's not being handled directly by
224 enum parse_opt_result
parse_opt_unknown_cb(struct parse_opt_ctx_t
*ctx
,
225 const struct option
*opt
,
226 const char *arg
, int unset
)
229 return PARSE_OPT_UNKNOWN
;
233 * Recreates the command-line option in the strbuf.
235 static int recreate_opt(struct strbuf
*sb
, const struct option
*opt
,
236 const char *arg
, int unset
)
240 if (opt
->long_name
) {
241 strbuf_addstr(sb
, unset
? "--no-" : "--");
242 strbuf_addstr(sb
, opt
->long_name
);
244 strbuf_addch(sb
, '=');
245 strbuf_addstr(sb
, arg
);
247 } else if (opt
->short_name
&& !unset
) {
248 strbuf_addch(sb
, '-');
249 strbuf_addch(sb
, opt
->short_name
);
251 strbuf_addstr(sb
, arg
);
259 * For an option opt, recreates the command-line option in opt->value which
260 * must be an char* initialized to NULL. This is useful when we need to pass
261 * the command-line option to another command. Since any previous value will be
262 * overwritten, this callback should only be used for options where the last
265 int parse_opt_passthru(const struct option
*opt
, const char *arg
, int unset
)
267 static struct strbuf sb
= STRBUF_INIT
;
268 char **opt_value
= opt
->value
;
270 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
275 *opt_value
= strbuf_detach(&sb
, NULL
);
281 * For an option opt, recreate the command-line option, appending it to
282 * opt->value which must be a strvec. This is useful when we need to pass
283 * the command-line option, which can be specified multiple times, to another
286 int parse_opt_passthru_argv(const struct option
*opt
, const char *arg
, int unset
)
288 static struct strbuf sb
= STRBUF_INIT
;
289 struct strvec
*opt_value
= opt
->value
;
291 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
294 strvec_push(opt_value
, sb
.buf
);
299 int parse_opt_tracking_mode(const struct option
*opt
, const char *arg
, int unset
)
302 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_NEVER
;
303 else if (!arg
|| !strcmp(arg
, "direct"))
304 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_EXPLICIT
;
305 else if (!strcmp(arg
, "inherit"))
306 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_INHERIT
;
308 return error(_("option `%s' expects \"%s\" or \"%s\""),
309 "--track", "direct", "inherit");