Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b6e4c8f

Browse files
committed
Equip the programs installed by contrib with proper --help and --version
options and normally formatted help output.
1 parent 867a2a6 commit b6e4c8f

File tree

5 files changed

+195
-86
lines changed

5 files changed

+195
-86
lines changed

contrib/oid2name/oid2name.c

+52-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Originally by
66
* B. Palmer, bpalmer@crimelabs.net 1-17-2001
77
*
8-
* $PostgreSQL: pgsql/contrib/oid2name/oid2name.c,v 1.34 2009/02/25 13:24:40 petere Exp $
8+
* $PostgreSQL: pgsql/contrib/oid2name/oid2name.c,v 1.35 2009/02/27 09:30:21 petere Exp $
99
*/
1010
#include "postgres_fe.h"
1111

@@ -47,6 +47,7 @@ struct options
4747
};
4848

4949
/* function prototypes */
50+
static void help(const char *progname);
5051
void get_opts(int, char **, struct options *);
5152
void *myalloc(size_t size);
5253
char *mystrdup(const char *str);
@@ -64,6 +65,9 @@ void
6465
get_opts(int argc, char **argv, struct options * my_opts)
6566
{
6667
int c;
68+
const char *progname;
69+
70+
progname = get_progname(argv[0]);
6771

6872
/* set the defaults */
6973
my_opts->quiet = false;
@@ -77,8 +81,22 @@ get_opts(int argc, char **argv, struct options * my_opts)
7781
my_opts->port = NULL;
7882
my_opts->username = NULL;
7983

84+
if (argc > 1)
85+
{
86+
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
87+
{
88+
help(progname);
89+
exit(0);
90+
}
91+
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
92+
{
93+
puts("oid2name (PostgreSQL) " PG_VERSION);
94+
exit(0);
95+
}
96+
}
97+
8098
/* get opts */
81-
while ((c = getopt(argc, argv, "H:p:U:d:t:o:f:qSxish?")) != -1)
99+
while ((c = getopt(argc, argv, "H:p:U:d:t:o:f:qSxish")) != -1)
82100
{
83101
switch (c)
84102
{
@@ -142,31 +160,44 @@ get_opts(int argc, char **argv, struct options * my_opts)
142160
my_opts->tablespaces = true;
143161
break;
144162

145-
/* help! (ugly in code for easier editing) */
146-
case '?':
147163
case 'h':
148-
fprintf(stderr,
149-
"Usage: oid2name [-s|-d database] [-S][-i][-q][-x] [-t table|-o oid|-f file] ...\n"
150-
" default action show all database Oids\n"
151-
" -d database database to connect to\n"
152-
" -s show all tablespaces\n"
153-
" -S show system objects too\n"
154-
" -i show indexes and sequences too\n"
155-
" -x extended (show additional columns)\n"
156-
" -q quiet (don't show headers)\n"
157-
" -t <table> show info for table named <table>\n"
158-
" -o <oid> show info for table with Oid <oid>\n"
159-
" -f <filenode> show info for table with filenode <filenode>\n"
160-
" -H host connect to remote host\n"
161-
" -p port host port to connect to\n"
162-
" -U username username to connect with\n"
163-
);
164-
exit(1);
164+
help(progname);
165+
exit(0);
165166
break;
167+
168+
default:
169+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
170+
exit(1);
166171
}
167172
}
168173
}
169174

