+/*
+ * Print the help description of a particular command-line option, identified
+ * by its short option name.
+ */
+static void print_option(int val)
+{
+ const struct option *tmp[2];
+ const struct helptext *help;
+ const char *argname;
+ char context[32];
+ size_t rc, n = 0;
+ int w;
+
+ /* Find the options and help text corresponding to val. */
+ for (const struct option *o = lopts; o->val; o++) {
+ if (o->val == val) {
+ assert(n < sizeof tmp / sizeof tmp[0]);
+ tmp[n++] = o;
+ }
+ }
+
+ for (help = helptext; help->val; help++) {
+ if (help->val == val)
+ break;
+ }
+
+ /* Prepare translations. */
+ if (!ENABLE_NLS) {
+ argname = help->argname;
+ } else if (n > 0) {
+ rc = snprintf(context, sizeof context, "longopt%c%s",
+ help->text ? '|' : '\0', tmp[0]->name);
+ assert(rc < sizeof context);
+
+ if (help->argname)
+ argname = pgettext_expr(context, help->argname);
+ }
+
+ switch (n) {
+ case 0:
+ w = printf(_(" -%c"), val);
+ break;
+ case 1:
+ w = printf(tmp[0]->has_arg ? _(" -%c, --%s=%s")
+ : _(" -%c, --%s"),
+ val, tmp[0]->name, argname);
+ break;
+ case 2:
+ if (tmp[0]->has_arg) {
+ w = printf(_(" -%c, --%s=%s, --%s=%s"), val,
+ tmp[0]->name, argname,
+ tmp[1]->name, argname);
+ } else {
+ w = printf(_(" -%c, --%s, --%s"), val,
+ tmp[0]->name, tmp[1]->name);
+ }
+ break;
+ default:
+ assert(0);
+ }
+
+ if (!help->text) {
+ putchar('\n');
+ return;
+ }
+
+ if (w > 18) {
+ putchar('\n');
+ w = 0;
+ }
+
+ for (const char *line = gettext(help->text); *line;) {
+ const char *nl = strchr(line, '\n');
+
+ if (!nl) {
+ printf("%*s%s\n", 20-w, "", line);
+ break;
+ }
+
+ printf("%*s%.*s\n", 20-w, "", (int)(nl-line), line);
+ line = nl+1;
+ }
+}
+