Class: Frett::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/frett/cli.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/frett/cli.rb', line 5

def self.execute(stdout, arguments=[])
  options = {}
  banner = "Usage: frett [options] 'search string' [directory path]"

  optparse = OptionParser.new do|opts|
    opts.banner = banner

    options[:escape] = nil
    opts.on( '-n', '--escape', 'escape special characters' ) do
      options[:escape] = true
    end
    opts.on( '-N', '--no-escape', 
             'don\'t escape special chars. you may want to use:',
             '\'?\' for any character, \'*\' for multiple characters' ) do
      options[:escape] = false
    end

    options[:use_wildcard] = nil
    opts.on( '-w', '--use-wildcard', 'adds a \'*\' in front & to the end of your search string' ) do
      options[:use_wildcard] = true
    end
    opts.on( '-W', '--no-wildcard', 'don\'t wildcard the search string' ) do
      options[:use_wildcard] = false
    end

    options[:use_or] = nil
    opts.on( '-o', '--use-or', 'build search query using OR for the terms of your search string' ) do
      options[:use_or] = true
    end
    opts.on( '-a', '--use-and', 'build search query using AND for the terms of your search string' ) do
      options[:use_or] = false
    end

    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end

  optparse.parse!(arguments)
  search_options = options.inject({}) { |hsh, (key, value)| value.nil? ? hsh : hsh.merge(key => value) }
  path = File.join(Frett::Config.working_dir, arguments.last) if arguments.size > 1
  arguments.pop if path && File.exist?(path)
  needle = arguments.join(" ")

  puts "WARNING: frett_service is NOT running..".red unless File.exist?(File.join(Frett::Config.working_dir, Frett::Config.service_name << ".pid"))

  if needle.strip.empty?
    puts banner
  else
    Frett::Search.new(search_options).search(needle, path)
  end
end