Equip the programs installed by contrib with proper --help and --version

options and normally formatted help output.
This commit is contained in:
Peter Eisentraut 2009-02-27 09:30:21 +00:00
parent 867a2a6d81
commit b6e4c8f255
5 changed files with 195 additions and 86 deletions

View File

@ -5,7 +5,7 @@
* Originally by * Originally by
* B. Palmer, bpalmer@crimelabs.net 1-17-2001 * B. Palmer, bpalmer@crimelabs.net 1-17-2001
* *
* $PostgreSQL: pgsql/contrib/oid2name/oid2name.c,v 1.34 2009/02/25 13:24:40 petere Exp $ * $PostgreSQL: pgsql/contrib/oid2name/oid2name.c,v 1.35 2009/02/27 09:30:21 petere Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
@ -47,6 +47,7 @@ struct options
}; };
/* function prototypes */ /* function prototypes */
static void help(const char *progname);
void get_opts(int, char **, struct options *); void get_opts(int, char **, struct options *);
void *myalloc(size_t size); void *myalloc(size_t size);
char *mystrdup(const char *str); char *mystrdup(const char *str);
@ -64,6 +65,9 @@ void
get_opts(int argc, char **argv, struct options * my_opts) get_opts(int argc, char **argv, struct options * my_opts)
{ {
int c; int c;
const char *progname;
progname = get_progname(argv[0]);
/* set the defaults */ /* set the defaults */
my_opts->quiet = false; my_opts->quiet = false;
@ -77,8 +81,22 @@ get_opts(int argc, char **argv, struct options * my_opts)
my_opts->port = NULL; my_opts->port = NULL;
my_opts->username = NULL; my_opts->username = NULL;
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
help(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("oid2name (PostgreSQL) " PG_VERSION);
exit(0);
}
}
/* get opts */ /* get opts */
while ((c = getopt(argc, argv, "H:p:U:d:t:o:f:qSxish?")) != -1) while ((c = getopt(argc, argv, "H:p:U:d:t:o:f:qSxish")) != -1)
{ {
switch (c) switch (c)
{ {
@ -142,29 +160,42 @@ get_opts(int argc, char **argv, struct options * my_opts)
my_opts->tablespaces = true; my_opts->tablespaces = true;
break; break;
/* help! (ugly in code for easier editing) */
case '?':
case 'h': case 'h':
fprintf(stderr, help(progname);
"Usage: oid2name [-s|-d database] [-S][-i][-q][-x] [-t table|-o oid|-f file] ...\n" exit(0);
" default action show all database Oids\n" break;
" -d database database to connect to\n"
default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
}
}
static void
help(const char *progname)
{
printf("%s helps examining the file structure used by PostgreSQL.\n\n"
"Usage:\n"
" %s [OPTIONS]...\n"
"\nOptions:\n"
" -d DBNAME database to connect to\n"
" -f FILENODE show info for table with given file node\n"
" -H HOSTNAME database server host or socket directory\n"
" -i show indexes and sequences too\n"
" -o OID show info for table with given OID\n"
" -p PORT database server port number\n"
" -q quiet (don't show headers)\n"
" -s show all tablespaces\n" " -s show all tablespaces\n"
" -S show system objects too\n" " -S show system objects too\n"
" -i show indexes and sequences too\n" " -t TABLE show info for named table\n"
" -U NAME connect as specified database user\n"
" -x extended (show additional columns)\n" " -x extended (show additional columns)\n"
" -q quiet (don't show headers)\n" " --help show this help, then exit\n"
" -t <table> show info for table named <table>\n" " --version output version information, then exit\n"
" -o <oid> show info for table with Oid <oid>\n" "\nThe default action is to show all database OIDs.\n\n"
" -f <filenode> show info for table with filenode <filenode>\n" "Report bugs to <pgsql-bugs@postgresql.org>.\n",
" -H host connect to remote host\n" progname, progname);
" -p port host port to connect to\n"
" -U username username to connect with\n"
);
exit(1);
break;
}
}
} }
void * void *

