Permalink
Browse files

move from ad hoc cli interface to standard unix with optparse

  • Loading branch information...
1 parent 38f067e commit b886020d9a7e2e5ac04c0944e48a896c392499b2 @kingink committed Dec 25, 2011
Showing with 42 additions and 24 deletions.
  1. +0 −1 .gitignore
  2. +42 −23 bibimbap.rb
View
@@ -1,3 +1,2 @@
-.gitignore
.DS_Store
*.swp
View
@@ -1,42 +1,61 @@
require 'rubygems'
require 'rmagick'
+require 'optparse'
+
include Magick
class Bibimbap
def boom
- unless ARGV[0]
- puts "\n\n\nYou need to specify a filename: bibimbap.rb <filename> [size,size,size] [effect]\nSizes are optional comma separated list. If no sizes are specified, one image will be generated of 400 size\nEffect is optional, available effects: vignette, negate, spread, oil_paint, emboss.\n\n\n"
- exit
+ options = {}
+ option_parser = OptionParser.new do |opts|
+ opts.banner = "Usage: bibimbap.rb [options] file1"
+ opts.on("-s [SIZE]", "--size [SIZE]", 'Some description here') do |size|
+ unless size.nil?
+ options[:size] = size
+ else
+ options[:size] = '400'
+ end
+ end
+ opts.on("-e [EFFECT]", "--effect [EFFECT]", 'Effect name [vignette|emboss|negate|oil_paint|spread]') do |effect|
+ unless effect.nil?
+ options[:effect] = effect
+ else
+ options[:effect] = nil
+ end
+ end
+ opts.on( '-h', '--help', 'Display this screen' ) do
+ puts opts
+ exit
+ end
+ if ARGV.empty?
+ puts opts
+ exit
+ end
end
-
+
+ option_parser.parse!
+ puts options.inspect
+
img = Magick::Image.read(ARGV[0]).first
width = nil
height = nil
- unless ARGV[1].nil?
- sizes = ARGV[1].split(',')
- else
- sizes = '400'
+ img.change_geometry("#{options[:size]}x#{options[:size]}") do |cols, rows, img|
+ img.resize!(cols, rows)
+ width = cols
+ height = rows
end
- sizes.each do |size|
- img.change_geometry("#{size}x#{size}") do |cols, rows, img|
- img.resize!(cols, rows)
- width = cols
- height = rows
- end
-
- img = apply_effect(ARGV[2], img) if ARGV[2]
-
- file_name = "#{width}x#{height}_#{ARGV[0]}"
+ img = apply_effect(options[:effect], img) if options[:effect]
- if File.exists?(file_name)
- puts "File already exists. Unable to write file."
- exit
- end
+ file_name = "#{width}x#{height}_#{ARGV[0]}"
- img.write(file_name)
+ if File.exists?(file_name)
+ puts "File already exists. Unable to write file."
+ exit
end
+
+ img.write(file_name)
end
def apply_effect(effect, img)

0 comments on commit b886020

Please sign in to comment.