#!/usr/bin/env ruby $LOAD_PATH << './lib' require 'optparse' require 'web_grep' options = {} OptionParser.new do |opt| opt.banner = "Usage:\n web_grep WORD WEB_PAGE [OPTIONS]" opt.separator "\nOptions" opt.on('-w', '--word [word]', String, 'Searcheble word or RegExp') do |word| options[:word] = word end opt.on('-f', '--file [file_path]', String, 'Search in file: "../index.html"') do |file| options[:file] = file end opt.on('-u', '--url [url]', String, 'Search in URL: "ya.ru"') do |url| options[:url] = url end opt.on('-q', '--quite', 'Show only xpaths') do |q| options[:quite] = q end opt.on('-c', '--count', 'Show only count') do |c| options[:only_count] = c end opt.on('-x', '--xpath', 'Show xpaths to founded') do |x| options[:xpath] = x end opt.on('-s', '--css', 'Show css paths to founded') do |s| options[:css] = s end opt.on_tail('-v', '--version', 'Show version') do puts WebGrep::VERSION exit end opt.on_tail('-h', '--help', 'Show this help') { puts opt; exit } if ARGV.empty? && options.empty? opt.on { puts opt; exit } end end.parse! greped = WebGrep::Grep.new( word: options[:word] || ARGV[0], url: options[:url ] || ARGV[1], file: options[:file], quite: options[:quite] ).grep! greped.each do |l| puts "\033[32;1m" + "CSS path: #{l.css_path}" if options[:css] puts "\033[32;1m" + "XPath: #{l.path}" if options[:xpath] puts "\033[0m" + "Matched content: #{l.content}" unless options[:quite] end unless options[:only_count] puts "#{"\033[32;1m"}Found #{greped.count}#{"\033[0m"}"