Module: CarrotRpc::CLI

Defined in:
lib/carrot_rpc/cli.rb

Overview

Command-line interface for CarrotRpc

Class Method Summary collapse

Class Method Details

.add_common_options(option_parser) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/carrot_rpc/cli.rb', line 3

def self.add_common_options(option_parser)
  option_parser.separator ""

  option_parser.separator "Common options:"
  option_parser.on("-h", "--help") do
    puts option_parser.to_s
    exit
  end

  option_parser.on("-v", "--version") do
    puts CarrotRpc::VERSION
    exit
  end
end

.add_process_options(option_parser) ⇒ OptionParser

Add “Process options” to ‘option_parser`.

Parameters:

  • option_parser (OptionParser)

Returns:

  • (OptionParser)


25
26
27
28
29
30
31
32
33
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
# File 'lib/carrot_rpc/cli.rb', line 25

def self.add_process_options(option_parser)
  option_parser.separator ""

  option_parser.separator "Process options:"
  option_parser.on("-d", "--daemonize", "run daemonized in the background (default: false)") do
    CarrotRpc.configuration.daemonize = true
  end

  option_parser.on(" ", "--pidfile PIDFILE", "the pid filename") do |value|
    CarrotRpc.configuration.pidfile = value
  end

  stm_msg = "runs servers with '_test' appended to queue names." \
            "Set Rails Rack env vars to 'test' when used in conjunction with '--autoload_rails'"
  option_parser.on(" ", "--server_test_mode", stm_msg) do
    CarrotRpc.configuration.server_test_mode = true
  end

  option_parser.on(
    " ",
    "--autoload_rails value",
    "loads rails env by default. Uses Rails Logger by default."
  ) do |value|
    pv = value == "false" ? false : true
    CarrotRpc.configuration.autoload_rails = pv
  end

  option_parser.on(" ", "--logfile VALUE", "relative path and name for Log file. Overrides Rails logger.") do |value|
    CarrotRpc.configuration.logfile = File.expand_path("../../#{value}", __FILE__)
  end

  option_parser.on(
    " ",
    "--loglevel VALUE",
    "levels of loggin: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN"
  ) do |value|
    CarrotRpc.configuration.loglevel = Logger.const_get(value) || 0
  end

  # Optional. Defaults to using the ENV['RABBITMQ_URL']
  option_parser.on(
    " ",
    "--rabbitmq_url VALUE",
    "connection string to RabbitMQ 'amqp://user:pass@host:10000/vhost'"
  ) do |value|
    CarrotRpc.configuration.bunny = Bunny.new(value)
  end

  option_parser.on(
    " ",
    "--thread-request VARIABLE",
    "Copies the current request into VARIABLE Thread local variable, " \
    "where it can be retrieved with `Thread.thread_variable_get(<VARIABLE>)`"
  ) do |variable|
    CarrotRpc.configuration.thread_request_variable = variable
  end
end

.add_ruby_options(option_parser) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/carrot_rpc/cli.rb', line 83

def self.add_ruby_options(option_parser)
  option_parser.separator ""

  option_parser.separator "Ruby options:"

  option_parser.on("-I", "--include PATH", "an additional $LOAD_PATH") do |value|
    $LOAD_PATH.unshift(*value.split(":").map { |v| File.expand_path(v) })
  end

  option_parser.on("--debug", "set $DEBUG to true") do
    $DEBUG = true
  end

  option_parser.on("--warn", "enable warnings") do
    $-w = true
  end
end

.option_parserObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/carrot_rpc/cli.rb', line 103

def self.option_parser
  option_parser = OptionParser.new
  option_parser.banner =  "RPC Server Runner for RabbitMQ RPC Services."
  option_parser.separator ""
  option_parser.separator "Usage: server [options]"

  add_process_options(option_parser)
  add_ruby_options(option_parser)
  add_common_options(option_parser)

  option_parser.separator ""

  option_parser
end

.parse_options(args = ARGV) ⇒ Object



118
119
120
# File 'lib/carrot_rpc/cli.rb', line 118

def self.parse_options(args = ARGV)
  option_parser.parse!(args)
end