bin/image_optim in image_optim-0.7.3 vs bin/image_optim in image_optim-0.8.0

- old
+ new

@@ -6,10 +6,12 @@ require 'optparse' require 'find' options = {} +OptionParser.accept(ImageOptim::TrueFalseNil, OptionParser.top.atype[TrueClass][0].merge('nil' => nil)){ |arg, val| val } + option_parser = OptionParser.new do |op| op.banner = <<-TEXT #{op.program_name} v#{ImageOptim.version} Usege: @@ -21,30 +23,65 @@ options[:recursive] = recursive end op.separator nil - op.on('--[no-]threads NUMBER', Integer, 'Number of threads or disable (defaults to number of processors)') do |threads| + op.on('--[no-]threads N', Integer, 'Number of threads or disable (defaults to number of processors)') do |threads| options[:threads] = threads end - op.on('--[no-]nice NUMBER', Integer, 'Nice level (defaults to 10)') do |nice| + op.on('--[no-]nice N', Integer, 'Nice level (defaults to 10)') do |nice| options[:nice] = nice end op.separator nil + op.separator ' Disabling workers:' ImageOptim::Worker.klasses.each do |klass| - bin = klass.underscored_name.to_sym + bin = klass.bin_sym op.on("--no-#{bin}", "disable #{bin} worker") do |enable| options[bin] = enable end end op.separator nil + op.separator ' Worker options:' - op.on('-v', '--verbose', 'Verbose info') do |verbose| + ImageOptim::Worker.klasses.each_with_index do |klass, i| + op.separator nil unless i.zero? + + bin = klass.bin_sym + klass.option_definitions.each do |option_definition| + name = option_definition.name.to_s.gsub('_', '-') + default = option_definition.default + type = option_definition.type + + type, marking = case + when [TrueClass, FalseClass, ImageOptim::TrueFalseNil].include?(type) + [type, 'B'] + when Integer >= type + [Integer, 'N'] + when Array >= type + [Array, 'a,b,c'] + else + raise "Unknown type #{type}" + end + + description = "#{option_definition.description.gsub(' - ', ' - ')} (defaults to #{default})" + description = description.scan(/(.*?.{1,60})(?:\s|\z)/).flatten.join("\n ").split("\n") + + op.on("--#{bin}-#{name} #{marking}", type, *description) do |value| + options[bin] = {} unless options[bin].is_a?(Hash) + options[bin][option_definition.name.to_sym] = value + end + end + end + + op.separator nil + op.separator ' Common options:' + + op.on('-v', '--verbose', 'Verbose output') do |verbose| options[:verbose] = verbose end op.on_tail('-h', '--help', 'Show full help') do puts option_parser.help @@ -61,17 +98,34 @@ option_parser.parse! rescue OptionParser::ParseError => e abort "#{e.to_s}\n\n#{option_parser.help}" end +if options[:verbose] + def print_options(options, level = 1) + prefix = ' ' * level + options.each do |key, value| + if value.is_a?(Hash) + puts "#{prefix}#{key}:" + print_options(value, level + 1) + else + puts "#{prefix}#{key}: #{value.inspect}" + end + end + end + + puts 'Options:' + print_options(options) +end + if ARGV.empty? abort "specify image paths to optimize\n\n#{option_parser.help}" else recursive = options.delete(:recursive) image_optim = begin ImageOptim.new(options) rescue ImageOptim::ConfigurationError => e - abort e + abort e.to_s end paths = [] ARGV.each do |arg| if File.file?(arg)