1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 #include "string-list.h"
11 /*----- some often used options -----*/
13 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
18 v
= unset
? 0 : DEFAULT_ABBREV
;
21 return error(_("option `%s' expects a numerical value"),
23 v
= strtol(arg
, (char **)&arg
, 10);
25 return error(_("option `%s' expects a numerical value"),
27 if (v
&& v
< MINIMUM_ABBREV
)
29 else if (v
> the_hash_algo
->hexsz
)
30 v
= the_hash_algo
->hexsz
;
32 *(int *)(opt
->value
) = v
;
36 int parse_opt_expiry_date_cb(const struct option
*opt
, const char *arg
,
41 if (parse_expiry_date(arg
, (timestamp_t
*)opt
->value
))
42 die(_("malformed expiration date '%s'"), arg
);
46 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
52 arg
= unset
? "never" : (const char *)opt
->defval
;
53 value
= git_config_colorbool(NULL
, arg
);
55 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
57 *(int *)opt
->value
= value
;
61 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
64 int *target
= opt
->value
;
69 /* --no-quiet, --no-verbose */
71 else if (opt
->short_name
== 'v') {
85 int parse_opt_commits(const struct option
*opt
, const char *arg
, int unset
)
88 struct commit
*commit
;
90 BUG_ON_OPT_NEG(unset
);
94 if (get_oid(arg
, &oid
))
95 return error("malformed object name %s", arg
);
96 commit
= lookup_commit_reference(the_repository
, &oid
);
98 return error("no such commit %s", arg
);
99 commit_list_insert(commit
, opt
->value
);
103 int parse_opt_commit(const struct option
*opt
, const char *arg
, int unset
)
105 struct object_id oid
;
106 struct commit
*commit
;
107 struct commit
**target
= opt
->value
;
109 BUG_ON_OPT_NEG(unset
);
113 if (get_oid(arg
, &oid
))
114 return error("malformed object name %s", arg
);
115 commit
= lookup_commit_reference(the_repository
, &oid
);
117 return error("no such commit %s", arg
);
122 int parse_opt_object_name(const struct option
*opt
, const char *arg
, int unset
)
124 struct object_id oid
;
127 oid_array_clear(opt
->value
);
132 if (get_oid(arg
, &oid
))
133 return error(_("malformed object name '%s'"), arg
);
134 oid_array_append(opt
->value
, &oid
);
138 int parse_opt_object_id(const struct option
*opt
, const char *arg
, int unset
)
140 struct object_id oid
;
141 struct object_id
*target
= opt
->value
;
144 oidcpy(target
, null_oid());
149 if (get_oid(arg
, &oid
))
150 return error(_("malformed object name '%s'"), arg
);
155 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
157 int *target
= opt
->value
;
161 *target
= unset
? 2 : 1;
165 static size_t parse_options_count(const struct option
*opt
)
169 for (; opt
&& opt
->type
!= OPTION_END
; opt
++)
174 struct option
*parse_options_dup(const struct option
*o
)
176 struct option no_options
[] = { OPT_END() };
178 return parse_options_concat(o
, no_options
);
181 struct option
*parse_options_concat(const struct option
*a
,
182 const struct option
*b
)
185 size_t a_len
= parse_options_count(a
);
186 size_t b_len
= parse_options_count(b
);
188 ALLOC_ARRAY(ret
, st_add3(a_len
, b_len
, 1));
189 COPY_ARRAY(ret
, a
, a_len
);
190 COPY_ARRAY(ret
+ a_len
, b
, b_len
+ 1); /* + 1 for final OPTION_END */
195 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
197 struct string_list
*v
= opt
->value
;
200 string_list_clear(v
, 0);
207 string_list_append(v
, arg
);
211 int parse_opt_noop_cb(const struct option
*opt
, const char *arg
, int unset
)
217 * Report that the option is unknown, so that other code can handle
218 * it. This can be used as a callback together with
219 * OPTION_LOWLEVEL_CALLBACK to allow an option to be documented in the
220 * "-h" output even if it's not being handled directly by
223 enum parse_opt_result
parse_opt_unknown_cb(struct parse_opt_ctx_t
*ctx
,
224 const struct option
*opt
,
225 const char *arg
, int unset
)
228 return PARSE_OPT_UNKNOWN
;
232 * Recreates the command-line option in the strbuf.
234 static int recreate_opt(struct strbuf
*sb
, const struct option
*opt
,
235 const char *arg
, int unset
)
239 if (opt
->long_name
) {
240 strbuf_addstr(sb
, unset
? "--no-" : "--");
241 strbuf_addstr(sb
, opt
->long_name
);
243 strbuf_addch(sb
, '=');
244 strbuf_addstr(sb
, arg
);
246 } else if (opt
->short_name
&& !unset
) {
247 strbuf_addch(sb
, '-');
248 strbuf_addch(sb
, opt
->short_name
);
250 strbuf_addstr(sb
, arg
);
258 * For an option opt, recreates the command-line option in opt->value which
259 * must be an char* initialized to NULL. This is useful when we need to pass
260 * the command-line option to another command. Since any previous value will be
261 * overwritten, this callback should only be used for options where the last
264 int parse_opt_passthru(const struct option
*opt
, const char *arg
, int unset
)
266 static struct strbuf sb
= STRBUF_INIT
;
267 char **opt_value
= opt
->value
;
269 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
274 *opt_value
= strbuf_detach(&sb
, NULL
);
280 * For an option opt, recreate the command-line option, appending it to
281 * opt->value which must be a strvec. This is useful when we need to pass
282 * the command-line option, which can be specified multiple times, to another
285 int parse_opt_passthru_argv(const struct option
*opt
, const char *arg
, int unset
)
287 static struct strbuf sb
= STRBUF_INIT
;
288 struct strvec
*opt_value
= opt
->value
;
290 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
293 strvec_push(opt_value
, sb
.buf
);
298 int parse_opt_tracking_mode(const struct option
*opt
, const char *arg
, int unset
)
301 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_NEVER
;
302 else if (!arg
|| !strcmp(arg
, "direct"))
303 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_EXPLICIT
;
304 else if (!strcmp(arg
, "inherit"))
305 *(enum branch_track
*)opt
->value
= BRANCH_TRACK_INHERIT
;
307 return error(_("option `%s' expects \"%s\" or \"%s\""),
308 "--track", "direct", "inherit");