Make a new function (hoptusage()) to fabricate an uptodate usage: ...

message based on opts[] instead of relying on a hardcoded usage string.
This commit is contained in:
david parsons
2017-02-02 20:43:18 -08:00
parent 3e8386d071
commit 1c712469dd
6 changed files with 93 additions and 44 deletions
+3
View File
@@ -101,6 +101,9 @@ makepage: makepage.c pgm_options.o $(MKDLIB) mkdio.h
pgm_options.o: pgm_options.c mkdio.h config.h
$(CC) $(CFLAGS) -I. -c pgm_options.c
gethopt.o: gethopt.c
$(CC) $(CFLAGS) -I. -c gethopt.c
main.o: main.c mkdio.h config.h
$(CC) $(CFLAGS) -I. -c main.c
+45
View File
@@ -177,6 +177,51 @@ int nropts;
}
void
hoptusage(char *pgm, struct h_opt opts[], int nropts, char *arguments)
{
int i;
int optcount;
fprintf(stderr, "usage: %s", pgm);
/* print out the options that don't have flags first */
for ( optcount=i=0; i < nropts; i++ ) {
if ( opts[i].optchar && !opts[i].opthasarg) {
if (optcount == 0 )
fputs(" [-", stderr);
fputc(opts[i].optchar, stderr);
optcount++;
}
}
if ( optcount )
fputc(']', stderr);
/* print out the options WITH flags */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optchar && opts[i].opthasarg)
fprintf(stderr, " [-%c %s]", opts[i].optchar, opts[i].opthasarg);
/* print out the long options */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optword ) {
fprintf(stderr, " [-%s", opts[i].optword);
if ( opts[i].opthasarg )
fprintf(stderr, " %s", opts[i].opthasarg);
fputc(']', stderr);
}
/* print out the arguments string, if any */
if ( arguments )
fprintf(stderr, " %s", arguments);
/* and we're done */
fputc('\n', stderr);
}
#if DEBUG
struct h_opt opts[] = {
{ 0, "css", 0, 1, "css file" },
+3 -1
View File
@@ -14,7 +14,7 @@ struct h_opt {
int option;
char *optword;
char optchar;
int opthasarg;
char *opthasarg;
char *optdesc;
} ;
@@ -38,4 +38,6 @@ extern void hoptset(struct h_context *, int, char **);
extern int hopterr(struct h_context *, int);
extern struct h_opt *gethopt(struct h_context *, struct h_opt*, int);
extern void hoptusage(char *, struct h_opt*, int, char *);
#endif/*__GETHOPT_D*/
+16 -19
View File
@@ -61,21 +61,21 @@ complain(char *fmt, ...)
struct h_opt opts[] = {
{ 0, "html5", '5', 0, "recognise html5 block elements" },
{ 0, "base", 'b', 1, "URL prefix" },
{ 0, "debug", 'd', 0, "debugging" },
{ 0, "version",'V', 0, "show version info" },
{ 0, 0, 'E', 1, "url flags" },
{ 0, 0, 'F', 1, "set/show hex flags" },
{ 0, 0, 'f', 1, "set/show named flags" },
{ 0, 0, 'G', 0, "github flavoured markdown" },
{ 0, 0, 'n', 0, "don't write generated html" },
{ 0, 0, 's', 1, "format a string" },
{ 0, "style", 'S', 0, "output <style> blocks" },
{ 0, 0, 't', 1, "format a string with mkd_line()" },
{ 0, "toc", 'T', 0, "output a TOC" },
{ 0, 0, 'C', 1, "prefix for markdown extra footnotes" },
{ 0, 0, 'o', 1, "write output to a file" },
{ 0, "html5", '5', 0, "recognise html5 block elements" },
{ 0, "base", 'b', "url-base", "URL prefix" },
{ 0, "debug", 'd', 0, "debugging" },
{ 0, "version",'V', 0, "show version info" },
{ 0, 0, 'E', "flags", "url flags" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, 0, 'f', "{+-}flags", "set/show named flags" },
{ 0, 0, 'G', 0, "github flavoured markdown" },
{ 0, 0, 'n', 0, "don't write generated html" },
{ 0, 0, 's', "text", "format `text`" },
{ 0, "style", 'S', 0, "output <style> blocks" },
{ 0, 0, 't', "text", "format `text` with mkd_line()" },
{ 0, "toc", 'T', 0, "output a TOC" },
{ 0, 0, 'C', "prefix", "prefix for markdown extra footnotes" },
{ 0, 0, 'o', "file", "write output to file" },
};
#define NROPTS (sizeof opts/sizeof opts[0])
@@ -112,10 +112,7 @@ main(int argc, char **argv)
while ( opt=gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
" [-F bitmap] [-f {+-}flags]"
" [-o ofile] [-s text]"
" [-t text] [file]\n", pgm);
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch (opt->optchar) {
+4 -5
View File
@@ -23,9 +23,9 @@ basename(char *p)
char *pgm = "makepage";
struct h_opt opts[] = {
{ 0, "version", 'V', 0, "show version info" },
{ 0, 0, 'F', 1, "set/show hex flags" },
{ 0, "flags", 'f', 1, "set/show named flags" },
{ 0, "version", 'V', 0, "show version info" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, "flags", 'f', "{+-}flags", "set/show named flags" },
} ;
#define NROPTS (sizeof opts / sizeof opts[0])
@@ -49,8 +49,7 @@ char **argv;
while ( opt = gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
fprintf(stderr, "usage: %s [-V] [-F bitmap] [-f {+-}flags]"
" [file]\n", pgm);
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch ( opt->optchar ) {
+22 -19
View File
@@ -70,9 +70,9 @@ fail(char *why, ...)
enum { ADD_CSS, ADD_HEADER, ADD_FOOTER };
struct h_opt opts[] = {
{ ADD_CSS, "css", 0, 1, "Additional stylesheets for this page" },
{ ADD_HEADER, "header", 0, 1, "Additonal headers for this page" },
{ ADD_FOOTER, "footer", 0, 1, "Additional footers for this page" },
{ ADD_CSS, "css", 0, "url", "Additional css for this page" },
{ ADD_HEADER, "header", 0, "header", "Additonal headers for this page" },
{ ADD_FOOTER, "footer", 0, "footer", "Additional footers for this page" },
};
#define NROPTS (sizeof opts/sizeof opts[0])
@@ -102,21 +102,24 @@ char **argv;
hoptset(&flags, argc, argv);
hopterr(&flags, 1);
while ( res = gethopt(&flags, opts, NROPTS) ) {
if ( res != HOPTERR ) {
switch ( res->option ) {
case ADD_CSS:
EXPAND(css) = hoptarg(&flags);
break;
case ADD_HEADER:
EXPAND(headers) = hoptarg(&flags);
break;
case ADD_FOOTER:
EXPAND(footers) = hoptarg(&flags);
break;
default:
fprintf(stderr, "unknown option?\n");
break;
}
if ( res == HOPTERR ) {
hoptusage(argv[0], opts, NROPTS, "source [dest]");
exit(1);
}
switch ( res->option ) {
case ADD_CSS:
EXPAND(css) = hoptarg(&flags);
break;
case ADD_HEADER:
EXPAND(headers) = hoptarg(&flags);
break;
case ADD_FOOTER:
EXPAND(footers) = hoptarg(&flags);
break;
default:
fprintf(stderr, "unknown option?\n");
break;
}
}
@@ -160,7 +163,7 @@ char **argv;
break;
default:
fprintf(stderr, "usage: %s [opts] source [dest]\n", pgm);
hoptusage(argv[0], opts, NROPTS, "source [dest]");
exit(1);
}