/*
- * Copyright © 2021 Nick Bowler
+ * Copyright © 2021-2023 Nick Bowler
*
* Helper functions for formatting --help program output.
*
#endif
#if ENABLE_NLS
+# include <locale.h>
# include <gettext.h>
# include <mbswidth.h>
#else
*/
static int option_type(const struct option *opt)
{
- return ((opt->val <= CHAR_MAX) << 2) | (opt->has_arg & 3);
+ int ret = opt->has_arg & 3;
+
+ if (!opt->flag)
+ ret |= (opt->val <= CHAR_MAX) << 2;
+
+ return ret;
}
enum {
goto no_translate;
switch (option_type(opt)) {
+#if HELP_GETOPT_LONG_ONLY
+ case OPT_SHORT_WITH_OPTIONAL_ARG:
+ case OPT_LONG_WITH_OPTIONAL_ARG:
+ w = snprintf(optstring, sizeof optstring,
+ _(" -%s [%s]"), opt->name,
+ pgettext_expr(opt->name, argname));
+ break;
+ case OPT_SHORT_WITH_MANDATORY_ARG:
+ case OPT_LONG_WITH_MANDATORY_ARG:
+ w = snprintf(optstring, sizeof optstring,
+ _(" -%s %s"), opt->name,
+ pgettext_expr(opt->name, argname));
+ break;
+ case OPT_SHORT_WITHOUT_ARG:
+ case OPT_LONG_WITHOUT_ARG:
+ w = snprintf(optstring, sizeof optstring,
+ _(" -%s"), opt->name);
+ break;
+#else
case OPT_SHORT_WITH_OPTIONAL_ARG:
w = snprintf(optstring, sizeof optstring,
_(" -%c, --%s[=%s]"), opt->val, opt->name,
w = snprintf(optstring, sizeof optstring,
_(" --%s"), opt->name);
break;
+#endif
default:
assert(0);
}
no_translate:
switch (option_type(opt)) {
+#if HELP_GETOPT_LONG_ONLY
+ case OPT_SHORT_WITH_OPTIONAL_ARG:
+ case OPT_LONG_WITH_OPTIONAL_ARG:
+ w = printf(" -%s [%s]", opt->name, argname);
+ break;
+ case OPT_SHORT_WITH_MANDATORY_ARG:
+ case OPT_LONG_WITH_MANDATORY_ARG:
+ w = printf(" -%s %s", opt->name, argname);
+ break;
+ case OPT_SHORT_WITHOUT_ARG:
+ case OPT_LONG_WITHOUT_ARG:
+ w = printf(" -%s", opt->name);
+ break;
+#else
case OPT_SHORT_WITH_OPTIONAL_ARG:
w = printf(" -%c, --%s[=%s]", opt->val, opt->name, argname);
break;
case OPT_LONG_WITHOUT_ARG:
w = printf(" --%s", opt->name);
break;
+#endif
default:
assert(0);
}
void help_print_desc(const struct option *opt, const char *s, int i, int w)
{
- for (s = pgettext_expr(opt->name, s); *s; w = 0) {
+ if (opt)
+ s = pgettext_expr(opt->name, s);
+
+ if (i && s[0] == '\0')
+ putchar('\n');
+
+ for (; *s; w = 0) {
const char *nl = strchr(s, '\n');
int n = (nl ? nl-s : -1);