我正在学习使用Ruby的OptionParser类。如何提高解析器错误消息的质量?以下是带有强制选项的标志示例,该选项必须是hour
,day
,week
或month
之一。修改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
我试过了。异常不会抛出,直到opt_parser.parse!被称为,以便开始......救援块没有帮助。 – everett1992
@ everett1992所以解救周围!呼叫。 – mcfinnigan
然后,你将不得不字符串搜索异常消息,以找到哪个标志是无效的(我的实际代码有多个标志,这个例子只是为了简洁)。这是一个选项,但似乎kludgey。我希望错误消息在选项解析器中是常用的,我很惊讶他们不被支持。 – everett1992