diff options
-rw-r--r-- | build-aux/getopt.m4 | 24 | ||||
-rw-r--r-- | src/grecsopt.h | 1 | ||||
-rw-r--r-- | src/opthelp.c | 28 |
3 files changed, 47 insertions, 6 deletions
diff --git a/build-aux/getopt.m4 b/build-aux/getopt.m4 index afb6528..8a9b675 100644 --- a/build-aux/getopt.m4 +++ b/build-aux/getopt.m4 @@ -294,13 +294,21 @@ define([<GETOPT>],[< } >]) +define([<_getopt_sc_array>], + [<ifelse([<$1>],,,[<[<"$1", >]_getopt_sc_array(shift($@))>])>]) + define([<STDFUNC>],[< divert(5) - +ifelse([<$3>],,,[< +static char *subcommands[] = { + _getopt_sc_array($3) + NULL +};>]) static struct grecs_proginfo proginfo = { $2, /* progname */ - ifelse([<$3>],,"",N_("$3")), /* docstring */ - ifelse([<$4>],,"",N_("$4")), /* args_doc */ + ifelse([<$3>],,NULL,subcommands), /* subcommands */ + ifelse([<$4>],,"",N_("$4")), /* docstring */ + ifelse([<$5>],,"",N_("$5")), /* args_doc */ opthelp, /* opthelp */ sizeof(opthelp)/sizeof(opthelp[0]), /* optcount */ NULL, /* print_help_hook */ @@ -327,7 +335,15 @@ define([<OPTIONS_BEGIN>], _getopt_set_options(shift(shift(shift($@)))) _getopt_if_option_set([<gnu>], [<STDFUNC([<$1 " (" PACKAGE_NAME ") " PACKAGE_VERSION>], - [<$1>], [<$2>], [<$3>])>]) + [<$1>], [<>], [<$2>], [<$3>])>]) +>]) + +define([<OPTIONS_COMMAND_BEGIN>], + [<divert(-1) + _getopt_set_options(shift(shift(shift(shift($@))))) + _getopt_if_option_set([<gnu>], + [<STDFUNC([<$1 " (" PACKAGE_NAME ") " PACKAGE_VERSION>], + [<$1>], [<$2>], [<$3>], [<$4>])>]) >]) define([<OPTIONS_END>],[< diff --git a/src/grecsopt.h b/src/grecsopt.h index 2de9ea3..4c00e53 100644 --- a/src/grecsopt.h +++ b/src/grecsopt.h @@ -19,6 +19,7 @@ struct grecs_proginfo { const char *progname; + char **subcmd; const char *docstring; const char *args_doc; struct grecs_opthelp *opthelp; diff --git a/src/opthelp.c b/src/opthelp.c index 58deeed..75e48d2 100644 --- a/src/opthelp.c +++ b/src/opthelp.c @@ -118,9 +118,20 @@ grecs_print_help(struct grecs_proginfo *pinfo) struct grecs_opthelp *opthelp; size_t optcount; - printf("%s %s [%s]... %s\n", - _("Usage:"), pinfo->progname, _("OPTION"), + printf("%s %s ", + _("Usage:"), pinfo->progname); + if (pinfo->subcmd) + printf("%s ", pinfo->subcmd[0]); + printf("[%s]... %s\n", + _("OPTION"), !ISEMPTY(pinfo->args_doc) ? gettext(pinfo->args_doc) : ""); + if (pinfo->subcmd && pinfo->subcmd[1]) { + const char **p; + + printf("%s: ", pinfo->subcmd[2] ? _("Aliases") : _("Alias")); + for (p = pinfo->subcmd + 1; *p; p++) + printf("%s%c", *p, p[1] ? ' ' : '\n'); + } if (!ISEMPTY(pinfo->docstring)) print_option_descr(gettext(pinfo->docstring), 0, RMARGIN); putchar('\n'); @@ -255,6 +266,8 @@ grecs_print_usage(struct grecs_proginfo *pinfo) buf = grecs_malloc(bufsize); n = snprintf(buf, bufsize, "%s %s ", _("Usage:"), pinfo->progname); + if (pinfo->subcmd) + n += snprintf(buf + n, bufsize - n, "%s ", pinfo->subcmd[0]); /* Print a list of short options without arguments. */ for (i = nidx = 0; i < optcount; i++) @@ -368,6 +381,17 @@ grecs_print_usage(struct grecs_proginfo *pinfo) } #endif FLUSH; + + if (pinfo->subcmd && pinfo->subcmd[1]) { + const char **p; + + printf("%s: %s", pinfo->subcmd[2] ? _("Aliases") : _("Alias"), + pinfo->progname); + for (p = pinfo->subcmd + 1; *p; p++) + printf(" %s", *p); + putchar('\n'); + } + free(optidx); free(buf); } |