]> git.saurik.com Git - redis.git/blame - utils/generate-command-help.rb
Merge branch 'unstable' into unstable-zset
[redis.git] / utils / generate-command-help.rb
CommitLineData
5397f2b5
TH
1#!/usr/bin/env ruby
2
a2a69d58
PN
3GROUPS = [
4 "generic",
5 "string",
6 "list",
7 "set",
8 "sorted_set",
9 "hash",
10 "pubsub",
11 "transactions",
12 "connection",
13 "server"
14].freeze
15
16GROUPS_BY_NAME = Hash[*
17 GROUPS.each_with_index.map do |n,i|
18 [n,i]
19 end.flatten
20].freeze
21
5397f2b5 22def argument arg
50d0e82d
PN
23 name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"]
24 name = arg["enum"].join "|" if "enum" == arg["type"]
25 name = arg["command"] + " " + name if arg["command"]
26 if arg["multiple"]
27 name = "#{name} [#{name} ...]"
28 end
29 if arg["optional"]
30 name = "[#{name}]"
5397f2b5
TH
31 end
32 name
33end
34
35def arguments command
50d0e82d
PN
36 return "-" unless command["arguments"]
37 command["arguments"].map do |arg|
5397f2b5 38 argument arg
50d0e82d
PN
39 end.join " "
40end
41
42def commands
43 return @commands if @commands
44
6418b4c7 45 require "rubygems"
50d0e82d
PN
46 require "net/http"
47 require "net/https"
48 require "json"
49 require "uri"
50
51 url = URI.parse "https://github.com/antirez/redis-doc/raw/master/commands.json"
52 client = Net::HTTP.new url.host, url.port
53 client.use_ssl = true
54 response = client.get url.path
55 if response.is_a?(Net::HTTPSuccess)
56 @commands = JSON.parse(response.body)
57 else
58 response.error!
59 end
5397f2b5
TH
60end
61
a2a69d58
PN
62def generate_groups
63 GROUPS.map do |n|
64 "\"#{n}\""
65 end.join(",\n ");
66end
67
50d0e82d
PN
68def generate_commands
69 commands.to_a.sort do |x,y|
70 x[0] <=> y[0]
71 end.map do |key, command|
a2a69d58
PN
72 group = GROUPS_BY_NAME[command["group"]]
73 if group.nil?
74 STDERR.puts "Please update groups array in #{__FILE__}"
75 raise "Unknown group #{command["group"]}"
76 end
77
78 ret = <<-SPEC
50d0e82d
PN
79{ "#{key}",
80 "#{arguments(command)}",
81 "#{command["summary"]}",
a2a69d58 82 #{group},
50d0e82d
PN
83 "#{command["since"]}" }
84 SPEC
a2a69d58
PN
85 ret.strip
86 end.join(",\n ")
50d0e82d
PN
87end
88
89# Write to stdout
a2a69d58
PN
90puts <<-HELP_H
91/* Automatically generated by #{__FILE__}, do not edit. */
92
93#ifndef __REDIS_HELP_H
94#define __REDIS_HELP_H
95
96static char *commandGroups[] = {
97 #{generate_groups}
98};
99
100struct commandHelp {
101 char *name;
102 char *params;
103 char *summary;
104 int group;
105 char *since;
106} commandHelp[] = {
107 #{generate_commands}
108};
109
110#endif
111HELP_H
50d0e82d 112