Hi,

There is a small catch in the parsing of --help and --version args by pgbench:
they are processed correctly only as the first argument. If it's not the case,
strange error message occurs:

$ pgbench -q --help
pgbench: unrecognized option '--help'
Try "pgbench --help" for more information.

The reason for this behavior is how these two arguments are handled in
src/bin/pgbench/pgbench.c:

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("pgbench (PostgreSQL) " PG_VERSION);
        exit(0);
    }
}

All other arguments are processed with getopt_long. The proposed patch replaces
the existing way of parsing the --help and --version arguments with getopt_long,
expanding the existing switch statement.

--
Andrei Korigodski

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c089..d1ff85a677 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -4763,6 +4763,7 @@ main(int argc, char **argv)
 		{"define", required_argument, NULL, 'D'},
 		{"file", required_argument, NULL, 'f'},
 		{"fillfactor", required_argument, NULL, 'F'},
+		{"help", no_argument, NULL, '?'},
 		{"host", required_argument, NULL, 'h'},
 		{"initialize", no_argument, NULL, 'i'},
 		{"init-steps", required_argument, NULL, 'I'},
@@ -4783,6 +4784,7 @@ main(int argc, char **argv)
 		{"transactions", required_argument, NULL, 't'},
 		{"username", required_argument, NULL, 'U'},
 		{"vacuum-all", no_argument, NULL, 'v'},
+		{"version", no_argument, NULL, 'V'},
 		/* long-named only options */
 		{"unlogged-tables", no_argument, NULL, 1},
 		{"tablespace", required_argument, NULL, 2},
@@ -4832,20 +4834,6 @@ main(int argc, char **argv)
 
 	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("pgbench (PostgreSQL) " PG_VERSION);
-			exit(0);
-		}
-	}
-
 #ifdef WIN32
 	/* stderr is buffered on Win32. */
 	setvbuf(stderr, NULL, _IONBF, 0);
@@ -4868,7 +4856,7 @@ main(int argc, char **argv)
 		exit(1);
 	}
 
-	while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1)
+	while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:V?", long_options, &optindex)) != -1)
 	{
 		char	   *script;
 
@@ -5097,6 +5085,14 @@ main(int argc, char **argv)
 					latency_limit = (int64) (limit_ms * 1000);
 				}
 				break;
+			case 'V':
+				puts("pgbench (PostgreSQL) " PG_VERSION);
+				exit(0);
+				break;
+			case '?':
+				usage();
+				exit(0);
+				break;
 			case 1:				/* unlogged-tables */
 				initialization_option_set = true;
 				unlogged_tables = true;

Reply via email to