175+
static void
176+
help(const char *progname)
177+
{
178+
printf("%s helps examining the file structure used by PostgreSQL.\n\n"
179+
"Usage:\n"
180+
" %s [OPTIONS]...\n"
181+
"\nOptions:\n"
182+
" -d DBNAME database to connect to\n"
183+
" -f FILENODE show info for table with given file node\n"
184+
" -H HOSTNAME database server host or socket directory\n"
185+
" -i show indexes and sequences too\n"
186+
" -o OID show info for table with given OID\n"
187+
" -p PORT database server port number\n"
188+
" -q quiet (don't show headers)\n"
189+
" -s show all tablespaces\n"
190+
" -S show system objects too\n"
191+
" -t TABLE show info for named table\n"
192+
" -U NAME connect as specified database user\n"
193+
" -x extended (show additional columns)\n"
194+
" --help show this help, then exit\n"
195+
" --version output version information, then exit\n"
196+
"\nThe default action is to show all database OIDs.\n\n"
197+
"Report bugs to <pgsql-bugs@postgresql.org>.\n",
198+
progname, progname);
199+
}
200+
170201
void *
171202
myalloc(size_t size)
172203
{

contrib/pg_standby/pg_standby.c

+58-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.17 2009/01/06 17:27:06 tgl Exp $
2+
* $PostgreSQL: pgsql/contrib/pg_standby/pg_standby.c,v 1.18 2009/02/27 09:30:21 petere Exp $
33
*
44
*
55
* pg_standby.c
@@ -42,6 +42,8 @@ int getopt(int argc, char *const argv[], const char *optstring);
4242
extern char *optarg;
4343
extern int optind;
4444

45+
const char *progname;
46+
4547
/* Options and defaults */
4648
int sleeptime = 5; /* amount of time to sleep between file checks */
4749
int waittime = -1; /* how long we have been waiting, -1 no wait
@@ -146,7 +148,7 @@ CustomizableInitialize(void)
146148
*/
147149
if (stat(archiveLocation, &stat_buf) != 0)
148150
{
149-
fprintf(stderr, "pg_standby: archiveLocation \"%s\" does not exist\n", archiveLocation);
151+
fprintf(stderr, "%s: archiveLocation \"%s\" does not exist\n", progname, archiveLocation);
150152
fflush(stderr);
151153
exit(2);
152154
}
@@ -261,8 +263,8 @@ CustomizableCleanupPriorWALFiles(void)
261263
rc = unlink(WALFilePath);
262264
if (rc != 0)
263265
{
264-
fprintf(stderr, "\npg_standby: ERROR failed to remove \"%s\": %s",
265-
WALFilePath, strerror(errno));
266+
fprintf(stderr, "\n%s: ERROR failed to remove \"%s\": %s",
267+
progname, WALFilePath, strerror(errno));
266268
break;
267269
}
268270
}
@@ -271,7 +273,7 @@ CustomizableCleanupPriorWALFiles(void)
271273
fprintf(stderr, "\n");
272274
}
273275
else
274-
fprintf(stderr, "pg_standby: archiveLocation \"%s\" open error\n", archiveLocation);
276+
fprintf(stderr, "%s: archiveLocation \"%s\" open error\n", progname, archiveLocation);
275277

