Created
September 29, 2017 12:32
-
-
Save jjuarez/640397cfa20c6bac5d695b8ff5bc4e49 to your computer and use it in GitHub Desktop.
An example of OptionParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
class LogParser | |
def self.parse(log_file, index) | |
data = [] | |
File.open(log_file).each do |line| | |
t = line.split(' ')[index] | |
data.push(t) if t | |
end | |
data.sort | |
end | |
def initialize(log_file, index) | |
@times = [] | |
@times = LogParser.parse(log_file, index) | |
self | |
end | |
def percentile(p) | |
(@times.size * p / 100) - 0.5 | |
end | |
end | |
class Cli | |
def self.run(arguments) | |
options = {} | |
OptionParser.new do |o| | |
o.on("-fFILE", "--logfile=FILE", "Log file") { |f| options[:log_file] = f } | |
o.on("-iINDEX", "--index=INDEX", "Index of the field") { |i| options[:index] = i.to_i } | |
end.parse! | |
puts LogParser.new(options[:log_file], options[:index]).percentile(70) if options[:log_file] && options[:index] | |
rescue Exception => e | |
$stderr.puts e.message | |
end | |
end | |
# ::main:: | |
Cli.run(ARGV) if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment