Class: Ned::Grep

Inherits:
Command show all
Defined in:
lib/ned/commands/grep.rb

Instance Method Summary collapse

Methods inherited from Command

config, description, #execute, #execute_all, help, #initialize, #load_lines, long_name, option_parser, #options, #peek, require_all, #require_all, #require_all?, require_all?, short_name

Constructor Details

This class inherits a constructor from Ned::Command

Instance Method Details

#execute_internal(line) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ned/commands/grep.rb', line 36

def execute_internal(line)
  line.slice!(-1) if line[-1] == "\n"

  if options[:invert]
    @pattern.match(line) ? [] : [line << "\n"]
  elsif options[:only]
    line.scan(@pattern).map(&:ensure_trailing_newline)
  else
    @pattern.match(line) ? [line << "\n"] : []
  end
end

#parse(args) ⇒ Object

Raises:

  • (OptionParser::ParseError)


28
29
30
31
32
33
34
# File 'lib/ned/commands/grep.rb', line 28

def parse(args)
  super

  raise OptionParser::ParseError.new("missing pattern argument") if args.size == 0

  @pattern = Regexp.new(args.shift)
end