#!/usr/bin/env ruby require 'coypond' def print_help puts "Usage:" puts " coypond search_term [options] [file1 [file2 [file3 [...]]]]" puts "" puts "Options:" puts " -m:\n search method names\n\n" puts " -c:\n search class names\n\n" puts " -M:\n search Module names\n\n" puts " -a:\n search all available type names (default: true unless another search option was given)\n\n" puts " -r:\n treat the search term as a regular expression\n\n" puts " -i:\n ignore case\n\n" puts " -g [gem name]:\n look through types in gems beginning with the name specified\n\n" puts "" puts "Examples:" puts " coypond initialize .\n\n" puts " coypond \"ActiveRecord.*#validates_.*\" -r -g activerecord\n\n" end if $*.empty? or $*.first == "-h" or $*.first == "--help" print_help exit(0) end options = {:search_term => $*.shift} while !$*.empty? and $*.first.start_with?("-") option = $*.shift case option when "-r" options[:regexp] = true when "-i" options[:ignore_case] = true when "-m" options[:method] = true when "-c" options[:class] = true when "-M" options[:module] = true when "-g" options[:gem] = $*.shift when "-a" options[:all] = true end end options[:all] ||= (options.keys & [:class, :method, :module]).empty? def expand(file, pattern=["**"]) if File.directory?(file) return Dir.glob(File.join([file, pattern, "*.rb"].flatten) ) else return file end end files = [] if options[:gem] $:.each do |path| files += expand(path, ["#{options[:gem]}*", "**"]) end else files = $*.map {|file| expand(file)}.flatten end coypond = Coypond::CoypondRunner.new(files) coypond.search(options) do |file_name, res| unless res.empty? puts "#{file_name}:" res.each do |dec, location, context| puts " #{location.join(",")}: #{context.strip}" end puts "" end end