diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 4730c73396..cffbfc864e 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -667,12 +667,13 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 				break;
 			case '?':
 				/* Actual help option given */
-				if (strcmp(argv[optind - 1], "-?") == 0)
+				if (optind <= argc &&
+				    strcmp(argv[optind - 1], "-?") == 0)
 				{
 					usage(NOPAGER);
 					exit(EXIT_SUCCESS);
 				}
-				/* unknown option reported by getopt */
+				/* unknown option or missing argument */
 				else
 					goto unknown_option;
 				break;
-- 
2.23.0
