Created
June 15, 2011 15:06
-
-
Save jjulian/1027295 to your computer and use it in GitHub Desktop.
A sample script that uses OptionParser and Logger. A great template to start with! Shown off at @bmoreonrails on June 14, 2011
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
# A sample script that uses OptionParser and Logger | |
# http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html | |
require 'optparse' | |
require 'logger' | |
# Defaults. This hash will hold all of the options | |
# parsed from the command-line by | |
# OptionParser. | |
options = { | |
:verbose => false, | |
:quick => false, | |
:logfile => nil | |
} | |
optparse = OptionParser.new do |opts| | |
# Set a banner, displayed at the top | |
# of the help screen. | |
opts.banner = "Usage: #{$0} [options] file1 file2 ..." | |
# Define the options, and what they do | |
opts.on( '-v', '--verbose', 'Output more information' ) do | |
options[:verbose] = true | |
end | |
opts.on( '-q', '--quick', 'Perform the task quickly' ) do | |
options[:quick] = true | |
end | |
opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do |file| | |
options[:logfile] = file | |
end | |
opts.on( '--color COLOR', ['red','green'], 'A silly param' ) do |color| | |
options[:color] = color | |
end | |
opts.on( '--flavor FLAVOR', {:sweet => :candy, :bitter => :lemon}, 'A silly param' ) do |food| | |
options[:food] = food | |
end | |
opts.on( '--phone num', /\d{3}\-\d{4}/, 'your digits' ) do |phone| | |
options[:phone] = phone | |
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. | |
optparse.parse! | |
def main(options, files) | |
# replace with your awesome code | |
$logger.debug "Being verbose" if options[:verbose] | |
$logger.debug "Being quick" if options[:quick] | |
$logger.debug "Logging to file #{options[:logfile]}" if options[:logfile] | |
$logger.debug "color is #{options[:color]}" if options[:color] | |
$logger.debug "food is #{options[:food]}" if options[:food] | |
$logger.debug "call you at #{options[:phone]}" if options[:phone] | |
files.each do |f| | |
$logger.debug "working on file #{f}..." | |
sleep 0.5 | |
end | |
# exit with a proper return code (0 = success, 1 = failure) | |
0 # success | |
end | |
$logger = Logger.new(options[:logfile] || STDOUT) | |
$logger.level = Logger::DEBUG | |
$logger.formatter = proc do |severity, datetime, progname, msg| | |
"#{datetime.strftime('%Y-%m-%d %H:%M:%S')} #{severity}: #{msg}\n" | |
end | |
exit main(options, ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment