From 053f672caa221120c3b24ed3237896f5ade2ca0c Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Mon, 21 Sep 2015 09:37:19 +0900 Subject: [PATCH] Treat -?/--help and -V/--version as normal options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently repmgr/repmgrd will only accept these as valid when provided as the first command line option, however it's possible a user will want to get the output of those options by adding them to the end of a previously inputted command. Note that after the first of these options is encountered, the program will terminate and not process any other options. This is consistent with psql's behaviour Per GitHub issue #107 from Sébastien Gross. --- repmgr.c | 24 +++++++++--------------- repmgrd.c | 24 +++++++++--------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/repmgr.c b/repmgr.c index 1557146f..9fca281a 100644 --- a/repmgr.c +++ b/repmgr.c @@ -145,6 +145,8 @@ main(int argc, char **argv) {"check-upstream-config", no_argument, NULL, 2}, {"recovery-min-apply-delay", required_argument, NULL, 3}, {"ignore-external-config-files", no_argument, NULL, 4}, + {"help", no_argument, NULL, '?'}, + {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; @@ -158,28 +160,20 @@ main(int argc, char **argv) progname = get_progname(argv[0]); - if (argc > 1) - { - if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) - { - help(progname); - exit(SUCCESS); - } - if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) - { - printf("%s %s (PostgreSQL %s)\n", progname, REPMGR_VERSION, PG_VERSION); - exit(SUCCESS); - } - } - /* Prevent getopt_long() from printing an error message */ opterr = 0; - while ((c = getopt_long(argc, argv, "d:h:p:U:S:D:l:f:R:w:k:FWIvb:r:c", long_options, + while ((c = getopt_long(argc, argv, "?Vd:h:p:U:S:D:l:f:R:w:k:FWIvb:r:c", long_options, &optindex)) != -1) { switch (c) { + case '?': + help(progname); + exit(SUCCESS); + case 'V': + printf("%s %s (PostgreSQL %s)\n", progname, REPMGR_VERSION, PG_VERSION); + exit(SUCCESS); case 'd': strncpy(runtime_options.dbname, optarg, MAXLEN); break; diff --git a/repmgrd.c b/repmgrd.c index ac9e6858..48cabb77 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -147,6 +147,8 @@ main(int argc, char **argv) {"monitoring-history", no_argument, NULL, 'm'}, {"daemonize", no_argument, NULL, 'd'}, {"pid-file", required_argument, NULL, 'p'}, + {"help", no_argument, NULL, '?'}, + {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; @@ -160,21 +162,7 @@ main(int argc, char **argv) int server_version_num = 0; progname = get_progname(argv[0]); - if (argc > 1) - { - if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) - { - help(progname); - exit(SUCCESS); - } - if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) - { - printf("%s %s (PostgreSQL %s)\n", progname, REPMGR_VERSION, PG_VERSION); - exit(SUCCESS); - } - } - - while ((c = getopt_long(argc, argv, "f:v:mdp:", long_options, &optindex)) != -1) + while ((c = getopt_long(argc, argv, "?Vf:v:mdp:", long_options, &optindex)) != -1) { switch (c) { @@ -193,6 +181,12 @@ main(int argc, char **argv) case 'p': pid_file = optarg; break; + case '?': + help(progname); + exit(SUCCESS); + case 'V': + printf("%s %s (PostgreSQL %s)\n", progname, REPMGR_VERSION, PG_VERSION); + exit(SUCCESS); default: usage(); exit(ERR_BAD_CONFIG);