passing blocks to be used in OptionParser on method

68 views Asked by At

I want to wrap OptionParser in another function that defines some defaults, but allows me to overwrite the banner, add separators, and add new options. I am having a hard time passing the relevant information to the on method.

Any advice on how to fix the line 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) 
1

There are 1 answers

0
mr paul On

HA! figured it out! the line in question needs to be: options.each { |o,p| opts.on(*o, &p) }