View File

@ -1,5 +1,5 @@
/* /*
* $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.17 2009/01/06 17:27:06 tgl Exp $ * $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.18 2009/02/27 09:30:21 petere Exp $
* *
* *
* pg_standby.c * pg_standby.c
@ -42,6 +42,8 @@ int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
const char *progname;
/* Options and defaults */ /* Options and defaults */
int sleeptime = 5; /* amount of time to sleep between file checks */ int sleeptime = 5; /* amount of time to sleep between file checks */
int waittime = -1; /* how long we have been waiting, -1 no wait int waittime = -1; /* how long we have been waiting, -1 no wait
@ -146,7 +148,7 @@ CustomizableInitialize(void)
*/ */
if (stat(archiveLocation, &stat_buf) != 0) if (stat(archiveLocation, &stat_buf) != 0)
{ {
fprintf(stderr, "pg_standby: archiveLocation \"%s\" does not exist\n", archiveLocation); fprintf(stderr, "%s: archiveLocation \"%s\" does not exist\n", progname, archiveLocation);
fflush(stderr); fflush(stderr);
exit(2); exit(2);
} }
@ -261,8 +263,8 @@ CustomizableCleanupPriorWALFiles(void)
rc = unlink(WALFilePath); rc = unlink(WALFilePath);
if (rc != 0) if (rc != 0)
{ {
fprintf(stderr, "\npg_standby: ERROR failed to remove \"%s\": %s", fprintf(stderr, "\n%s: ERROR failed to remove \"%s\": %s",
WALFilePath, strerror(errno)); progname, WALFilePath, strerror(errno));
break; break;
} }
} }
@ -271,7 +273,7 @@ CustomizableCleanupPriorWALFiles(void)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
else else
fprintf(stderr, "pg_standby: archiveLocation \"%s\" open error\n", archiveLocation); fprintf(stderr, "%s: archiveLocation \"%s\" open error\n", progname, archiveLocation);
closedir(xldir); closedir(xldir);
fflush(stderr); fflush(stderr);
@ -430,23 +432,29 @@ RestoreWALFileForRecovery(void)
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "\npg_standby allows Warm Standby servers to be configured\n"); printf("%s allows PostgreSQL warm standby servers to be configured.\n\n", progname);
fprintf(stderr, "Usage:\n"); printf("Usage:\n");
fprintf(stderr, " pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n"); printf(" %s [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n", progname);
fprintf(stderr, " note space between ARCHIVELOCATION and NEXTWALFILE\n"); printf("\n"
fprintf(stderr, "with main intended use as a restore_command in the recovery.conf\n"); "with main intended use as a restore_command in the recovery.conf:\n"
fprintf(stderr, " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n"); " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n"
fprintf(stderr, "e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n"); "e.g.\n"
fprintf(stderr, "\nOptions:\n"); " restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
fprintf(stderr, " -c copies file from archive (default)\n"); printf("\nOptions:\n");
fprintf(stderr, " -d generate lots of debugging output (testing only)\n"); printf(" -c copies file from archive (default)\n");
fprintf(stderr, " -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n"); printf(" -d generate lots of debugging output (testing only)\n");
fprintf(stderr, " -l links into archive (leaves file in archive)\n"); printf(" -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit\n"
fprintf(stderr, " -r MAXRETRIES max number of times to retry, with progressive wait (default=3)\n"); " (0 keeps all)\n");
fprintf(stderr, " -s SLEEPTIME seconds to wait between file checks (min=1, max=60, default=5)\n"); printf(" -l links into archive (leaves file in archive)\n");
fprintf(stderr, " -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n"); printf(" -r MAXRETRIES max number of times to retry, with progressive wait\n"
fprintf(stderr, " -w MAXWAITTIME max seconds to wait for a file (0=no limit)(default=0)\n"); " (default=3)\n");
fflush(stderr); printf(" -s SLEEPTIME seconds to wait between file checks (min=1, max=60,\n"
" default=5)\n");
printf(" -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n");
printf(" -w MAXWAITTIME max seconds to wait for a file (0=no limit) (default=0)\n");
printf(" --help show this help, then exit\n");
printf(" --version output version information, then exit\n");
printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
} }
static void static void
@ -461,6 +469,22 @@ main(int argc, char **argv)
{ {
int c; int c;
progname = get_progname(argv[0]);
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage();
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("pg_standby (PostgreSQL) " PG_VERSION);
exit(0);
}
}
(void) signal(SIGINT, sighandler); (void) signal(SIGINT, sighandler);
(void) signal(SIGQUIT, sighandler); (void) signal(SIGQUIT, sighandler);
@ -478,8 +502,7 @@ main(int argc, char **argv)
keepfiles = atoi(optarg); keepfiles = atoi(optarg);
if (keepfiles < 0) if (keepfiles < 0)
{ {
fprintf(stderr, "usage: pg_standby -k keepfiles must be >= 0\n"); fprintf(stderr, "%s: -k keepfiles must be >= 0\n", progname);
usage();
exit(2); exit(2);
} }
break; break;
@ -490,8 +513,7 @@ main(int argc, char **argv)
maxretries = atoi(optarg); maxretries = atoi(optarg);
if (maxretries < 0) if (maxretries < 0)
{ {
fprintf(stderr, "usage: pg_standby -r maxretries must be >= 0\n"); fprintf(stderr, "%s: -r maxretries must be >= 0\n", progname);
usage();
exit(2); exit(2);
} }
break; break;
@ -499,8 +521,7 @@ main(int argc, char **argv)
sleeptime = atoi(optarg); sleeptime = atoi(optarg);
if (sleeptime <= 0 || sleeptime > 60) if (sleeptime <= 0 || sleeptime > 60)
{ {
fprintf(stderr, "usage: pg_standby -s sleeptime incorrectly set\n"); fprintf(stderr, "%s: -s sleeptime incorrectly set\n", progname);
usage();
exit(2); exit(2);
} }
break; break;
@ -513,13 +534,12 @@ main(int argc, char **argv)
maxwaittime = atoi(optarg); maxwaittime = atoi(optarg);
if (maxwaittime < 0) if (maxwaittime < 0)
{ {
fprintf(stderr, "usage: pg_standby -w maxwaittime incorrectly set\n"); fprintf(stderr, "%s: -w maxwaittime incorrectly set\n", progname);
usage();
exit(2); exit(2);
} }
break; break;
default: default:
usage(); fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
exit(2); exit(2);
break; break;
} }
@ -530,7 +550,7 @@ main(int argc, char **argv)
*/ */
if (argc == 1) if (argc == 1)
{ {
usage(); fprintf(stderr, "%s: not enough command-line arguments\n", progname);
exit(2); exit(2);
} }
@ -547,8 +567,8 @@ main(int argc, char **argv)
} }
else else
{ {
fprintf(stderr, "pg_standby: must specify archiveLocation\n"); fprintf(stderr, "%s: must specify archive location\n", progname);
usage(); fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
exit(2); exit(2);
} }
@ -559,8 +579,8 @@ main(int argc, char **argv)
} }
else else
{ {
fprintf(stderr, "pg_standby: use %%f to specify nextWALFileName\n"); fprintf(stderr, "%s: use %%f to specify nextWALFileName\n", progname);
usage(); fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
exit(2); exit(2);
} }
@ -571,8 +591,8 @@ main(int argc, char **argv)
} }
else else
{ {
fprintf(stderr, "pg_standby: use %%p to specify xlogFilePath\n"); fprintf(stderr, "%s: use %%p to specify xlogFilePath\n", progname);
usage(); fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
exit(2); exit(2);
} }

