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