Class: Options::BasicOptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/options/basic_option_parser.rb

Overview

Option parser of basic options for all clients

Basic client options

broker

URI of broker in format IP:PORT (default: DEFAULT_BROKER, see Defaults)

conn-allowed-mechs

allowed SASL mechanisms for authentication

help

show help message and exit

Direct Known Subclasses

ConnectorOptionParser, SRCommonOptionParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicOptionParser

Initialization of basic client options



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/options/basic_option_parser.rb', line 40

def initialize()
  @options = OpenStruct.new
  # Basic client's options with default values

  # Broker in format IP:PORT option
  @options.broker = Defaults::DEFAULT_BROKER
  # Exit timer
  @options.exit_timer = Defaults::DEFAULT_EXIT_TIMER
  # Allowed SASL mechanisms
  @options.sasl_mechs = Defaults::DEFAULT_SASL_MECHS
  # Idle timeout
  @options.idle_timeout = Defaults::DEFAULT_IDLE_TIMEOUT
  # Max frame size
  @options.max_frame_size = Defaults::DEFAULT_MAX_FRAME_SIZE
  # SASL enabled
  @options.sasl_enabled = Defaults::DEFAULT_SASL_ENABLED
  # Client library logging
  @options.log_lib = Defaults::DEFAULT_LOG_LIB

  @opt_parser = OptionParser.new
  # Basic usage
  @opt_parser.banner = "Usage: <basic_program> [OPTIONS]"

  @opt_parser.separator ""
  # Broker
  @opt_parser.on(
    "-b",
    "--broker BROKER",
    String,
    "URI of broker in format IP:PORT "+
    "(default: #{Defaults::DEFAULT_BROKER})"
  ) do |broker|
    @options.broker = broker
  end

  # Client exits after this timeout.
  # Handlers can restart the timer
  # (e.g. on receiving messages, making connections etc.)
  @opt_parser.on(
    "-t",
    "--timeout TIMEOUT",
    Float,
    "timeout in seconds to wait before exiting, 0 unlimited (default: 0)"
  ) do |timeout|
    @options.exit_timer = ExitTimer.new(timeout) if timeout > 0
  end

  # Allowed SASL mechanisms
  @opt_parser.on(
    "--conn-allowed-mechs MECHS",
    String,
    "space separated list of SASL mechanisms allowed by client "+
    "for authentication (ANONYMOUS/PLAIN/EXTERNAL, default: "+
    "'#{Defaults::DEFAULT_SASL_MECHS}')"
  ) do |sasl_mechs|
    @options.sasl_mechs = sasl_mechs
  end

  # Max frame size
  @opt_parser.on(
    "--conn-max-frame-size SIZE",
    Integer,
    "define custom maximum frame size in bytes " +
    "(range: #{Defaults::DEFAULT_MIN_MAX_FRAME_SIZE}-" +
    "#{Defaults::DEFAULT_MAX_MAX_FRAME_SIZE}, " +
    "default: #{Defaults::DEFAULT_MAX_FRAME_SIZE})",
  ) do |max_frame_size|
    if max_frame_size < Defaults::DEFAULT_MIN_MAX_FRAME_SIZE \
      or max_frame_size > Defaults::DEFAULT_MAX_MAX_FRAME_SIZE
      raise OptionParser::InvalidArgument, "#{max_frame_size} " +
        "(out of range: #{Defaults::DEFAULT_MIN_MAX_FRAME_SIZE}-" +
        "#{Defaults::DEFAULT_MAX_MAX_FRAME_SIZE})"
    end
    @options.max_frame_size = max_frame_size
  end

  # Heartbeats configuration
  @opt_parser.on(
    "--conn-heartbeat HEARTBEAT",
    Integer,
    "enable and set connection heartbeat, " +
    "default: #{Defaults::DEFAULT_IDLE_TIMEOUT})"
  ) do |idle_timeout|
    @options.idle_timeout = idle_timeout
  end

  # Connection SASL enabled
  @opt_parser.on(
    "--conn-sasl-enabled [ENABLED]",
    Options::BOOLEAN_STRINGS,
    "enable connection SASL (#{Options::BOOLEAN_STRINGS.join("/")}, "+
    "default: #{Defaults::DEFAULT_SASL_ENABLED})"
  ) do |sasl_enabled|
    @options.sasl_enabled = true
    @options.sasl_enabled = StringUtils.str_to_bool(sasl_enabled) if sasl_enabled
  end

  # Client library logging
  @opt_parser.on(
    "--log-lib LEVEL",
    LOG_LIB_STRINGS,
    "enable client library logging (#{LOG_LIB_STRINGS.join("/")}, " +
    "default: #{Defaults::DEFAULT_LOG_LIB})"
  ) do |log_lib|
    @options.log_lib = log_lib
  end

  # Help
  @opt_parser.on_tail(
    "-h",
    "--help",
    "show help message and exit"
  ) do
    puts @opt_parser
    exit
  end
end

Instance Attribute Details

#optionsObject

Client options



37
38
39
# File 'lib/options/basic_option_parser.rb', line 37

def options
  @options
end

Instance Method Details

#parse(args) ⇒ Object

Parsing of basic options for all clients

Parameters

args

arguments to parse



161
162
163
# File 'lib/options/basic_option_parser.rb', line 161

def parse(args)
  @opt_parser.parse(args)
end