View File

@ -4,7 +4,7 @@
* A simple benchmark program for PostgreSQL * A simple benchmark program for PostgreSQL
* Originally written by Tatsuo Ishii and enhanced by many contributors. * Originally written by Tatsuo Ishii and enhanced by many contributors.
* *
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.84 2009/02/25 13:24:40 petere Exp $ * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.85 2009/02/27 09:30:21 petere Exp $
* Copyright (c) 2000-2009, PostgreSQL Global Development Group * Copyright (c) 2000-2009, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED; * ALL RIGHTS RESERVED;
* *
@ -250,10 +250,41 @@ diffTime(struct timeval *t1, struct timeval *t2, struct timeval *result)
} }
static void static void
usage(void) usage(const char *progname)
{ {
fprintf(stderr, "usage: pgbench [-h hostname][-p port][-c nclients][-t ntransactions | -T duration][-s scaling_factor][-D varname=value][-n][-C][-v][-S][-N][-M querymode][-f filename][-l][-U login][-d][dbname]\n"); printf("%s is a benchmarking tool for PostgreSQL.\n\n"
fprintf(stderr, "(initialize mode): pgbench -i [-h hostname][-p port][-s scaling_factor] [-F fillfactor] [-U login][-d][dbname]\n"); "Usage:\n"
" %s [OPTIONS]... [DBNAME]\n"
"\nInitialization options:\n"
" -i invokes initialization mode\n"
" -F NUM fill factor\n"
" -s NUM scaling factor\n"
"\nBenchmarking options:\n"
" -c NUM number of concurrent database clients (default: 1)\n"
" -C establish new connection for each transaction\n"
" -D VARNAME=VALUE\n"
" define variable for use by custom script\n"
" -f FILENAME read transaction script from FILENAME\n"
" -l write transaction times to log file\n"
" -M {simple|extended|prepared}\n"
" protocol for submitting queries to server (default: simple)\n"
" -n do not run VACUUM before tests\n"
" -N do not update tables \"tellers\" and \"branches\"\n"
" -s NUM report scale factor in output\n"
" -S perform SELECT-only transactions\n"
" -t NUM number of transactions each client runs (default: 10)\n"
" -T NUM duration of benchmark test in seconds\n"
" -v vacuum all four standard tables before tests\n"
"\nCommon options:\n"
" -d print debugging output\n"
" -h HOSTNAME database server host or socket directory\n"
" -p PORT database server port number\n"
" -U USERNAME connect as specified database user\n"
" --help show this help, then exit\n"
" --version output version information, then exit\n"
"\n"
"Report bugs to <pgsql-bugs@postgresql.org>.\n",
progname, progname);
} }
/* random number generator: uniform distribution from min to max inclusive */ /* random number generator: uniform distribution from min to max inclusive */
@ -1499,6 +1530,24 @@ main(int argc, char **argv)
char val[64]; char val[64];
const char *progname;
progname = get_progname(argv[0]);
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("pgbench (PostgreSQL) " PG_VERSION);
exit(0);
}
}
#ifdef WIN32 #ifdef WIN32
/* stderr is buffered on Win32. */ /* stderr is buffered on Win32. */
setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0);
@ -1652,7 +1701,7 @@ main(int argc, char **argv)
case 'M': case 'M':
if (num_files > 0) if (num_files > 0)
{ {
fprintf(stderr, "querymode(-M) should be specifiled before transaction scripts(-f)\n"); fprintf(stderr, "query mode (-M) should be specifiled before transaction scripts (-f)\n");
exit(1); exit(1);
} }
for (querymode = 0; querymode < NUM_QUERYMODE; querymode++) for (querymode = 0; querymode < NUM_QUERYMODE; querymode++)
@ -1660,12 +1709,12 @@ main(int argc, char **argv)
break; break;
if (querymode >= NUM_QUERYMODE) if (querymode >= NUM_QUERYMODE)
{ {
fprintf(stderr, "invalid querymode(-M): %s\n", optarg); fprintf(stderr, "invalid query mode (-M): %s\n", optarg);
exit(1); exit(1);
} }
break; break;
default: default:
usage(); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1); exit(1);
break; break;
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/contrib/vacuumlo/vacuumlo.c,v 1.40 2009/02/26 16:02:37 petere Exp $ * $PostgreSQL: pgsql/contrib/vacuumlo/vacuumlo.c,v 1.41 2009/02/27 09:30:21 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -51,7 +51,7 @@ struct _param
}; };
int vacuumlo(char *, struct _param *); int vacuumlo(char *, struct _param *);
void usage(void); void usage(const char *progname);
@ -307,10 +307,10 @@ vacuumlo(char *database, struct _param * param)
} }
void void
usage(void) usage(const char *progname)
{ {
printf("vacuumlo removes unreferenced large objects from databases.\n\n"); printf("%s removes unreferenced large objects from databases.\n\n", progname);
printf("Usage:\n vacuumlo [OPTION]... DBNAME...\n\n"); printf("Usage:\n %s [OPTION]... DBNAME...\n\n", progname);
printf("Options:\n"); printf("Options:\n");
printf(" -h HOSTNAME database server host or socket directory\n"); printf(" -h HOSTNAME database server host or socket directory\n");
printf(" -n don't remove large objects, just show what would be done\n"); printf(" -n don't remove large objects, just show what would be done\n");
@ -319,7 +319,10 @@ usage(void)
printf(" -w never prompt for password\n"); printf(" -w never prompt for password\n");
printf(" -W force password prompt\n"); printf(" -W force password prompt\n");
printf(" -v write a lot of progress messages\n"); printf(" -v write a lot of progress messages\n");
printf(" --help show this help, then exit\n");
printf(" --version output version information, then exit\n");
printf("\n"); printf("\n");
printf("Report bugs to <pgsql-bugs@postgresql.org>.\n");
} }
@ -330,6 +333,9 @@ main(int argc, char **argv)
struct _param param; struct _param param;
int c; int c;
int port; int port;
const char *progname;
progname = get_progname(argv[0]);
/* Parameter handling */ /* Parameter handling */
param.pg_user = NULL; param.pg_user = NULL;
@ -339,20 +345,30 @@ main(int argc, char **argv)
param.verbose = 0; param.verbose = 0;
param.dry_run = 0; param.dry_run = 0;
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("vacuumlo (PostgreSQL) " PG_VERSION);
exit(0);
}
}
while (1) while (1)
{ {
c = getopt(argc, argv, "?h:U:p:vnwW"); c = getopt(argc, argv, "h:U:p:vnwW");
if (c == -1) if (c == -1)
break; break;
switch (c) switch (c)
{ {
case '?': case '?':
if (optopt == '?') fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
{
usage();
exit(0);
}
exit(1); exit(1);
case ':': case ':':
exit(1); exit(1);
@ -376,7 +392,7 @@ main(int argc, char **argv)
port = strtol(optarg, NULL, 10); port = strtol(optarg, NULL, 10);
if ((port < 1) || (port > 65535)) if ((port < 1) || (port > 65535))
{ {
fprintf(stderr, "[%s]: invalid port number '%s'\n", argv[0], optarg); fprintf(stderr, "%s: invalid port number: %s\n", progname, optarg);
exit(1); exit(1);
} }
param.pg_port = strdup(optarg); param.pg_port = strdup(optarg);

View File

@ -1,4 +1,4 @@
<!-- $PostgreSQL: pgsql/doc/src/sgml/pgstandby.sgml,v 2.6 2008/12/15 22:08:35 momjian Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/pgstandby.sgml,v 2.7 2009/02/27 09:30:21 petere Exp $ -->
<sect1 id="pgstandby"> <sect1 id="pgstandby">
<title>pg_standby</title> <title>pg_standby</title>
@ -199,13 +199,6 @@ pg_standby <optional> <replaceable>option</> ... </optional> <replaceable>archiv
</tbody> </tbody>
</tgroup> </tgroup>
</table> </table>
<note>
<para>
<literal>--help</literal> is not supported since
<application>pg_standby</application> is not intended for interactive use,
except during development and testing.
</para>
</note>
</sect2> </sect2>
<sect2> <sect2>