Created
February 16, 2012 12:13
-
-
Save mipmip/1844428 to your computer and use it in GitHub Desktop.
Ruby cli script with options and arguments example
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
#!/usr/bin/env ruby | |
# A script that will pretend to resize a number of images | |
require 'optparse' | |
# This hash will hold all of the options | |
# parsed from the command-line by | |
# OptionParser. | |
options = {} | |
optparse = OptionParser.new do|opts| | |
# Set a banner, displayed at the top | |
# of the help screen. | |
opts.banner = "Usage: optparse1.rb [options] file1 file2 ..." | |
# Define the options, and what they do | |
options[:verbose] = false | |
opts.on( '-v', '--verbose', 'Output more information' ) do | |
options[:verbose] = true | |
end | |
options[:quick] = false | |
opts.on( '-q', '--quick', 'Perform the task quickly' ) do | |
options[:quick] = true | |
end | |
options[:logfile] = nil | |
opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file| | |
options[:logfile] = file | |
end | |
# 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 | |
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! | |
puts "Being verbose" if options[:verbose] | |
puts "Being quick" if options[:quick] | |
puts "Logging to file #{options[:logfile]}" if options[:logfile] | |
ARGV.each do|f| | |
puts "Resizing image #{f}..." | |
sleep 0.5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment