1 #include "git-compat-util.h"
2 #include "parse-options.h"
6 #include "string-list.h"
8 /*----- some often used options -----*/
10 int parse_opt_abbrev_cb(const struct option
*opt
, const char *arg
, int unset
)
15 v
= unset
? 0 : DEFAULT_ABBREV
;
17 v
= strtol(arg
, (char **)&arg
, 10);
19 return opterror(opt
, "expects a numerical value", 0);
20 if (v
&& v
< MINIMUM_ABBREV
)
25 *(int *)(opt
->value
) = v
;
29 int parse_opt_approxidate_cb(const struct option
*opt
, const char *arg
,
32 *(unsigned long *)(opt
->value
) = approxidate(arg
);
36 int parse_opt_expiry_date_cb(const struct option
*opt
, const char *arg
,
39 return parse_expiry_date(arg
, (unsigned long *)opt
->value
);
42 int parse_opt_color_flag_cb(const struct option
*opt
, const char *arg
,
48 arg
= unset
? "never" : (const char *)opt
->defval
;
49 value
= git_config_colorbool(NULL
, arg
);
52 "expects \"always\", \"auto\", or \"never\"", 0);
53 *(int *)opt
->value
= value
;
57 int parse_opt_verbosity_cb(const struct option
*opt
, const char *arg
,
60 int *target
= opt
->value
;
63 /* --no-quiet, --no-verbose */
65 else if (opt
->short_name
== 'v') {
79 int parse_opt_with_commit(const struct option
*opt
, const char *arg
, int unset
)
81 unsigned char sha1
[20];
82 struct commit
*commit
;
86 if (get_sha1(arg
, sha1
))
87 return error("malformed object name %s", arg
);
88 commit
= lookup_commit_reference(sha1
);
90 return error("no such commit %s", arg
);
91 commit_list_insert(commit
, opt
->value
);
95 int parse_opt_tertiary(const struct option
*opt
, const char *arg
, int unset
)
97 int *target
= opt
->value
;
98 *target
= unset
? 2 : 1;
102 int parse_options_concat(struct option
*dst
, size_t dst_size
, struct option
*src
)
106 for (i
= 0; i
< dst_size
; i
++)
107 if (dst
[i
].type
== OPTION_END
)
109 for (j
= 0; i
< dst_size
; i
++, j
++) {
111 if (src
[j
].type
== OPTION_END
)
117 int parse_opt_string_list(const struct option
*opt
, const char *arg
, int unset
)
119 struct string_list
*v
= opt
->value
;
122 string_list_clear(v
, 0);
129 string_list_append(v
, xstrdup(arg
));
133 int parse_opt_noop_cb(const struct option
*opt
, const char *arg
, int unset
)
139 * Recreates the command-line option in the strbuf.
141 static int recreate_opt(struct strbuf
*sb
, const struct option
*opt
,
142 const char *arg
, int unset
)
146 if (opt
->long_name
) {
147 strbuf_addstr(sb
, unset
? "--no-" : "--");
148 strbuf_addstr(sb
, opt
->long_name
);
150 strbuf_addch(sb
, '=');
151 strbuf_addstr(sb
, arg
);
153 } else if (opt
->short_name
&& !unset
) {
154 strbuf_addch(sb
, '-');
155 strbuf_addch(sb
, opt
->short_name
);
157 strbuf_addstr(sb
, arg
);
165 * For an option opt, recreates the command-line option in opt->value which
166 * must be an char* initialized to NULL. This is useful when we need to pass
167 * the command-line option to another command. Since any previous value will be
168 * overwritten, this callback should only be used for options where the last
171 int parse_opt_passthru(const struct option
*opt
, const char *arg
, int unset
)
173 static struct strbuf sb
= STRBUF_INIT
;
174 char **opt_value
= opt
->value
;
176 if (recreate_opt(&sb
, opt
, arg
, unset
) < 0)
182 *opt_value
= strbuf_detach(&sb
, NULL
);