From 62fb1075edd466dc0b713ef8feeadacb1bad3177 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 25 Apr 2013 13:06:53 +0300 Subject: 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) : New member. * src/opthelp.c (grecs_print_help): if defined, print the subcommand and its aliases. --- src/grecsopt.h | 1 + src/opthelp.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src') 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); } -- cgit v1.2.1