diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 210be63f67c..4a4ce82e595 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -552,15 +552,17 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) options->single_txn = true; break; case '?': - /* Actual help option given */ - if (strcmp(argv[optind - 1], "--help") == 0 || strcmp(argv[optind - 1], "-?") == 0) + if (optind <= argc && + (strcmp(argv[optind - 1], "--help") == 0 || + strcmp(argv[optind - 1], "-?") == 0)) { + /* actual help option given */ usage(); exit(EXIT_SUCCESS); } - /* unknown option reported by getopt */ else { + /* getopt error (unknown option or missing argument) */ fprintf(stderr, _("Try \"%s --help\" for more information.\n"), pset.progname); exit(EXIT_FAILURE);