Skip to content

Instantly share code, notes, and snippets.

@jasonwbarnett
Created August 6, 2013 22:04
Show Gist options
  • Save jasonwbarnett/6169159 to your computer and use it in GitHub Desktop.
Save jasonwbarnett/6169159 to your computer and use it in GitHub Desktop.
copy and paste me when you need something like getopts in ruby.
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options]"
opts.on("-d", "--debug",
"Enables some helpful debugging output."
) { |value| options[:debug] = true }
opts.on("-f", "--file FILE",
"The server.xml file you would like to load."
) { |value| options[:file] = value }
opts.on("-o", "--old",
"Generate using the old formatting."
) { |value| options[:old] = true }
opts.on("-h", "--help", "Display this help message.") do
puts opts
exit
end
end
begin
optparse.parse!
rescue OptionParser::MissingArgument
puts "You're missing an argument...\n\n"
puts optparse
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment