Sha256: 19ed01139816e1cb9b157d19b49c119191f012ec6ac173a4466294f6ae01ad45

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'optparse'

module SqRubyGrep
  class Parser
    def self.parse(options)
      args = Hash.new

      opt_parser = OptionParser.new do |opts|
        opts.banner = 'RubyGrep searches the named input FILE for lines containing a match to the given PATTERN.'
        opts.banner = 'Usage: ruby_grep [FILE] [options]'

        opts.on('-e PATTERN', '--regex=PATTERN', String, 'Use PATTERN as the pattern.') do |pattern|
          args[:pattern] = pattern
        end

        opts.on('-A NUM', '--after-context=NUM', Integer, 'Print NUM lines of trailing context after matching lines.') do |n|
          args[:after_lines] = n
        end

        opts.on('-B NUM', '--before-context=NUM', Integer, 'Print NUM line of leading context before matching lines.') do |n|
          args[:before_lines] = n
        end

        opts.on('-c', '--colorize', 'Colorize matches') do
          args[:colorize] = true
        end

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

      end

      opt_parser.parse!(options)

      args[:file_path] = ARGV.shift

      if args[:file_path].to_s.empty? || args[:pattern].to_s.empty?
        puts opt_parser
        exit
      end

      return args
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sq_ruby_grep-0.0.3 lib/sq_ruby_grep/parser.rb