From 0567de2636ff0ed0a9dc378cdbd82e0bb7dce788 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 29 Sep 2021 21:58:46 -0400 Subject: [PATCH] help: Add a single-dash long option mode. For programs using getopt_long_only with single-dash long options, add a compile time flag to select a mode that produces more appropriate help output. This is typical of command-line options for X applications. --- Makefile.am | 9 ++++++++- src/help.c | 35 +++++++++++++++++++++++++++++++++++ t/.gitignore | 1 + tests/functions.at | 29 +++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index a9f7934..3f976b1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,10 +18,17 @@ t_packtestu64_SOURCES = t/packtestu64.c src/pack.c src/tap.c t_packtests64_SOURCES = t/packtests64.c src/pack.c src/tap.c if HAVE_STRUCT_OPTION -check_PROGRAMS += t/helpdesc t/helpopt +check_PROGRAMS += t/helpdesc t/helpopt t/helpopt2 endif +EXTRA_LIBRARIES = libglohelp.a +libglohelp_a_SOURCES = src/help.c +libglohelp_a_CFLAGS = -DHELP_GETOPT_LONG_ONLY +libglohelp_a_SHORTNAME = glo + t_helpopt_SOURCES = t/helpopt.c src/help.c src/tap.c +t_helpopt2_SOURCES = t/helpopt.c src/tap.c +t_helpopt2_LDADD = $(libglohelp_a_OBJECTS) t_helpdesc_SOURCES = t/helpdesc.c src/help.c src/tap.c DISTCLEANFILES = diff --git a/src/help.c b/src/help.c index 0ceb8f4..d1dd5f5 100644 --- a/src/help.c +++ b/src/help.c @@ -71,6 +71,25 @@ int help_print_optstring(const struct option *opt, const char *argname, int l) 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, @@ -99,6 +118,7 @@ int help_print_optstring(const struct option *opt, const char *argname, int l) w = snprintf(optstring, sizeof optstring, _(" --%s"), opt->name); break; +#endif default: assert(0); } @@ -112,6 +132,20 @@ int help_print_optstring(const struct option *opt, const char *argname, int l) 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; @@ -130,6 +164,7 @@ no_translate: case OPT_LONG_WITHOUT_ARG: w = printf(" --%s", opt->name); break; +#endif default: assert(0); } diff --git a/t/.gitignore b/t/.gitignore index 7fee3da..acb4e4e 100644 --- a/t/.gitignore +++ b/t/.gitignore @@ -1,4 +1,5 @@ /helpdesc /helpopt +/helpopt2 /packtest[su] /packtest[su]64 diff --git a/tests/functions.at b/tests/functions.at index 507c105..dbbcf6d 100644 --- a/tests/functions.at +++ b/tests/functions.at @@ -80,3 +80,32 @@ AT_CHECK([m4_join([ ], ]]) AT_CLEANUP + +AT_SETUP([help_print_optstring (getopt_long_only)]) + +AT_SKIP_IF([test ! -x "$builddir/t/helpopt2"]) + +AT_CHECK([m4_join([ ], + ["$builddir/t/helpopt2"], + [--foo], + [--bar -b], + [--baz ARG], + [--baz -B ARG], + [--quux '@<:@ARG@:>@'], + [--quux -q '@<:@ARG@:>@'], + [--hello-this-is-a-very-long-option 20], + [--hello-this-is-a-very-long-option 50], + [--not-long 12])], [0], +[[ -foo 6 + -bar 6 + -baz ARG 10 + -baz ARG 10 + -quux [ARG] 13 + -quux [ARG] 13 + -hello-this-is-a-very-long-option + 0 + -hello-this-is-a-very-long-option 35 + -not-long 11 +]]) + +AT_CLEANUP -- 2.43.2