1

I'm trying to figure out how to define subparameters for one of my parameters. This is what I have that is NOT working:

require 'optparse'

options = {}
OptionParser.new do |parser|
  parser.on("-r", "--require LIBRARY", "Require the LIBRARY before executing your script") do |lib|
    parser.make_switch(["-p"], '--pop THING') do |o|
         puts "You required #{o}!"
    end
 end
 parser.on("-f", '--file FILE', 'File to be processed') do |file|
    puts "This is the file: #{file}"
 end
end.parse!

I'd like to do:

 ruby myapp -r Library -p thing #<--required params

or

 ruby myapp -f
ed_is_my_name
  • 601
  • 3
  • 9
  • 24
  • 1
    Make `-p` a normal parameter and then check after parsing that `-p` and `-r` were passed together or not at all. – Max Mar 20 '18 at 14:15
  • @Max - That might be an okay solution...as long as no other argument takes a '-p' switch. – ed_is_my_name Mar 20 '18 at 14:18
  • 1
    The same principle applies. There's no rule saying you can only use optparse to handle arguments. Once it's done you can do whatever kind of post-processing you want to handle more complex cases. – Max Mar 20 '18 at 14:24

0 Answers0