lib/zpng/cli.rb in zpng-0.0.1 vs lib/zpng/cli.rb in zpng-0.0.2

- old
+ new

@@ -32,10 +32,14 @@ end ACTIONS.each do |t,desc| opts.on *[ "-#{t[0].upcase}", "--#{t}", desc, eval("lambda{ |_| @actions << :#{t} }") ] end + + opts.on "-E", "--extract-chunk id" do |id| + @actions << [:extract_chunk, id.to_i] + end end if (argv = optparser.parse(@argv)).empty? puts optparser.help return @@ -49,15 +53,32 @@ @file_name = fname @zpng = load_file fname @actions.each do |action| - self.send(action) if self.respond_to?(action) + if action.is_a?(Array) + self.send(*action) if self.respond_to?(action.first) + else + self.send(action) if self.respond_to?(action) + end end end rescue Errno::EPIPE # output interrupt, f.ex. when piping output to a 'head' command # prevents a 'Broken pipe - <STDOUT> (Errno::EPIPE)' message + end + + def extract_chunk id + @img.chunks.each do |chunk| + if chunk.idx == id + case chunk + when ZPNG::Chunk::ZTXT + print chunk.text + else + print chunk.data + end + end + end end def load_file fname @img = ZPNG::Image.new fname end