I am going to answer, recommending a look at the new β slop β gem. βThis is a wrapper around getoptlong .
You can use gem install slop if you use RVM or sudo gem install slop otherwise.
GetOptLong is very powerful, but although I have used it several times, I still need to view documents every time.
If you want a little more power, with a βsimpler to use interface than GetOptLong,β take a look at the Ruby OptionParser . You will need to better develop the logic, but this is a quick transition transforming your code. I had to stub the class for the CommonLog stone, because I do not use it. Important material follows the line pull log from ARGV :
require 'optparse' class CommonLog def initialize(*args); end def urlReport(); puts "running urlReport()"; end def ipReport(); puts "running ipReport()"; end def statReport(arg); puts "running statReport(#{arg})"; end end log = CommonLog.new(ARGV[0]) OptionParser.new { |opts| opts.banner = "Usage: #{File.basename($0)} -u -i -s filename" opts.on( '-u', '--[no-]url', 'some short text describing URL') do log.urlReport() end opts.on('-i', '--[no-]ip', 'some short text describing IP') do log.ipReport() end opts.on('-s', '--stat FILENAME', 'some short text describing STAT') do |arg| log.statReport(arg) end }.parse!
Also, as a quick criticism, you are not writing Ruby's idiomatic code:
- Operators
when can be written: when "-h", "--help"if ARGV[0] == nil || ARGV.size != 1 if ARGV[0] == nil || ARGV.size != 1 collapsed. Learn how ARGV and arrays work. Usually for ARGV[0] for nil there will be no more arguments, therefore ARGV.empty? likely to be sufficient.
source share