Skip to content

Commit 9440b83

Browse files
pcloudsgitster
authored andcommitted
parse-options: replace opterror() with optname()
Introduce optname() that does the early half of original opterror() to come up with the name of the option reported back to the user, and use it to kill opterror(). The callers of opterror() now directly call error() using the string returned by opterror() instead. There are a few issues with opterror() - it tries to assemble an English sentence from pieces. This is not great for translators because we give them pieces instead of a full sentence. - It's a wrapper around error() and needs some hack to let the compiler know it always returns -1. - Since it takes a string instead of printf format, one call site has to assemble the string manually before passing to it. Using error() directly solves the second and third problems. It kind helps the first problem as well because "%s does foo" does give a translator a full sentence in a sense and let them reorder if needed. But it has limitations, if the subject part has to change based on the rest of the sentence, that language is screwed. This is also why I try to avoid calling optname() when 'flags' is known in advance. Mark of these strings for translation as well while at there. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c83d950 commit 9440b83

File tree

7 files changed

+40
-33
lines changed

7 files changed

+40
-33
lines changed

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int option_read_message(struct parse_opt_ctx_t *ctx,
128128
ctx->argc--;
129129
arg = *++ctx->argv;
130130
} else
131-
return opterror(opt, "requires a value", 0);
131+
return error(_("option `%s' requires a value"), opt->long_name);
132132

133133
if (buf->len)
134134
strbuf_addch(buf, '\n');

builtin/revert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ static int option_parse_m(const struct option *opt,
6969

7070
replay->mainline = strtol(arg, &end, 10);
7171
if (*end || replay->mainline <= 0)
72-
return opterror(opt, "expects a number greater than zero", 0);
72+
return error(_("option `%s' expects a number greater than zero"),
73+
opt->long_name);
7374

7475
return 0;
7576
}

parse-options-cb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
1818
} else {
1919
v = strtol(arg, (char **)&arg, 10);
2020
if (*arg)
21-
return opterror(opt, "expects a numerical value", 0);
21+
return error(_("option `%s' expects a numerical value"),
22+
opt->long_name);
2223
if (v && v < MINIMUM_ABBREV)
2324
v = MINIMUM_ABBREV;
2425
else if (v > 40)
@@ -54,8 +55,8 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
5455
arg = unset ? "never" : (const char *)opt->defval;
5556
value = git_config_colorbool(NULL, arg);
5657
if (value < 0)
57-
return opterror(opt,
58-
"expects \"always\", \"auto\", or \"never\"", 0);
58+
return error(_("option `%s' expects \"always\", \"auto\", or \"never\""),
59+
opt->long_name);
5960
*(int *)opt->value = value;
6061
return 0;
6162
}

parse-options.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
3232
p->argc--;
3333
*arg = *++p->argv;
3434
} else
35-
return opterror(opt, "requires a value", flags);
35+
return error(_("%s requires a value"), optname(opt, flags));
3636
return 0;
3737
}
3838

