From 1419a6e2943fd59099c2e60621dae507a51eda91 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja.osipov@gmail.com>
Date: Mon, 20 Dec 2010 20:32:42 +0300
Subject: [PATCH] Bug#684700 -h and --help options are broken

Post-review fixes: realign help text to match
exactly GNU tools (21st column), reorder help
messages to provide most relvant options first,
edit help text, print values for default options.
---
 core/tarantool.c        | 42 +++++++++++++++++++++++++----------------
 third_party/gopt/gopt.c | 10 +++++++---
 2 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/core/tarantool.c b/core/tarantool.c
index c5e92507d5..acfd97bb95 100644
--- a/core/tarantool.c
+++ b/core/tarantool.c
@@ -54,7 +54,8 @@
 #include <tarantool_version.h>
 
 static pid_t master_pid;
-const char *cfg_filename = "tarantool.cfg";
+#define DEFAULT_CFG_FILENAME "tarantool.cfg"
+const char *cfg_filename = DEFAULT_CFG_FILENAME;
 struct tarantool_cfg cfg;
 
 bool init_storage;
@@ -237,35 +238,44 @@ main(int argc, char **argv)
 	palloc_init();
 
 	const void *opt_def =
-		gopt_start(gopt_option('h', 0, gopt_shorts('h', '?'), gopt_longs("help"),
-				       NULL, "display this help"),
-			   gopt_option('c', GOPT_ARG, gopt_shorts('c'), gopt_longs("config"),
-				       "=<filename>", "path to config file"),
-			   gopt_option('v', 0, gopt_shorts('V'), gopt_longs("version"),
-				       NULL, "print version"),
+		gopt_start(
+			   gopt_option('g', GOPT_ARG, gopt_shorts(0),
+						   gopt_longs("cfg-get", "cfg_get"),
+						   "=KEY", "return a value from configuration file described by KEY"),
+			   gopt_option('c', GOPT_ARG, gopt_shorts('c'),
+						   gopt_longs("config"),
+						   "=FILE", "path to configuration file (default: " DEFAULT_CFG_FILENAME ")"),
 #ifdef STORAGE
 			   gopt_option('C', 0, gopt_shorts(0), gopt_longs("cat"),
-				       "=<filename>", "cat file to stdout in readable format"),
-			   gopt_option('I', 0, gopt_shorts(0), gopt_longs("init_storage", "init-storage"),
-				       NULL, "initialize storage"),
+						   "=FILE", "cat snapshot file to stdout in readable format and exit"),
+			   gopt_option('I', 0, gopt_shorts(0),
+						   gopt_longs("init-storage", "init_storage"),
+						   NULL, "initialize storage (an empty snapshot file) and exit"),
 #endif
 			   gopt_option('v', 0, gopt_shorts('v'), gopt_longs("verbose"),
-				       NULL, "increase log level"),
+						   NULL, "increase verbosity level in log messages"),
 			   gopt_option('D', 0, gopt_shorts('D'), gopt_longs("daemonize"),
-				       NULL, "daemonize"),
-			   gopt_option('g', GOPT_ARG, gopt_shorts(0), gopt_longs("cfg_get", "cfg-get"),
-				       "=<key>", "return value from config described by key"));
+						   NULL, "redirect input/output streams to a log file and run as daemon"),
+               gopt_option('h', 0, gopt_shorts('h', '?'), gopt_longs("help"),
+						   NULL, "display this help and exit"),
+			   gopt_option('V', 0, gopt_shorts('V'), gopt_longs("version"),
+						   NULL, "print program version and exit"));
 
 	void *opt = gopt_sort(&argc, (const char **)argv, opt_def);
 
-	if (gopt(opt, 'V')){
+	if (gopt(opt, 'v')){
 		puts(tarantool_version());
 		return 0;
 	}
 
 	if (gopt(opt, 'h')) {
-		puts("usage:");
+		puts("Tarantool -- an efficient in-memory data store.");
+		printf("Usage: %s [OPTIONS]\n", basename(argv[0]));
+		puts("");
 		gopt_help(opt_def);
+		puts("");
+		puts("Please visit project home page at http://launchpad.net/tarantool");
+		puts("to see online documentation, submit bugs or contribute a patch.");
 		return 0;
 	}
 
diff --git a/third_party/gopt/gopt.c b/third_party/gopt/gopt.c
index ff16a79de3..5fa9960043 100644
--- a/third_party/gopt/gopt.c
+++ b/third_party/gopt/gopt.c
@@ -268,10 +268,13 @@ void gopt_free( void *vptr_opts ){
 
 void gopt_help(const void *opt_def){
   const struct opt_spec_s *opt = opt_def;
+  /* -h, --help          print help */
+  /*                    ^ this is HELP_MSG_OFFSET */
+  const int HELP_MSG_OFFSET = 21;
   while (opt->key) {
     const char *shorts = opt->shorts;
     char has_shorts = 0;
-    printf("\t");
+    printf("  ");
     if (*shorts) {
       has_shorts = 1;
       printf("-%c", *shorts++);
@@ -285,9 +288,10 @@ void gopt_help(const void *opt_def){
       else
 	printf("  ");
       if (opt->help_arg)
-	printf("--%s%-*s", *longs, 25 - strlen(*longs), opt->help_arg);
+	printf("--%s%-*s", *longs, HELP_MSG_OFFSET - strlen(*longs),
+               opt->help_arg);
       else
-	printf("--%-*s", 25, *longs);
+	printf("--%-*s", HELP_MSG_OFFSET, *longs);
     }
     if (opt->help)
       puts(opt->help);
-- 
GitLab