summaryrefslogtreecommitdiffabout
path: root/src
authorSergey Poznyakoff <gray@gnu.org.ua>2013-04-25 10:06:53 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2013-04-25 10:06:53 (GMT)
commit62fb1075edd466dc0b713ef8feeadacb1bad3177 (patch) (side-by-side diff)
tree8b2f429811ae77b67616f4a47c68841382b2aa51 /src
parent4dd512029108d5ee879de899e85ba4634f7b09d5 (diff)
downloadgrecs-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') (more/less context) (ignore whitespace changes)
-rw-r--r--src/grecsopt.h1
-rw-r--r--src/opthelp.c28
2 files changed, 27 insertions, 2 deletions
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);
}

Return to:

Send suggestions and report system problems to the System administrator.