#!/usr/bin/env ruby #== czsearch is a userful command to search via the Code Zauker facility # Send something like -W0 to ruby, for a cleaner output $VERBOSE=nil require 'code_zauker' require 'code_zauker/grep' include Grep require 'optparse' options={} optparse= OptionParser.new do |opts| opts.banner="Usage: czsearch [options] [term1] [term2]..." options[:ignorecase]=false options[:precontext]=0 options[:postcontext]=0 options[:extensions_to_ignore]=[] options[:file_to_exclude]=[] opts.on('-i', '--ignore-case','ignore case distinctions') do options[:ignorecase]=true end opts.on('-B', '--before-context NUM', Integer, 'print NUM lines of leading context') do | c | options[:precontext]=c end opts.on('-A','--after-context NUM',Integer,'print NUM lines of trailing context') do | c | options[:postcontext]=c end opts.on('-C','--context NUM',Integer,'print NUM lines of output context') do | c | if c>0 options[:postcontext]=c options[:precontext]=options[:postcontext] end end opts.on('-X','--exclude FILE_PATTERN',String, 'Exclude files that match FILE_PATTERN (as ruby regexp). Case insensitive') do |p| options[:file_to_exclude].push(/#{Regexp.escape(p)}/i); end opts.on( '-h', '--help', 'Display this screen' ) do puts opts puts "Options are grep-like" puts "EXAMPLES:" puts "czsearch ciao Koros" puts " Will search Koros OR ciao" puts "czsearch -i gnu" puts " Will match also GNU and Gnu" puts "czsearch -X .orig -X .bak -X .java html:select" puts " Will skip java and backup file" exit end end optparse.parse! ARGV.each do | s | #puts "Code Zauker Searching for #{s}" fs=CodeZauker::FileScanner.new() if options[:ignorecase]==false files=fs.search(s) pattern=/#{Regexp.escape(s)}/ else files=fs.isearch(s) pattern=/#{Regexp.escape(s)}/i end files.each do |f| to_exclude=false if options[:file_to_exclude].length >0 # Will match? to_exclude=false options[:file_to_exclude].each do |pattern| #puts "\n\t#{f} =~ #{pattern}" if (f =~ pattern ) to_exclude=true #puts "Excluded #{f}" break end end end if !to_exclude begin lines=grep(f,pattern, pre_context=options[:precontext], post_context=options[:postcontext]); lines.each do |l | puts "#{f}:#{l}" end rescue ArgumentError => ioe puts "FATAL ArgumentError on #{f}" raise ioe end else end end end