Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Created December 7, 2009 22:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mrrooijen/251200 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'pp'
# This hash will hold all of the options
# parsed from the command-line by
# OptionParser.
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: executable [OPTIONS]"
# This displays the help screen, all programs are
# assumed to have this option.
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
options[:simple] = false
opts.on( '-s', '--simple', "Simple argument" ) do
options[:simple] = true
end
options[:mand] = ""
opts.on( '-m', '--mandatory FILE', "Mandatory argument" ) do|f|
options[:mand] = f
end
options[:opt] = false
opts.on( '-o', '--optional [OPT]', "Optional argument" ) do|f|
options[:opt] = f || "nothing"
end
options[:float] = 0.0
opts.on( '-f', '--float NUM', Float, "Convert to float" ) do|f|
options[:float] = f
end
options[:list] = []
opts.on( '-l', '--list a,b,c', Array, "List of parameters" ) do |l|
options[:list] = l
end
options[:set] = :yes
opts.on( '-a', '--set OPT', [:yes, :no, :maybe], "Parameters from a set" ) do|s|
options[:set] = s
end
end
# Parse the command-line. Remember there are two forms
# of the parse method. The 'parse' method simply parses
# ARGV, while the 'parse!' method parses ARGV and removes
# any options found there, as well as any parameters for
# the options. What's left is the list of files to resize.
optparse.parse!
pp "Options:", options
pp "ARGV:", ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment