有没有办法为水晶程序做必要的论证?例如
./myprog ~/Music -r
代替
./myprog -d ~/Music -r
所以如果没有[directory]参数,我的程序就不会运行。现在使用“option_parser”,只能做-arguments。
没有办法使用option_parser
创建必需的参数,但是如果没有传递的参数,你可以解析参数并抛出错误或退出:
require "option_parser"
directory = nil
parser = OptionParser.new
parser.on("-d DIR", "Directory [required]") do |d|
directory = d
end
parser.parse ARGV
if directory.nil?
# directory argument was not set
# print help and exit
puts parser
exit 1
else
# ...
end