Add more detail to error message for invalid arguments for server process
authorPeter Eisentraut
Sat, 10 Mar 2012 23:52:05 +0000 (01:52 +0200)
committerPeter Eisentraut
Sun, 11 Mar 2012 00:03:52 +0000 (02:03 +0200)
It now prints the argument that was at fault.

Also fix a small misbehavior where the error message issued by
getopt() would complain about a program named "--single", because
that's what argv[0] is in the server process.

src/backend/tcop/postgres.c

index 49a396918d9bd3ed2bfc228369ced0c497ee09c9..397c0734c2900501b443bf16f2a7296c2d09aa88 100644 (file)
@@ -3190,6 +3190,13 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
        gucsource = PGC_S_CLIENT;       /* switches came from client */
    }
 
+#ifdef HAVE_INT_OPTERR
+   /* Turn this off because it's either printed to stderr and not the log
+    * where we'd want it, or argv[0] is now "--single", which would make for a
+    * weird error message.  We print our own error message below. */
+   opterr = 0;
+#endif
+
    /*
     * Parse command-line options.  CAUTION: keep this in sync with
     * postmaster/postmaster.c (the option sets should not conflict) and with
@@ -3363,33 +3370,39 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
                errs++;
                break;
        }
+
+       if (errs)
+           break;
    }
 
    /*
     * Should be no more arguments except an optional database name, and
     * that's only in the secure case.
     */
-   if (errs || argc - optind > 1 || (argc != optind && !secure))
+   if (!errs && secure && argc - optind >= 1)
+       dbname = strdup(argv[optind++]);
+   else
+       dbname = NULL;
+
+   if (errs || argc != optind)
    {
+       if (errs)
+           optind--;           /* complain about the previous argument */
+
        /* spell the error message a bit differently depending on context */
        if (IsUnderPostmaster)
            ereport(FATAL,
                    (errcode(ERRCODE_SYNTAX_ERROR),
-                errmsg("invalid command-line arguments for server process"),
+                errmsg("invalid command-line argument for server process: %s", argv[optind]),
              errhint("Try \"%s --help\" for more information.", progname)));
        else
            ereport(FATAL,
                    (errcode(ERRCODE_SYNTAX_ERROR),
-                    errmsg("%s: invalid command-line arguments",
-                           progname),
+                    errmsg("%s: invalid command-line argument: %s",
+                           progname, argv[optind]),
              errhint("Try \"%s --help\" for more information.", progname)));
    }
 
-   if (argc - optind == 1)
-       dbname = strdup(argv[optind]);
-   else
-       dbname = NULL;
-
    /*
     * Reset getopt(3) library so that it will work correctly in subprocesses
     * or when this function is called a second time with another array.