diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-04-25 13:06:53 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-04-25 13:06:53 +0300 |
commit | 62fb1075edd466dc0b713ef8feeadacb1bad3177 (patch) | |
tree | 8b2f429811ae77b67616f4a47c68841382b2aa51 /src/opthelp.c | |
parent | 4dd512029108d5ee879de899e85ba4634f7b09d5 (diff) | |
download | grecs-62fb1075edd466dc0b713ef8feeadacb1bad3177.tar.gz grecs-62fb1075edd466dc0b713ef8feeadacb1bad3177.tar.bz2 |
Add support for subcommand names.
This should simplify using grecs in programs that implement a subcommand
notion (a la git or svn, e.g. myprod doit -n *).
* build-aux/getopt.m4 (STDFUNC): Initialize the subcmd member of
struct grecs_proginfo.
(OPTIONS_COMMAND_BEGIN): New macro.
* src/grecsopt.h (grecs_proginfo) <subcmd>: New member.
* src/opthelp.c (grecs_print_help): if defined, print the subcommand
and its aliases.
Diffstat (limited to 'src/opthelp.c')
-rw-r--r-- | src/opthelp.c | 28 |
1 files changed, 26 insertions, 2 deletions
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); } |