TuxedoTomson
TuxedoTomson

Reputation: 483

Ruby OptionParser throwing ArgumentError

this is my first time using OptionParser and i'm getting this error:

/Users/jay/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mechanize-> 2.7.3/lib/mechanize/http/agent.rb:651:in resolve': absolute URL needed (not -v) (ArgumentError)from /Users/jay/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:223:infetch' from /Users/jay/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/mechanize-2.7.3/lib/mechanize.rb:440:in get' from /Users/jay/Code/linkedin-scraper/lib/myModel-scraper/profile.rb:20:ininitialize' from ./bin/myModel-scraper:6:in new' from ./bin/myModel-scraper:6:in'

Bin:

require 'rubygems'
require 'optparse'

opts = OptionParser.new do |opts|
 opts.on_tail("-v", "--version", "Show version") { puts myModel::Scraper::VERSION; exit }
 opts.parse!
end

Lib/version:

module myModel
  module Scraper
    VERSION = '0.1.2'
  end
end

I guess i'm doing this completely wrong, could somebody explain whats happening here?

Upvotes: 0

Views: 186

Answers (1)

the Tin Man
the Tin Man

Reputation: 160581

I'd usually write an OptionParser block like this:

OptionParser.new do |opts|
  opts.on_tail("-v", "--version", "Show version") { puts myModel::Scraper::VERSION; exit }
end.parse!

I'd probably use on instead of on_tail:

opts.on("-v", "--version", "Show version") { puts myModel::Scraper::VERSION; exit }

but I understand why you might want it to appear at the end of the help list.

Upvotes: 0

Related Questions