bin/image_optim in image_optim-0.5.1 vs bin/image_optim in image_optim-0.6.0
- old
+ new
@@ -2,10 +2,11 @@
# encoding: UTF-8
require 'image_optim'
require 'progress'
require 'optparse'
+require 'find'
options = {}
option_parser = OptionParser.new do |op|
op.banner = <<-TEXT
@@ -18,21 +19,25 @@
op.on('--[no-]threads NUMBER', Integer, 'Number of threads or disable (defaults to number of processors)') do |threads|
options[:threads] = threads
end
- op.on('--[no-]nice NUMBER', Integer, 'Nice levle (defaults to 10)') do |nice|
+ op.on('--[no-]nice NUMBER', Integer, 'Nice level (defaults to 10)') do |nice|
options[:nice] = nice
end
ImageOptim::Worker.klasses.each do |klass|
bin = klass.underscored_name.to_sym
op.on("--[no-]#{bin} PATH", "#{bin} path or disable") do |path|
options[bin] = path
end
end
+ op.on('-r', '-R', '--recursive', 'Scan directories') do |recursive|
+ options[:recursive] = recursive
+ end
+
op.on('-v', '--verbose', 'Verbose info') do |verbose|
options[:verbose] = verbose
end
op.on_tail('-h', '--help', 'Show full help') do
@@ -53,17 +58,32 @@
end
if ARGV.empty?
abort "specify image paths to optimize\n\n#{option_parser.help}"
else
+ recursive = options.delete(:recursive)
io = begin
ImageOptim.new(options)
rescue ImageOptim::ConfigurationError, ImageOptim::BinaryNotFoundError => e
abort e
end
- paths = ARGV.reject do |arg|
- unless File.file?(arg)
- $stderr << "WARN: #{arg} is not a file\n"
+
+ paths = []
+ ARGV.each do |arg|
+ if File.file?(arg)
+ if io.optimizable?(arg)
+ paths << arg
+ else
+ warn "#{arg} is not an image or there is no optimizer for it"
+ end
+ else
+ if recursive
+ Find.find(arg) do |path|
+ paths << path if File.file?(path) && io.optimizable?(path)
+ end
+ else
+ warn "#{arg} is not a file"
+ end
end
end
paths = paths.with_progress('optimizing') if paths.length > 1
module Space