Class: GrepFu::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/grep-fu/options.rb

Constant Summary collapse

PATH_REPLACEMENTS =
{
  'a' => 'app', 
  'c' => 'app/controllers', 
  'h' => 'app/helpers', 
  'm' => 'app/models', 
  'v' => 'app/views', 
  'l' => 'lib',
  'p' => 'public', 
  'css' => 'public/stylesheets', 
  'js' => 'public/javascripts', 
  't' => 'test',
  's' => 'spec',
  'vp' => 'vendor/plugins',
  'mig' => 'db/migrate'
}
COLOR_ON_BY_DEFAULT =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Options

Returns a new instance of Options.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grep-fu/options.rb', line 35

def initialize(args)
  if args.include?('--version')
    puts "grep-fu #{File.read(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))}"
    exit(0)
  end

  @verbose = (args -= ['--verbose'] if args.include?('--verbose'))
  @single_line = (args -= ['--single-line'] if args.include?('--single-line'))

  @color = (args -= ['--color'] if (args.include?('--color')) || COLOR_ON_BY_DEFAULT)
  if args.include?('--no-color')
    args -= ['--no-color']
    @color = false
  end

  @search_criteria = args.last
  @file_path = args.size == 2 ? PATH_REPLACEMENTS[args.first] || args.first : './'
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



3
4
5
# File 'lib/grep-fu/options.rb', line 3

def color
  @color
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



3
4
5
# File 'lib/grep-fu/options.rb', line 3

def file_path
  @file_path
end

#search_criteriaObject (readonly)

Returns the value of attribute search_criteria.



3
4
5
# File 'lib/grep-fu/options.rb', line 3

def search_criteria
  @search_criteria
end

#single_lineObject (readonly)

Returns the value of attribute single_line.



3
4
5
# File 'lib/grep-fu/options.rb', line 3

def single_line
  @single_line
end

#verboseObject (readonly)

Returns the value of attribute verbose.



3
4
5
# File 'lib/grep-fu/options.rb', line 3

def verbose
  @verbose
end

Class Method Details

.usage(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grep-fu/options.rb', line 23

def self.usage(file)
  lines = ['',
    "Usage: #{file} [findpath] search_string [--verbose|--single-line]\n",
    "    Where findpath is one of the following:",
    "      any literal subdirectory",
    Options::PATH_REPLACEMENTS.map { |abbr, txt| "      #{abbr} - #{txt}" }.join("\n"),
  "\n\n"
  ]

  lines.join("\n")
end

Instance Method Details

#to_sObject



54
55
56
57
58
# File 'lib/grep-fu/options.rb', line 54

def to_s
  options = @verbose ? '-rin' : '-ril'
  options << ' --color=always' if @color
  options
end