276278
closedir(xldir);
277279
fflush(stderr);
@@ -430,23 +432,29 @@ RestoreWALFileForRecovery(void)
430432
static void
431433
usage(void)
432434
{
433-
fprintf(stderr, "\npg_standby allows Warm Standby servers to be configured\n");
434-
fprintf(stderr, "Usage:\n");
435-
fprintf(stderr, " pg_standby [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n");
436-
fprintf(stderr, " note space between ARCHIVELOCATION and NEXTWALFILE\n");
437-
fprintf(stderr, "with main intended use as a restore_command in the recovery.conf\n");
438-
fprintf(stderr, " restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n");
439-
fprintf(stderr, "e.g. restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
440-
fprintf(stderr, "\nOptions:\n");
441-
fprintf(stderr, " -c copies file from archive (default)\n");
442-
fprintf(stderr, " -d generate lots of debugging output (testing only)\n");
443-
fprintf(stderr, " -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit (0 keeps all)\n");
444-
fprintf(stderr, " -l links into archive (leaves file in archive)\n");
445-
fprintf(stderr, " -r MAXRETRIES max number of times to retry, with progressive wait (default=3)\n");
446-
fprintf(stderr, " -s SLEEPTIME seconds to wait between file checks (min=1, max=60, default=5)\n");
447-
fprintf(stderr, " -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n");
448-
fprintf(stderr, " -w MAXWAITTIME max seconds to wait for a file (0=no limit)(default=0)\n");
449-
fflush(stderr);
435+
printf("%s allows PostgreSQL warm standby servers to be configured.\n\n", progname);
436+
printf("Usage:\n");
437+
printf(" %s [OPTION]... ARCHIVELOCATION NEXTWALFILE XLOGFILEPATH [RESTARTWALFILE]\n", progname);
438+
printf("\n"
439+
"with main intended use as a restore_command in the recovery.conf:\n"
440+
" restore_command = 'pg_standby [OPTION]... ARCHIVELOCATION %%f %%p %%r'\n"
441+
"e.g.\n"
442+
" restore_command = 'pg_standby -l /mnt/server/archiverdir %%f %%p %%r'\n");
443+
printf("\nOptions:\n");
444+
printf(" -c copies file from archive (default)\n");
445+
printf(" -d generate lots of debugging output (testing only)\n");
446+
printf(" -k NUMFILESTOKEEP if RESTARTWALFILE not used, removes files prior to limit\n"
447+
" (0 keeps all)\n");
448+
printf(" -l links into archive (leaves file in archive)\n");
449+
printf(" -r MAXRETRIES max number of times to retry, with progressive wait\n"
450+
" (default=3)\n");
451+
printf(" -s SLEEPTIME seconds to wait between file checks (min=1, max=60,\n"
452+
" default=5)\n");
453+
printf(" -t TRIGGERFILE defines a trigger file to initiate failover (no default)\n");
454+
printf(" -w MAXWAITTIME max seconds to wait for a file (0=no limit) (default=0)\n");
455+
printf(" --help show this help, then exit\n");
456+
printf(" --version output version information, then exit\n");
457+
printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
450458
}
451459

452460
static void
@@ -461,6 +469,22 @@ main(int argc, char **argv)
461469
{
462470
int c;
463471

472+
progname = get_progname(argv[0]);
473+
474+
if (argc > 1)
475+
{
476+
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
477+
{
478+
usage();
479+
exit(0);
480+
}
481+
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
482+
{
483+
puts("pg_standby (PostgreSQL) " PG_VERSION);
484+
exit(0);
485+
}
486+
}
487+
464488
(void) signal(SIGINT, sighandler);
465489
(void) signal(SIGQUIT, sighandler);
466490

@@ -478,8 +502,7 @@ main(int argc, char **argv)
478502
keepfiles = atoi(optarg);
479503
if (keepfiles < 0)
480504
{
481-
fprintf(stderr, "usage: pg_standby -k keepfiles must be >= 0\n");
482-
usage();
505+
fprintf(stderr, "%s: -k keepfiles must be >= 0\n", progname);
483506
exit(2);
484507
}
485508
break;
@@ -490,17 +513,15 @@ main(int argc, char **argv)
490513
maxretries = atoi(optarg);
491514
if (maxretries < 0)
492515
{
493-
fprintf(stderr, "usage: pg_standby -r maxretries must be >= 0\n");
494-
usage();
516+
fprintf(stderr, "%s: -r maxretries must be >= 0\n", progname);
495517
exit(2);
496518
}
497519
break;
498520
case 's': /* Sleep time */
499521
sleeptime = atoi(optarg);
500522
if (sleeptime <= 0 || sleeptime > 60)
501523
{
502-
fprintf(stderr, "usage: pg_standby -s sleeptime incorrectly set\n");
503-
usage();
524+
fprintf(stderr, "%s: -s sleeptime incorrectly set\n", progname);
504525
exit(2);
505526
}
506527
break;
@@ -513,13 +534,12 @@ main(int argc, char **argv)
513534
maxwaittime = atoi(optarg);
514535
if (maxwaittime < 0)
515536
{
516-
fprintf(stderr, "usage: pg_standby -w maxwaittime incorrectly set\n");
517-
usage();
537+
fprintf(stderr, "%s: -w maxwaittime incorrectly set\n", progname);
518538
exit(2);
519539
}
520540
break;
521541
default:
522-
usage();
542+
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
523543
exit(2);
524544
break;
525545
}
@@ -530,7 +550,7 @@ main(int argc, char **argv)
530550
*/
531551
if (argc == 1)
532552
{
533-
usage();
553+
fprintf(stderr, "%s: not enough command-line arguments\n", progname);
534554
exit(2);
535555
}
536556

@@ -547,8 +567,8 @@ main(int argc, char **argv)
547567
}
548568
else
549569
{
550-
fprintf(stderr, "pg_standby: must specify archiveLocation\n");
551-
usage();
570+
fprintf(stderr, "%s: must specify archive location\n", progname);
571+
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
552572
exit(2);
553573
}
554574

@@ -559,8 +579,8 @@ main(int argc, char **argv)
559579
}
560580
else
561581
{
562-
fprintf(stderr, "pg_standby: use %%f to specify nextWALFileName\n");
563-
usage();
582+
fprintf(stderr, "%s: use %%f to specify nextWALFileName\n", progname);
583+
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
564584
exit(2);
565585
}
566586

@@ -571,8 +591,8 @@ main(int argc, char **argv)
571591
}
572592
else
573593
{
574-
fprintf(stderr, "pg_standby: use %%p to specify xlogFilePath\n");
575-
usage();
594+
fprintf(stderr, "%s: use %%p to specify xlogFilePath\n", progname);
595+
fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
576596
exit(2);
577597
}
578598

0 commit comments

Comments
 (0)