@@ -49,7 +49,6 @@ static int opt_command_mode_error(const struct option *opt,
4949
int flags)
5050
{
5151
const struct option *that;
52-
struct strbuf message = STRBUF_INIT;
5352
struct strbuf that_name = STRBUF_INIT;
5453

5554
/*
@@ -67,13 +66,13 @@ static int opt_command_mode_error(const struct option *opt,
6766
strbuf_addf(&that_name, "--%s", that->long_name);
6867
else
6968
strbuf_addf(&that_name, "-%c", that->short_name);
70-
strbuf_addf(&message, ": incompatible with %s", that_name.buf);
69+
error(_("%s is incompatible with %s"),
70+
optname(opt, flags), that_name.buf);
7171
strbuf_release(&that_name);
72-
opterror(opt, message.buf, flags);
73-
strbuf_release(&message);
7472
return -1;
7573
}
76-
return opterror(opt, ": incompatible with something else", flags);
74+
return error(_("%s : incompatible with something else"),
75+
optname(opt, flags));
7776
}
7877

7978
static int get_value(struct parse_opt_ctx_t *p,
@@ -86,11 +85,11 @@ static int get_value(struct parse_opt_ctx_t *p,
8685
int err;
8786

8887
if (unset && p->opt)
89-
return opterror(opt, "takes no value", flags);
88+
return error(_("%s takes no value"), optname(opt, flags));
9089
if (unset && (opt->flags & PARSE_OPT_NONEG))
91-
return opterror(opt, "isn't available", flags);
90+
return error(_("%s isn't available"), optname(opt, flags));
9291
if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
93-
return opterror(opt, "takes no value", flags);
92+
return error(_("%s takes no value"), optname(opt, flags));
9493

9594
switch (opt->type) {
9695
case OPTION_LOWLEVEL_CALLBACK:
@@ -176,7 +175,8 @@ static int get_value(struct parse_opt_ctx_t *p,
176175
return -1;
177176
*(int *)opt->value = strtol(arg, (char **)&s, 10);
178177
if (*s)
179-
return opterror(opt, "expects a numerical value", flags);
178+
return error(_("%s expects a numerical value"),
179+
optname(opt, flags));
180180
return 0;
181181

182182
case OPTION_MAGNITUDE:
@@ -191,9 +191,9 @@ static int get_value(struct parse_opt_ctx_t *p,
191191
if (get_arg(p, opt, flags, &arg))
192192
return -1;
193193
if (!git_parse_ulong(arg, opt->value))
194-
return opterror(opt,
195-
"expects a non-negative integer value with an optional k/m/g suffix",
196-
flags);
194+
return error(_("%s expects a non-negative integer value"
195+
" with an optional k/m/g suffix"),
196+
optname(opt, flags));
197197
return 0;
198198

199199
default:
@@ -257,7 +257,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
257257
if (!rest)
258258
continue;
259259
if (*rest == '=')
260-
return opterror(options, "takes no value", flags);
260+
return error(_("%s takes no value"),
261+
optname(options, flags));
261262
if (*rest)
262263
continue;
263264
p->out[p->cpidx++] = arg - 2;
@@ -773,12 +774,17 @@ void NORETURN usage_msg_opt(const char *msg,
773774
usage_with_options(usagestr, options);
774775
}
775776

776-
#undef opterror
777-
int opterror(const struct option *opt, const char *reason, int flags)
777+
const char *optname(const struct option *opt, int flags)
778778
{
779+
static struct strbuf sb = STRBUF_INIT;
780+
781+
strbuf_reset(&sb);
779782
if (flags & OPT_SHORT)
780-
return error("switch `%c' %s", opt->short_name, reason);
781-
if (flags & OPT_UNSET)
782-
return error("option `no-%s' %s", opt->long_name, reason);
783-
return error("option `%s' %s", opt->long_name, reason);
783+
strbuf_addf(&sb, "switch `%c'", opt->short_name);
784+
else if (flags & OPT_UNSET)
785+
strbuf_addf(&sb, "option `no-%s'", opt->long_name);
786+
else
787+
strbuf_addf(&sb, "option `%s'", opt->long_name);
788+
789+
return sb.buf;
784790
}

parse-options.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ extern NORETURN void usage_msg_opt(const char *msg,
189189
const struct option *options);
190190

191191
extern int optbug(const struct option *opt, const char *reason);
192-
extern int opterror(const struct option *opt, const char *reason, int flags);
193-
#if defined(__GNUC__)
194-
#define opterror(o,r,f) (opterror((o),(r),(f)), const_error())
195-
#endif
192+
const char *optname(const struct option *opt, int flags);
196193

197194
/*----- incremental advanced APIs -----*/
198195

ref-filter.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,9 +2302,11 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
23022302

23032303
if (rf->merge) {
23042304
if (no_merged) {
2305-
return opterror(opt, "is incompatible with --merged", 0);
2305+
return error(_("option `%s' is incompatible with --merged"),
2306+
opt->long_name);
23062307
} else {
2307-
return opterror(opt, "is incompatible with --no-merged", 0);
2308+
return error(_("option `%s' is incompatible with --no-merged"),
2309+
opt->long_name);
23082310
}
23092311
}
23102312

@@ -2318,7 +2320,7 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
23182320
rf->merge_commit = lookup_commit_reference_gently(the_repository,
23192321
&oid, 0);
23202322
if (!rf->merge_commit)
2321-
return opterror(opt, "must point to a commit", 0);
2323+
return error(_("option `%s' must point to a commit"), opt->long_name);
23222324

23232325
return 0;
23242326
}

t/t4211-line-log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ canned_test_failure () {
2525
test_bad_opts () {
2626
test_expect_success "invalid args: $1" "
2727
test_must_fail git log $1 2>errors &&
28-
grep '$2' errors
28+
test_i18ngrep '$2' errors
2929
"
3030
}
3131

0 commit comments

Comments
 (0)