简体   繁体   中英

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:in fetch' 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:in initialize' 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM