class Groonga::Client::CommandLine::Parser

Public Class Methods

new(options={}) click to toggle source
# File lib/groonga/client/command-line/parser.rb, line 25
def initialize(options={})
  @url      = nil
  @protocol = :http
  @host     = "localhost"
  @port     = nil

  @read_timeout = options[:read_timeout] || Client::Default::READ_TIMEOUT
end

Public Instance Methods

open_client(options={}) { |client| ... } click to toggle source
# File lib/groonga/client/command-line/parser.rb, line 85
def open_client(options={})
  default_options = {
    :url      => @url,
    :protocol => @protocol,
    :host     => @host,
    :port     => @port,
    :read_timeout => @read_timeout,
    :backend  => :synchronous,
  }
  Client.open(default_options.merge(options)) do |client|
    yield(client)
  end
end
parse(arguments) { |parser| ... } click to toggle source
# File lib/groonga/client/command-line/parser.rb, line 34
def parse(arguments)
  parser = OptionParser.new
  parser.version = VERSION

  parser.separator("")

  parser.separator("Connection:")

  parser.on("--url=URL",
            "URL to connect to Groonga server.",
            "If this option is specified,",
            "--protocol, --host and --port are ignored.") do |url|
    @url = url
  end

  available_protocols = [:http, :gqtp]
  parser.on("--protocol=PROTOCOL", [:http, :gqtp],
            "Protocol to connect to Groonga server.",
            "[#{available_protocols.join(", ")}]",
            "(#{@protocol})") do |protocol|
    @protocol = protocol
  end

  parser.on("--host=HOST",
            "Groonga server to be connected.",
            "(#{@host})") do |host|
    @host = host
  end

  parser.on("--port=PORT", Integer,
            "Port number of Groonga server to be connected.",
            "(auto)") do |port|
    @port = port
  end

  parser.on("--read-timeout=TIMEOUT", Integer,
            "Timeout on reading response from Groonga server.",
            "You can disable timeout by specifying -1.",
            "(#{@read_timeout})") do |timeout|
    @read_timeout = timeout
  end

  yield(parser)

  rest = parser.parse(arguments)

  @port ||= default_port(@protocol)

  rest
end

Private Instance Methods

default_port(protocol) click to toggle source
# File lib/groonga/client/command-line/parser.rb, line 100
def default_port(protocol)
  case protocol
  when :http
    10041
  when :gqtp
    10043
  end
end