Created
August 6, 2013 22:04
-
-
Save jasonwbarnett/6169159 to your computer and use it in GitHub Desktop.
copy and paste me when you need something like getopts in ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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