print usage prettier (with an apparent off-by-one error)

This commit is contained in:
Eric House 2014-10-01 19:52:07 -07:00
parent b82383a5ad
commit 3534bc0e49

View file

@ -804,15 +804,31 @@ make_longopts()
static void static void
usage( char* appName, char* msg ) usage( char* appName, char* msg )
{ {
const char* param = "<param>";
int ii; int ii;
if ( msg != NULL ) { if ( msg != NULL ) {
fprintf( stderr, "Error: %s\n\n", msg ); fprintf( stderr, "Error: %s\n\n", msg );
} }
fprintf( stderr, "usage: %s \n", appName ); fprintf( stderr, "usage: %s \n", appName );
int maxWidth = 0;
for ( ii = 0; ii < VSIZE(CmdInfoRecs); ++ii ) { for ( ii = 0; ii < VSIZE(CmdInfoRecs); ++ii ) {
const CmdInfoRec* rec = &CmdInfoRecs[ii]; const CmdInfoRec* rec = &CmdInfoRecs[ii];
fprintf( stderr, " --%s %-20s # %s\n", rec->param, int width = strlen(rec->param) + 1;
rec->hasArg? "<param>" : "", rec->hint ); if ( rec->hasArg ) {
width += strlen(param) + 1;
}
if ( width > maxWidth ) {
maxWidth = width;
}
}
for ( ii = 0; ii < VSIZE(CmdInfoRecs); ++ii ) {
const CmdInfoRec* rec = &CmdInfoRecs[ii];
char buf[120];
snprintf( buf, sizeof(buf), "--%s %s", rec->param,
(rec->hasArg ? param : "") );
fprintf( stderr, " %-*s # %s\n", maxWidth, buf, rec->hint );
} }
fprintf( stderr, "\n(revision: %s)\n", SVN_REV); fprintf( stderr, "\n(revision: %s)\n", SVN_REV);
exit(1); exit(1);