parse-options: prefer opt->value to globals in callbacks
We have several parse-options callbacks that ignore their "opt" parameters entirely. This is a little unusual, as we'd normally put the result of the parsing into opt->value. In the case of these callbacks, though, they directly manipulate global variables instead (and in most cases the caller sets opt->value to NULL in the OPT_CALLBACK declaration). The immediate symptom we'd like to deal with is that the unused "opt" variables trigger -Wunused-parameter. But how to fix that is debatable. One option is to annotate them with UNUSED. But another is to have the caller pass in the appropriate variable via opt->value, and use it. That has the benefit of making the callbacks reusable (in theory at least), and makes it clear from the OPT_CALLBACK declaration which variables will be affected (doubly so for the cases in builtin/fast-export.c, where we do set opt->value, but it is completely ignored!). The slight downside is that we lose type safety, since they're now passing through void pointers. I went with the "just use them" approach here. The loss of type safety is unfortunate, but that is already an issue with most of the other callbacks. If we want to try to address that, we should do so more consistently (and this patch would prepare these callbacks for whatever we choose to do there). Note that in the cases in builtin/fast-export.c, we are passing anonymous enums. We'll have to give them names so that we can declare the appropriate pointer type within the callbacks. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
9b40386586
commit
66e3309294
@ -193,14 +193,16 @@ static const char * const builtin_checkout_index_usage[] = {
|
||||
static int option_parse_stage(const struct option *opt,
|
||||
const char *arg, int unset)
|
||||
{
|
||||
int *stage = opt->value;
|
||||
|
||||
BUG_ON_OPT_NEG(unset);
|
||||
|
||||
if (!strcmp(arg, "all")) {
|
||||
checkout_stage = CHECKOUT_ALL;
|
||||
*stage = CHECKOUT_ALL;
|
||||
} else {
|
||||
int ch = arg[0];
|
||||
if ('1' <= ch && ch <= '3')
|
||||
checkout_stage = arg[0] - '0';
|
||||
*stage = arg[0] - '0';
|
||||
else
|
||||
die(_("stage should be between 1 and 3 or all"));
|
||||
}
|
||||
@ -238,7 +240,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
|
||||
N_("write the content to temporary files")),
|
||||
OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
|
||||
N_("when creating files, prepend <string>")),
|
||||
OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
|
||||
OPT_CALLBACK_F(0, "stage", &checkout_stage, "(1|2|3|all)",
|
||||
N_("copy out the files from named stage"),
|
||||
PARSE_OPT_NONEG, option_parse_stage),
|
||||
OPT_END()
|
||||
|
Reference in New Issue
Block a user