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