Class: Groonga::Client::CommandLine::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/client/command-line/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



25
26
27
28
29
30
31
32
# 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

Instance Method Details

#open_client(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# 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| ... } ⇒ Object

Yields:

  • (parser)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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