]> git.saurik.com Git - redis.git/blob - utils/help.h
dc7f270d9f4d9826cbfbdfe4bbf4abcc36736e20
[redis.git] / utils / help.h
1
2 #include <stdio.h>
3 #include <string.h>
4
5 /*
6 * List command groups.
7 */
8
9 #define GROUPS \
10 G(UNKNOWN, "unknown") \
11 G(SET, "set") \
12 G(LIST, "list") \
13 G(HASH, "hash") \
14 G(GENERIC, "generic") \
15 G(PUBSUB, "pubsub") \
16 G(STRING, "string") \
17 G(SERVER, "server") \
18 G(CONNECTION, "connection") \
19 G(TRANSACTIONS, "transactions") \
20 G(SORTED_SET, "sorted_set")
21
22 /*
23 * Command group types.
24 */
25
26 typedef enum {
27 #define G(GROUP, _) COMMAND_GROUP_##GROUP,
28 GROUPS
29 #undef G
30 COMMAND_GROUP_LENGTH
31 } command_group_type_t;
32
33 /*
34 * Command group type names.
35 */
36
37 static char *command_group_type_names[] = {
38 #define G(_, STR) STR,
39 GROUPS
40 #undef G
41 };
42
43 /*
44 * Command help struct.
45 */
46
47 struct command_help {
48 char *name;
49 char *params;
50 char *summary;
51 command_group_type_t group;
52 char *since;
53 } command_help[] = {
54 __COMMANDS__
55 };
56
57 /*
58 * Output command help to stdout.
59 */
60
61 static void
62 output_command_help(struct command_help *help) {
63 printf("\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\n", help->name, help->params);
64 printf(" \x1b[33msummary:\x1b[0m %s\n", help->summary);
65 printf(" \x1b[33msince:\x1b[0m %s\n", help->since);
66 printf(" \x1b[33mgroup:\x1b[0m %s\n", command_group_type_names[help->group]);
67 }
68
69 /*
70 * Return command group type by name string.
71 */
72
73 static command_group_type_t
74 command_group_type_by_name(const char *name) {
75 for (int i = 0; i < COMMAND_GROUP_LENGTH; ++i) {
76 const char *group = command_group_type_names[i];
77 if (0 == strcasecmp(name, group)) return i;
78 }
79 return 0;
80 }
81
82 /*
83 * Output group names.
84 */
85
86 static void
87 output_group_help() {
88 for (int i = 0; i < COMMAND_GROUP_LENGTH; ++i) {
89 if (COMMAND_GROUP_UNKNOWN == i) continue;
90 const char *group = command_group_type_names[i];
91 printf(" \x1b[90m-\x1b[0m %s\n", group);
92 }
93 }
94
95 /*
96 * Output all command help, filtering by group or command name.
97 */
98
99 static void
100 output_help(int argc, const char **argv) {
101 int len = sizeof(command_help) / sizeof(struct command_help);
102
103 if (argc && 0 == strcasecmp("groups", argv[0])) {
104 output_group_help();
105 return;
106 }
107
108 command_group_type_t group = argc
109 ? command_group_type_by_name(argv[0])
110 : COMMAND_GROUP_UNKNOWN;
111
112 for (int i = 0; i < len; ++i) {
113 struct command_help help = command_help[i];
114 if (argc && !group && 0 != strcasecmp(help.name, argv[0])) continue;
115 if (group && group != help.group) continue;
116 output_command_help(&help);
117 }
118 puts("");
119 }