class Rubygrep::RubyGrepOptions

Attributes

expression[RW]
file_names[RW]
file_reader_options[RW]
general_output_options[RW]
matcher_options[RW]
outputter_options[RW]

Public Class Methods

new(args) click to toggle source
# File lib/rubygrep/ruby_grep_options.rb, line 6
def initialize(args)
  @file_reader_options, @matcher_options, @outputter_options = {}, {}, {}
  parse_options(args)
end

Public Instance Methods

set_multi_file_mode() click to toggle source
# File lib/rubygrep/ruby_grep_options.rb, line 11
def set_multi_file_mode
  outputter_options[:with_filename] = true
end

Private Instance Methods

end_grep(message) click to toggle source
# File lib/rubygrep/ruby_grep_options.rb, line 25
def end_grep(message)
  raise message
end
options_parser() click to toggle source
# File lib/rubygrep/ruby_grep_options.rb, line 29
def options_parser
  OptionParser.new do |opts|
    opts.banner = 'Usage: rubygrep [options] expression file1 [file2 file3 ...]'

    #file_reader_options
    opts.on( '-r', '--recursive', 'recursively read directories' ) do
      file_reader_options[:recursive] = true
    end

    #matcher_options
    opts.on( '-i', '--ignore-case', 'Ignore case when matching strings.' ) do
      matcher_options[:ignore_case] = true
    end

    opts.on( '-v', '--invert-selection', 'Invert the sense of matching, to select non-matching lines.') do
      matcher_options[:invert_selection] = true
    end

    #outputter_options
    # opts.on( '-m', '--mark', 'mark matched string' ) do |symbol|
    #   outputter_options[:mark_selection] = symbol || '|'
    # end

    opts.on('-n','--line-number','Prefix each line of output with the 1-based line number within its input file.') do
      outputter_options[:line_number] = true
    end

    opts.on('-H','--with-filename', 'Print the file name for each match. This is the default when there is more than one file to search.') do
      outputter_options[:with_filename] = true
    end
  end
end
parse_options(args) click to toggle source
# File lib/rubygrep/ruby_grep_options.rb, line 17
def parse_options(args)
  options_parser.parse!(args)
  end_grep('Expression is required') unless args[0]
  @expression = args.shift
  end_grep('At least one file in required') unless args[0]
  @file_names = args
  end