I'm not sure where that syntax for OptionParser came, and Trollop is a
fine lib, but if you want to stick with just Ruby standard library
stuff:
require 'optparse'
options = {}
opts = OptionParser.new do |opts|
opts.on('-s SOURCE_PATH','Source path') do |path|
options[:source_path] = path
end
opts.on('-d DEST_PATH','Destination path') do |path|
option[:dest_path] = path
end
opts.on('-v','Be verbose') do
options[:verbose] = true
end
end
opts.parse!
puts "Source Path is: #{options[:source_path]"
puts "Dest Path is: #{options[:dest_path]"
puts "We are being verbose" if options[:verbose]
It's a bit more code, but you also get built-in help:
$ my_app --help
Usage: my_app [options]
-s SOURCE_PATH Source path
-d DEST_PATH Dest path
-v Be verbose
You can use long-form options as well (e.g. --verbose)
--
Posted via http://www.ruby-forum.com/.