aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-04-25 13:06:53 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-04-25 13:06:53 +0300
commit62fb1075edd466dc0b713ef8feeadacb1bad3177 (patch)
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')
-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 @@
19 19
20struct grecs_proginfo { 20struct grecs_proginfo {
21 const char *progname; 21 const char *progname;
22 char **subcmd;
22 const char *docstring; 23 const char *docstring;
23 const char *args_doc; 24 const char *args_doc;
24 struct grecs_opthelp *opthelp; 25 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)
118 struct grecs_opthelp *opthelp; 118 struct grecs_opthelp *opthelp;
119 size_t optcount; 119 size_t optcount;
120 120
121 printf("%s %s [%s]... %s\n", 121 printf("%s %s ",
122 _("Usage:"), pinfo->progname, _("OPTION"), 122 _("Usage:"), pinfo->progname);
123 if (pinfo->subcmd)
124 printf("%s ", pinfo->subcmd[0]);
125 printf("[%s]... %s\n",
126 _("OPTION"),
123 !ISEMPTY(pinfo->args_doc) ? gettext(pinfo->args_doc) : ""); 127 !ISEMPTY(pinfo->args_doc) ? gettext(pinfo->args_doc) : "");
128 if (pinfo->subcmd && pinfo->subcmd[1]) {
129 const char **p;
130
131 printf("%s: ", pinfo->subcmd[2] ? _("Aliases") : _("Alias"));
132 for (p = pinfo->subcmd + 1; *p; p++)
133 printf("%s%c", *p, p[1] ? ' ' : '\n');
134 }
124 if (!ISEMPTY(pinfo->docstring)) 135 if (!ISEMPTY(pinfo->docstring))
125 print_option_descr(gettext(pinfo->docstring), 0, RMARGIN); 136 print_option_descr(gettext(pinfo->docstring), 0, RMARGIN);
126 putchar('\n'); 137 putchar('\n');
@@ -255,6 +266,8 @@ grecs_print_usage(struct grecs_proginfo *pinfo)
255 buf = grecs_malloc(bufsize); 266 buf = grecs_malloc(bufsize);
256 267
257 n = snprintf(buf, bufsize, "%s %s ", _("Usage:"), pinfo->progname); 268 n = snprintf(buf, bufsize, "%s %s ", _("Usage:"), pinfo->progname);
269 if (pinfo->subcmd)
270 n += snprintf(buf + n, bufsize - n, "%s ", pinfo->subcmd[0]);
258 271
259 /* Print a list of short options without arguments. */ 272 /* Print a list of short options without arguments. */
260 for (i = nidx = 0; i < optcount; i++) 273 for (i = nidx = 0; i < optcount; i++)
@@ -368,6 +381,17 @@ grecs_print_usage(struct grecs_proginfo *pinfo)
368 } 381 }
369#endif 382#endif
370 FLUSH; 383 FLUSH;
384
385 if (pinfo->subcmd && pinfo->subcmd[1]) {
386 const char **p;
387
388 printf("%s: %s", pinfo->subcmd[2] ? _("Aliases") : _("Alias"),
389 pinfo->progname);
390 for (p = pinfo->subcmd + 1; *p; p++)
391 printf(" %s", *p);
392 putchar('\n');
393 }
394
371 free(optidx); 395 free(optidx);
372 free(buf); 396 free(buf);
373} 397}

Return to:

Send suggestions and report system problems to the System administrator.