]> git.saurik.com Git - redis.git/blobdiff - src/redis-cli.c
Refactor help-related code into redis-cli.c
[redis.git] / src / redis-cli.c
index fec0fce408b9e1b84e5e7f2eafe9eaec47b9874f..865f093135550fdd8b7c36fe4214e87e90af95cd 100644 (file)
@@ -44,6 +44,7 @@
 #include "sds.h"
 #include "zmalloc.h"
 #include "linenoise.h"
+#include "help.h"
 
 #define REDIS_NOTUSED(V) ((void) V)
 
@@ -67,6 +68,7 @@ static struct config {
 } config;
 
 static void usage();
+char *redisGitSHA1(void);
 
 /*------------------------------------------------------------------------------
  * Utility functions
@@ -82,6 +84,65 @@ static long long mstime(void) {
     return mst;
 }
 
+/*------------------------------------------------------------------------------
+ * Help functions
+ *--------------------------------------------------------------------------- */
+
+/* Output command help to stdout. */
+static void outputCommandHelp(struct commandHelp *help) {
+    printf("\n  \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\n", help->name, help->params);
+    printf("  \x1b[33msummary:\x1b[0m %s\n", help->summary);
+    printf("  \x1b[33msince:\x1b[0m %s\n", help->since);
+    printf("  \x1b[33mgroup:\x1b[0m %s\n", commandGroups[help->group]);
+}
+
+/* Return command group type by name string. */
+static int commandGroupIndex(const char *name) {
+    int i, len = sizeof(commandGroups)/sizeof(char*);
+    for (i = 0; i < len; i++)
+        if (strcasecmp(name, commandGroups[i]) == 0)
+            return i;
+    return -1;
+}
+
+/* Output group names. */
+static void outputGroupHelp() {
+    int i, len = sizeof(commandGroups)/sizeof(char*);
+    for (i = 0; i < len; i++)
+        printf("  \x1b[90m-\x1b[0m %s\n", commandGroups[i]);
+}
+
+/* Output all command help, filtering by group or command name. */
+static void outputHelp(int argc, char **argv) {
+    int i, len = sizeof(commandHelp) / sizeof(struct commandHelp);
+    int group;
+    struct commandHelp *help;
+
+    if (argc && strcasecmp("groups", argv[0]) == 0) {
+        outputGroupHelp();
+        return;
+    }
+
+    group = argc ? commandGroupIndex(argv[0]) : -1;
+    for (i = 0; i < len; i++) {
+        help = &commandHelp[i];
+        if (group == -1) {
+            if (argc) {
+                if (strcasecmp(help->name, argv[0]) == 0) {
+                    outputCommandHelp(help);
+                }
+            } else {
+                outputCommandHelp(help);
+            }
+        } else {
+            if (group == help->group) {
+                outputCommandHelp(help);
+            }
+        }
+    }
+    puts("");
+}
+
 /*------------------------------------------------------------------------------
  * Networking / parsing
  *--------------------------------------------------------------------------- */
@@ -251,22 +312,6 @@ static int cliReadReply() {
     return REDIS_OK;
 }
 
-static void showInteractiveHelp(void) {
-    printf(
-    "\n"
-    "Welcome to redis-cli " REDIS_VERSION "!\n"
-    "Just type any valid Redis command to see a pretty printed output.\n"
-    "\n"
-    "It is possible to quote strings, like in:\n"
-    "  set \"my key\" \"some string \\xff\\n\"\n"
-    "\n"
-    "You can find a list of valid Redis commands at\n"
-    "  http://code.google.com/p/redis/wiki/CommandReference\n"
-    "\n"
-    "Note: redis-cli supports line editing, use up/down arrows for history."
-    "\n\n");
-}
-
 static int cliSendCommand(int argc, char **argv, int repeat) {
     char *command = argv[0];
     size_t *argvlen;
@@ -274,7 +319,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
 
     config.raw_output = !strcasecmp(command,"info");
     if (!strcasecmp(command,"help")) {
-        showInteractiveHelp();
+        outputHelp(--argc, ++argv);
         return REDIS_OK;
     }
     if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
@@ -291,6 +336,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
         redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
         while (config.monitor_mode) {
             if (cliReadReply() != REDIS_OK) exit(1);
+            fflush(stdout);
         }
 
         if (config.pubsub_mode) {
@@ -350,7 +396,7 @@ static int parseOptions(int argc, char **argv) {
 "automatically used as last argument.\n"
             );
         } else if (!strcmp(argv[i],"-v")) {
-            printf("redis-cli shipped with Redis version %s\n", REDIS_VERSION);
+            printf("redis-cli shipped with Redis version %s (%s)\n", REDIS_VERSION, redisGitSHA1());
             exit(0);
         } else {
             break;