Ruby OptionParser抛出ArgumentError

时间:2014-08-26 18:06:12

标签: ruby-on-rails ruby gem optionparser

这是我第一次使用OptionParser,我收到了这个错误:

  

/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'       来自/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'       来自./bin/myModel-scraper:6:in new' from ./bin/myModel-scraper:6:in'

滨:

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 /版本:

module myModel
  module Scraper
    VERSION = '0.1.2'
  end
end

我想我完全错了,有人可以解释一下这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

我通常会像这样写一个OptionParser块:

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

我可能会使用on代替on_tail

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

但我明白为什么你可能希望它出现在帮助列表的末尾。