Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm going to answer by recommending looking at the new-ish "<a href="https://github.com/injekt/slop" rel="nofollow noreferrer">slop</a>" gem. It's a wrapper around <code>getoptlong</code>. </p> <p>You can use <code>gem install slop</code> if you're using RVM, or <code>sudo gem install slop</code> otherwise.</p> <p>GetOptLong is very powerful but, though I've used it several times, I still have to go review the docs each time. </p> <p>If you want a bit more power, with an "easier to use interface than GetOptLong", look into Ruby's <a href="http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html" rel="nofollow noreferrer"><code>OptionParser</code></a>. You'll need to work out the logic better, but this is a quick pass converting your code. I had to stub out a class for the CommonLog gem because I don't use it. The important stuff follows the line pulling log from <code>ARGV</code>:</p> <pre><code>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! </code></pre> <p>Also, as a quick critique, you are not writing idiomatic Ruby code:</p> <ul> <li><code>when</code> statements can be written: <code>when "-h", "--help"</code></li> <li><code>if ARGV[0] == nil || ARGV.size != 1</code> is convoluted. Study up on how ARGV and arrays work. Normally, for <code>ARGV[0]</code> to be nil there will be no more arguments, so <code>ARGV.empty?</code> would probably suffice.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload