14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/redis-stat/option.rb', line 14
def self.parse argv
argv = argv.reject { |e| e == '-h' }
options = DEFAULT.dup
op = ::OptionParser.new { |opts|
opts.banner = "usage: redis-stat [HOST[:PORT] ...] [INTERVAL [COUNT]]"
opts.separator ''
opts.on('-a', '--auth=PASSWORD', 'Password') do |v|
options[:auth] = v
end
opts.on('-v', '--verbose', 'Show more info') do |v|
options[:verbose] = v
end
opts.on('--style=STYLE', 'Output style: unicode|ascii') do |v|
options[:style] = v.downcase.to_sym
end
opts.on('--no-color', 'Suppress ANSI color codes') do |v|
options[:mono] = true
end
opts.on('--csv[=CSV_FILE]', 'Print or save the result in CSV') do |v|
if v
options[:csv_file] = v
else
options[:csv_output] = true
end
end
opts.on('--es=ELASTICSEARCH_URL', 'Send results to ElasticSearch: [http://]HOST[:PORT][/INDEX]') do |v|
options[:es] = RedisStat::ElasticsearchSink.parse_url v
end
opts.separator ''
opts.on('--server[=PORT]', "Launch redis-stat web server (default port: #{RedisStat::DEFAULT_SERVER_PORT})") do |v|
options[:server_port] = v || RedisStat::DEFAULT_SERVER_PORT
end
opts.on('--daemon', "Daemonize redis-stat. Must be used with --server option.") do |v|
options[:daemon] = true
if RUBY_PLATFORM == 'java'
raise ArgumentError.new("Sorry. Daemonization is not supported in JRuby.")
end
end
opts.separator ''
opts.on('--version', 'Show version') do
puts RedisStat::VERSION
exit 0
end
opts.on_tail('--help', 'Show this message') do
puts opts
exit 0
end
}
begin
op.parse! argv
is_number = lambda { |str| str =~ /^([0-9]\.?[0-9]*)$|^([1-9][0-9]*)$/ }
numbers, hosts = argv.partition { |e| is_number.call e }
interval, count = numbers.map(&:to_f)
options[:interval] = interval if interval
options[:count] = count if count
options[:hosts] = hosts unless hosts.empty?
validate options
return options
rescue SystemExit => e
exit e.status
rescue Exception => e
puts e.to_s
puts op
exit 1
end
end
|