2014-01-30 48 views
1

我正在学习使用Ruby的OptionParser类。如何提高解析器错误消息的质量?以下是带有强制选项的标志示例,该选项必须是hour,day,weekmonth之一。修改Ruby OptionParser错误消息

opt_parser = OptionParser.new do |opts| 
    opts.banner = "Usage: #{$0} [options] username" 

    times = [:hour, :day, :week, :month] 
    opts.on('-t', '--time=TIME', times, 
      'Show messages from the last TIME (defaults to weeks)', "Avaliable options are (#{times.join(', ')})") do |time| 
    o.time = time 
    end 
end 

以下是一些示例输出。

$ ./script -t 
./scraper.rb:195:in `main': missing argument: -t (OptionParser::MissingArgument) 
from ./scraper.rb:210:in `<main>' 

$ ./script -t not_a_value 
./scraper.rb:195:in `main': invalid argument: -t not_a_value (OptionParser::InvalidArgument) 
from ./scraper.rb:210:in `<main>' 

我想错误提可接受值,像invalid option for -t 'not_a_value', valid options are hour, day, week, month

回答

0

当然这很简单,只要:

opt_parser = OptionParser.new do |opts| 
    opts.banner = "Usage: #{$0} [options] username" 

    times = [:hour, :day, :week, :month] 
    begin 
    opts.on('-t', '--time=TIME', times, 
     'Show messages from the last TIME (defaults to weeks)', "Avaliable options are (# {times.join(', ')})") do |time| 
    o.time = time 
    rescue OptionParser::MissingArgument, OptionParser::InvalidArgument 
     $stderr.print "Usage: -t <argument> where argument in [:hour, :day, :week, :month]" 
    end 
    end 
end 
+0

我试过了。异常不会抛出,直到opt_parser.parse!被称为,以便开始......救援块没有帮助。 – everett1992

+0

@ everett1992所以解救周围!呼叫。 – mcfinnigan

+1

然后,你将不得不字符串搜索异常消息,以找到哪个标志是无效的(我的实际代码有多个标志,这个例子只是为了简洁)。这是一个选项,但似乎kludgey。我希望错误消息在选项解析器中是常用的,我很惊讶他们不被支持。 – everett1992

0

OptionParser并不能真正帮助你多少与此,但你可以自己实现而不会有太多麻烦,并且仍然是干的。只需检查自己的正确性,并在需要时发出错误。

times = [:hour, :day, :week, :month] 
opts.on('-t', '--time=TIME', 
    'Show messages from the last TIME (defaults to weeks)', 
    "Available options are <#{times.join ', '}>") do |time| 
    times.include?(time.to_sym) or 
    raise OptionParser::ParseError.new("time must be one of <#{times.join ', '}>") 
    o.time = time 
end 

这也是很好的使输出干净了一点:

begin 
    p.parse!(ARGV) 
    rescue OptionParser::ParseError => e 
    puts e 
    exit 1 
    end