From 9440b831ad5b1750b65fa3f1d0b99179ab317c76 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 10 Nov 2018 06:16:11 +0100 Subject: parse-options: replace opterror() with optname() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- parse-options.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 3b874a83a0..0bf817193d 100644 --- a/parse-options.c +++ b/parse-options.c @@ -32,7 +32,7 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, p->argc--; *arg = *++p->argv; } else - return opterror(opt, "requires a value", flags); + return error(_("%s requires a value"), optname(opt, flags)); return 0; } @@ -49,7 +49,6 @@ static int opt_command_mode_error(const struct option *opt, int flags) { const struct option *that; - struct strbuf message = STRBUF_INIT; struct strbuf that_name = STRBUF_INIT; /* @@ -67,13 +66,13 @@ static int opt_command_mode_error(const struct option *opt, strbuf_addf(&that_name, "--%s", that->long_name); else strbuf_addf(&that_name, "-%c", that->short_name); - strbuf_addf(&message, ": incompatible with %s", that_name.buf); + error(_("%s is incompatible with %s"), + optname(opt, flags), that_name.buf); strbuf_release(&that_name); - opterror(opt, message.buf, flags); - strbuf_release(&message); return -1; } - return opterror(opt, ": incompatible with something else", flags); + return error(_("%s : incompatible with something else"), + optname(opt, flags)); } static int get_value(struct parse_opt_ctx_t *p, @@ -86,11 +85,11 @@ static int get_value(struct parse_opt_ctx_t *p, int err; if (unset && p->opt) - return opterror(opt, "takes no value", flags); + return error(_("%s takes no value"), optname(opt, flags)); if (unset && (opt->flags & PARSE_OPT_NONEG)) - return opterror(opt, "isn't available", flags); + return error(_("%s isn't available"), optname(opt, flags)); if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) - return opterror(opt, "takes no value", flags); + return error(_("%s takes no value"), optname(opt, flags)); switch (opt->type) { case OPTION_LOWLEVEL_CALLBACK: @@ -176,7 +175,8 @@ static int get_value(struct parse_opt_ctx_t *p, return -1; *(int *)opt->value = strtol(arg, (char **)&s, 10); if (*s) - return opterror(opt, "expects a numerical value", flags); + return error(_("%s expects a numerical value"), + optname(opt, flags)); return 0; case OPTION_MAGNITUDE: @@ -191,9 +191,9 @@ static int get_value(struct parse_opt_ctx_t *p, if (get_arg(p, opt, flags, &arg)) return -1; if (!git_parse_ulong(arg, opt->value)) - return opterror(opt, - "expects a non-negative integer value with an optional k/m/g suffix", - flags); + return error(_("%s expects a non-negative integer value" + " with an optional k/m/g suffix"), + optname(opt, flags)); return 0; default: @@ -257,7 +257,8 @@ again: if (!rest) continue; if (*rest == '=') - return opterror(options, "takes no value", flags); + return error(_("%s takes no value"), + optname(options, flags)); if (*rest) continue; p->out[p->cpidx++] = arg - 2; @@ -773,12 +774,17 @@ void NORETURN usage_msg_opt(const char *msg, usage_with_options(usagestr, options); } -#undef opterror -int opterror(const struct option *opt, const char *reason, int flags) +const char *optname(const struct option *opt, int flags) { + static struct strbuf sb = STRBUF_INIT; + + strbuf_reset(&sb); if (flags & OPT_SHORT) - return error("switch `%c' %s", opt->short_name, reason); - if (flags & OPT_UNSET) - return error("option `no-%s' %s", opt->long_name, reason); - return error("option `%s' %s", opt->long_name, reason); + strbuf_addf(&sb, "switch `%c'", opt->short_name); + else if (flags & OPT_UNSET) + strbuf_addf(&sb, "option `no-%s'", opt->long_name); + else + strbuf_addf(&sb, "option `%s'", opt->long_name); + + return sb.buf; } -- cgit 1.2.3-korg From 48a5499ef50d4d49ac78e1711535bebf3d5b3cfb Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 10 Nov 2018 06:16:12 +0100 Subject: parse-options.c: turn some die() to BUG() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These two strings are clearly not for the user to see. Reduce the violence in one string while at there. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- parse-options.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 0bf817193d..3f5f985c1e 100644 --- a/parse-options.c +++ b/parse-options.c @@ -197,7 +197,7 @@ static int get_value(struct parse_opt_ctx_t *p, return 0; default: - die("should not happen, someone must be hit on the forehead"); + BUG("opt->type %d should not happen", opt->type); } } @@ -424,7 +424,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx, ctx->flags = flags; if ((flags & PARSE_OPT_KEEP_UNKNOWN) && (flags & PARSE_OPT_STOP_AT_NON_OPTION)) - die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); + BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); parse_options_check(options); } -- cgit 1.2.3-korg From 8900342628c1c0c52dff62edaa2ee73d82287f2d Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 10 Nov 2018 06:16:13 +0100 Subject: parse-options.c: mark more strings for translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One error is updated to start with lowercase to be consistent with the rest. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- parse-options.c | 14 +++++++------- t/t0040-parse-options.sh | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 3f5f985c1e..86aad2e185 100644 --- a/parse-options.c +++ b/parse-options.c @@ -319,8 +319,8 @@ is_abbreviated: } if (ambiguous_option) { - error("Ambiguous option: %s " - "(could be --%s%s or --%s%s)", + error(_("ambiguous option: %s " + "(could be --%s%s or --%s%s)"), arg, (ambiguous_flags & OPT_UNSET) ? "no-" : "", ambiguous_option->long_name, @@ -353,7 +353,7 @@ static void check_typos(const char *arg, const struct option *options) return; if (starts_with(arg, "no-")) { - error ("did you mean `--%s` (with two dashes ?)", arg); + error(_("did you mean `--%s` (with two dashes ?)"), arg); exit(129); } @@ -361,7 +361,7 @@ static void check_typos(const char *arg, const struct option *options) if (!options->long_name) continue; if (starts_with(options->long_name, arg)) { - error ("did you mean `--%s` (with two dashes ?)", arg); + error(_("did you mean `--%s` (with two dashes ?)"), arg); exit(129); } } @@ -644,11 +644,11 @@ int parse_options(int argc, const char **argv, const char *prefix, break; default: /* PARSE_OPT_UNKNOWN */ if (ctx.argv[0][1] == '-') { - error("unknown option `%s'", ctx.argv[0] + 2); + error(_("unknown option `%s'"), ctx.argv[0] + 2); } else if (isascii(*ctx.opt)) { - error("unknown switch `%c'", *ctx.opt); + error(_("unknown switch `%c'"), *ctx.opt); } else { - error("unknown non-ascii option in string: `%s'", + error(_("unknown non-ascii option in string: `%s'"), ctx.argv[0]); } usage_with_options(usagestr, options); diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index 17d0c18feb..e46b1e02f0 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -228,7 +228,7 @@ EOF test_expect_success 'detect possible typos' ' test_must_fail test-tool parse-options -boolean >output 2>output.err && test_must_be_empty output && - test_cmp typo.err output.err + test_i18ncmp typo.err output.err ' cat >typo.err <<\EOF @@ -238,7 +238,7 @@ EOF test_expect_success 'detect possible typos' ' test_must_fail test-tool parse-options -ambiguous >output 2>output.err && test_must_be_empty output && - test_cmp typo.err output.err + test_i18ncmp typo.err output.err ' test_expect_success 'keep some options as arguments' ' -- cgit 1.2.3-korg