我有一个创建邮件的程序,我想做的是当给出
-t
标志且未给出标志参数时,默认使用某些内容,而不是输出通常的错误信息:<main>': missing argument: -t (OptionParser::MissingArgument)
所以我的问题是,如果我有这个标志:require 'optparse'
OPTIONS = {}
OptionParser.new do |opts|
opts.on('-t INPUT', '--type INPUT', 'Specify who to say hello to'){ |o| OPTIONS[:type] = o }
end.parse!
def say_hello
puts "Hello #{OPTIONS[:type]}"
end
case
when OPTIONS[:type]
say_hello
else
puts "Hello World"
end
我没有输入必要的参数INPUT
,程序该如何输出Hello World
而不是<main>': missing argument: -t (OptionParser::MissingArgument)
?
示例:
C:\Users\bin\ruby\test_folder>ruby opt.rb -t hello
Hello hello
C:\Users\bin\ruby\test_folder>ruby opt.rb -t
opt.rb:7:in `<main>': missing argument: -t (OptionParser::MissingArgument)
C:\Users\bin\ruby\test_folder>