From 76fd342496612c8432ef8f6c71794c935497d3c9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 4 Dec 2024 15:04:15 -0600 Subject: Provide a better error message for misplaced dispatch options. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this patch, misplacing a special must-be-first option for dispatching to a subprogram (e.g., postgres -D . --single) would fail with an error like FATAL: --single requires a value This patch adjusts this error to more accurately complain that the special option wasn't listed first. The aforementioned error message now looks like FATAL: --single must be first argument The dispatch option parsing code has been refactored for use wherever ParseLongOption() is called. Beyond the obvious advantage of avoiding code duplication, this should prevent similar problems when new dispatch options are added. Note that we assume that none of the dispatch option names match another valid command-line argument, such as the name of a configuration parameter. Ideally, we'd remove this must-be-first requirement for these options, but after some investigation, we decided that wasn't worth the added complexity and behavior changes. Author: Nathan Bossart, Greg Sabino Mullane Reviewed-by: Greg Sabino Mullane, Peter Eisentraut, Álvaro Herrera, Tom Lane Discussion: https://postgr.es/m/CAKAnmmJkZtZAiSryho%3DgYpbvC7H-HNjEDAh16F3SoC9LPu8rqQ%40mail.gmail.com --- src/backend/tcop/postgres.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/backend/tcop/postgres.c') diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 4b985bd0561..42af7680456 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3947,8 +3947,21 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx, /* ignored for consistency with the postmaster */ break; - case 'c': case '-': + + /* + * Error if the user misplaced a special must-be-first option + * for dispatching to a subprogram. parse_dispatch_option() + * returns DISPATCH_POSTMASTER if it doesn't find a match, so + * error for anything else. + */ + if (parse_dispatch_option(optarg) != DISPATCH_POSTMASTER) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("--%s must be first argument", optarg))); + + /* FALLTHROUGH */ + case 'c': { char *name, *value; -- cgit v1.2.3