我想在另一个定义一些默认值的函数中包装OptionParser,但允许 我要覆盖横幅,添加分隔符,并添加新选项。我很难将相关信息传递给on方法。
有关如何修复第options.each { |o, p| opts.on(o, &p) }
行的任何建议吗?
def my_opts (banner: "Usage: ruby #{File::basename($0)} [options]", separators: [], options: {})
option_parser = OptionParser.new do |opts|
opts.banner = banner
separators.each {|s| opts.separator s }
opts.separator ''
opts.separator 'Options:'
opts.on('-u', '--user USER', 'Username for authentication.') { |user| @user = user }
options.each { |o, p| opts.on(o, &p) }
opts.on_tail('--help', 'Print this help message.') { puts opts; exit }
end
return option_parser
end
opts = {['-f', '--file FILE', 'File to read']=>Proc.new { |f| @f=file } }
stdopts = my_opts( options: opts)
答案 0 :(得分:0)
HA!弄清楚了!有问题的行必须是:options.each { |o,p| opts.on(*o, &p) }