]> git.saurik.com Git - redis.git/blame - utils/generate-command-help.rb
Update help.h generator script to output man-style argument list
[redis.git] / utils / generate-command-help.rb
CommitLineData
5397f2b5
TH
1#!/usr/bin/env ruby
2
5397f2b5 3def argument arg
50d0e82d
PN
4 name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"]
5 name = arg["enum"].join "|" if "enum" == arg["type"]
6 name = arg["command"] + " " + name if arg["command"]
7 if arg["multiple"]
8 name = "#{name} [#{name} ...]"
9 end
10 if arg["optional"]
11 name = "[#{name}]"
5397f2b5
TH
12 end
13 name
14end
15
16def arguments command
50d0e82d
PN
17 return "-" unless command["arguments"]
18 command["arguments"].map do |arg|
5397f2b5 19 argument arg
50d0e82d
PN
20 end.join " "
21end
22
23def commands
24 return @commands if @commands
25
26 require "net/http"
27 require "net/https"
28 require "json"
29 require "uri"
30
31 url = URI.parse "https://github.com/antirez/redis-doc/raw/master/commands.json"
32 client = Net::HTTP.new url.host, url.port
33 client.use_ssl = true
34 response = client.get url.path
35 if response.is_a?(Net::HTTPSuccess)
36 @commands = JSON.parse(response.body)
37 else
38 response.error!
39 end
5397f2b5
TH
40end
41
50d0e82d
PN
42def generate_commands
43 commands.to_a.sort do |x,y|
44 x[0] <=> y[0]
45 end.map do |key, command|
46 <<-SPEC
47{ "#{key}",
48 "#{arguments(command)}",
49 "#{command["summary"]}",
50 COMMAND_GROUP_#{command["group"].upcase},
51 "#{command["since"]}" }
52 SPEC
53 end.join(", ")
54end
55
56# Write to stdout
57tmpl = File.read "./utils/help.h"
58puts "\n// Auto-generated, do not edit.\n" + tmpl.sub("__COMMANDS__", generate_commands)
59