[PATCH v2 5/7] argp: use "command-line option" context for option name translation
Vivien Kraus
vivien@planete-kraus.eu
Fri May 30 08:57:32 GMT 2025
A disambiguation context is useful, because option names are usually
single words, but the public API cannot let the caller override it
without introducing new fields.
---
argp/argp-help.c | 25 ++++++++++++++++++-------
argp/argp-parse.c | 1 +
manual/argp.texi | 14 ++++++++------
3 files changed, 27 insertions(+), 13 deletions(-)
diff --git a/argp/argp-help.c b/argp/argp-help.c
index 234e3c2deb..958258a152 100644
--- a/argp/argp-help.c
+++ b/argp/argp-help.c
@@ -1420,6 +1420,7 @@ usage_long_opt (const struct argp_option *opt,
{
argp_fmtstream_t stream = cookie;
const char *arg = opt->arg;
+ char *option_msgid;
const char *translated_opt_name = opt->name;
const char *name_candidates[3] = { 0 };
int translation_alias;
@@ -1430,15 +1431,25 @@ usage_long_opt (const struct argp_option *opt,
if (! (flags & OPTION_NO_USAGE))
{
- /* getopt does not recognize the domain, so the name will be
- translated in the current default textdomain. We want to
- document this string. */
- translated_opt_name = gettext (opt->name);
- name_candidates[0] = translated_opt_name;
- if (strcmp (translated_opt_name, opt->name) != 0)
+ /* Since we cannot customize the translation context, we will
+ use a default one. FIXME: use pgettext_expr(). */
+ static const char *default_context = "command-line option\004";
+ option_msgid = malloc (strlen (default_context) + strlen (opt->name) + 1);
+ if (option_msgid)
{
- name_candidates[1] = opt->name;
+ strcpy (option_msgid, default_context);
+ strcat (option_msgid, opt->name);
+ translated_opt_name = gettext (option_msgid);
+ free (option_msgid);
+ name_candidates[0] = translated_opt_name;
+ if (strcmp (translated_opt_name, opt->name) != 0)
+ {
+ name_candidates[1] = opt->name;
+ }
}
+ else
+ /* Do not try to translate. */
+ name_candidates[0] = opt->name;
for (translation_alias = 0;
name_candidates[translation_alias];
translation_alias++)
diff --git a/argp/argp-parse.c b/argp/argp-parse.c
index 82c7b784de..00fbf104db 100644
--- a/argp/argp-parse.c
+++ b/argp/argp-parse.c
@@ -472,6 +472,7 @@ parser_init (struct parser *parser, const struct argp *argp,
struct parser_sizes szs;
struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER;
+ opt_data.optctxt = "command-line option";
szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1;
szs.long_len = 0;
szs.num_groups = 0;
diff --git a/manual/argp.texi b/manual/argp.texi
index 6cedf96110..97456ef20e 100644
--- a/manual/argp.texi
+++ b/manual/argp.texi
@@ -208,7 +208,8 @@ messages. @xref{Argp Help Filtering}.
If non-zero, the strings used in the argp library are translated using
the domain described by this string. If zero, the current default
domain is used. The long option names are always translated with the
-current default domain.
+current default domain, and with the @samp{"command-line option"}
+disambiguation string.
@end table
@end deftp
@@ -250,11 +251,12 @@ the following fields:
@item const char *name
The long name for this option, corresponding to the long option
@samp{--@var{name}}; this field may be zero if this option @emph{only}
-has a short name. You should mark this string for translation without
-a context. To specify multiple names for an option, additional
-entries may follow this one, with the @code{OPTION_ALIAS} flag set.
-@xref{Argp Option Flags}. Translations are added automatically, it is
-not necessary to use an alias for translations.
+has a short name. You should mark this string for translation with
+the fixed @samp{"command-line option"} context. To specify multiple
+names for an option, additional entries may follow this one, with the
+@code{OPTION_ALIAS} flag set. @xref{Argp Option Flags}. Translations
+are added automatically, it is not necessary to use an alias for
+translations.
@item int key
The integer key provided by the current option to the option parser. If
--
2.49.0
More information about the Libc-alpha
mailing list