module PrettySearch

Constants

HELP_TEXT
VERSION

Public Class Methods

parse_cli_opts() click to toggle source

@return [Hash] like:

{
  help: [bool],
  data: [string],
}
# File lib/pretty_search/cli_options.rb, line 11
def parse_cli_opts
  abort 'Ruby 2.0.0 or newer is required' unless defined?(GetoptLong)
  cli_opts = GetoptLong.new(
    ['--help',  '-h', GetoptLong::NO_ARGUMENT],
    ['--first', '-f', GetoptLong::NO_ARGUMENT],
    ['--data',  '-d', GetoptLong::REQUIRED_ARGUMENT]
  )
  options = {}
  cli_opts.each do |option, args|
    # args is "" for options without an argument
    options[option[2..-1].to_sym] = args == '' ? true : args
  end
  options
end
run(query, data: nil, **options) click to toggle source
# File lib/pretty_search.rb, line 14
def self.run(query, data: nil, **options)
  if data.nil?
    raise MissingParameter, 'Data file is required, please pass in as --data'
  end
  collection = PrettySearch::Collection.load(data, options)
  found = collection.search(query)
  if found.empty?
    'No records found.'
  else
    found.join("\n")
  end
end