lib/redis-stat/option.rb in redis-stat-0.1.4 vs lib/redis-stat/option.rb in redis-stat-0.2.0
- old
+ new
@@ -1,23 +1,22 @@
require 'optparse'
class RedisStat
module Option
DEFAULT = {
- :host => '127.0.0.1',
- :port => 6379,
+ :hosts => ['127.0.0.1:6379'],
:interval => 2,
:count => nil,
:csv => nil
}
def self.parse argv
argv = argv.dup
options = DEFAULT.dup
opts = ::OptionParser.new { |opts|
- opts.banner = "usage: redis-stat [HOST[:PORT]] [INTERVAL [COUNT]]"
+ opts.banner = "usage: redis-stat [HOST[:PORT] ...] [INTERVAL [COUNT]]"
opts.separator ''
opts.on('--csv=OUTPUT_CSV_FILE_PATH', 'Save the result in CSV format') do |v|
options[:csv] = v
end
@@ -39,37 +38,17 @@
begin
opts.parse! argv
is_number = lambda { |str| str =~ /^([0-9]\.?[0-9]*)$|^([1-9][0-9]*)$/ }
- set_options = lambda { |host_port, interval, count|
- if host_port
- host, port = host_port.split(':')
- options[:host] = host
- options[:port] = port.to_i if port
- end
- options[:interval] = interval.to_f if interval
- options[:count] = count.to_i if count
- }
+ numbers, hosts = argv.partition { |e| is_number.call e }
+ interval, count = numbers.map(&:to_f)
- case argv.length
- when 1
- if is_number.call argv.first
- set_options.call nil, argv.first, nil
- else
- set_options.call argv.first, nil, nil
- end
- when 2
- if is_number.call argv.first
- set_options.call nil, argv.first, argv.last
- else
- set_options.call argv.first, argv.last, nil
- end
- when 3
- set_options.call *argv
- end
+ options[:interval] = interval if interval
+ options[:count] = count if count
+ options[:hosts] = hosts unless hosts.empty?
validate options
return options
rescue SystemExit => e
@@ -90,12 +69,22 @@
count = options[:count]
unless count.nil? || (count.is_a?(Numeric) && count > 0)
raise ArgumentError.new("Invalid count: #{count}")
end
- port = options[:port]
- unless port.is_a?(Fixnum) && port > 0 && port < 65536
- raise ArgumentError.new("Invalid port: #{port}")
+ hosts = options[:hosts]
+ if hosts.empty?
+ raise ArgumentError.new("Redis host not given")
+ end
+
+ hosts.each do |host|
+ host, port = host.split(':')
+ if port
+ port = port.to_i
+ unless port > 0 && port < 65536
+ raise ArgumentError.new("Invalid port: #{port}")
+ end
+ end
end
end
end
end