[PATCH v2 2/7] argp: document both translated and untranslated names in --help

Vivien Kraus vivien@planete-kraus.eu
Fri May 30 08:31:28 GMT 2025


They are displayed in the --help output as if they were aliases.  If
the translated and untranslated forms are the same, it is not
repeated.  The translation is displayed first because it is more
useful to the reader.
---
 argp/argp-help.c | 40 ++++++++++++++++++++++++++++++----------
 manual/argp.texi | 20 +++++++++++++-------
 2 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/argp/argp-help.c b/argp/argp-help.c
index 3fe3a244ce..234e3c2deb 100644
--- a/argp/argp-help.c
+++ b/argp/argp-help.c
@@ -1420,6 +1420,9 @@ usage_long_opt (const struct argp_option *opt,
 {
   argp_fmtstream_t stream = cookie;
   const char *arg = opt->arg;
+  const char *translated_opt_name = opt->name;
+  const char *name_candidates[3] = { 0 };
+  int translation_alias;
   int flags = opt->flags | real->flags;
 
   if (! arg)
@@ -1427,16 +1430,33 @@ usage_long_opt (const struct argp_option *opt,
 
   if (! (flags & OPTION_NO_USAGE))
     {
-      if (arg)
-	{
-	  arg = dgettext (domain, arg);
-	  if (flags & OPTION_ARG_OPTIONAL)
-	    __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
-	  else
-	    __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
-	}
-      else
-	__argp_fmtstream_printf (stream, " [--%s]", opt->name);
+      /* 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)
+        {
+          name_candidates[1] = opt->name;
+        }
+      for (translation_alias = 0;
+           name_candidates[translation_alias];
+           translation_alias++)
+        {
+          if (arg)
+            {
+              arg = dgettext (domain, arg);
+              if (flags & OPTION_ARG_OPTIONAL)
+                __argp_fmtstream_printf (stream, " [--%s[=%s]]",
+                                         name_candidates[translation_alias], arg);
+              else
+                __argp_fmtstream_printf (stream, " [--%s=%s]",
+                                         name_candidates[translation_alias], arg);
+            }
+          else
+            __argp_fmtstream_printf (stream, " [--%s]",
+                                     name_candidates[translation_alias]);
+        }
     }
 
   return 0;
diff --git a/manual/argp.texi b/manual/argp.texi
index 0023441812..552a3006d3 100644
--- a/manual/argp.texi
+++ b/manual/argp.texi
@@ -206,8 +206,9 @@ messages.  @xref{Argp Help Filtering}.
 
 @item const char *argp_domain
 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 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.
 
 @end table
 @end deftp
@@ -233,7 +234,9 @@ beginning, the unused fields left unspecified.
 The @code{options} field in a @code{struct argp} points to a vector of
 @code{struct argp_option} structures, each of which specifies an option
 that the argp parser supports.  Multiple entries may be used for a single
-option provided it has multiple names.  This should be terminated by an
+option provided it has multiple names.  In any case, option names are
+translated, so either the translated or untranslated form is
+recognized for each option.  This should be terminated by an
 entry with zero in all fields.  Note that when using an initialized C
 array for options, writing @code{@{ 0 @}} is enough to achieve this.
 
@@ -247,9 +250,11 @@ 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.  To specify multiple names for an option, additional
-entries may follow this one, with the @code{OPTION_ALIAS} flag
-set.  @xref{Argp Option Flags}.
+has a short name.  You should mark this string for translation.  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
@@ -323,7 +328,8 @@ This option isn't displayed in any help messages.
 This option is an alias for the closest previous non-alias option.  This
 means that it will be displayed in the same help entry, and will inherit
 fields other than @code{name} and @code{key} from the option being
-aliased.
+aliased.  It is not necessary to list the translation of an option
+name as an alias.
 
 
 @item OPTION_DOC
-- 
2.49.0


More information about the Libc-alpha mailing list