Class: Options::SenderOptionParser

Inherits:
SRCommonOptionParser show all
Defined in:
lib/options/sender_option_parser.rb

Overview

Option parser of basic (see Options::BasicOptionParser), common (see Options::SRCommonOptionParser) and specific options for sender client

Specific sender options

count

number of messages to send (default: DEFAULT_COUNT, see Defaults)

msg-content

message content (default: DEFAULT_MSG_CONTENT, see Defaults)

msg-content-map-item

message content map item

msg-content-list-item

message content list item

msg-durable

message durability (default: DEFAULT_MSG_DURABLE)

msg-ttl

message Time-To-Live (ms) (default: DEFAULT_MSG_TTL)

msg-correlation-id

message correlation ID

msg-reply-to

address to send reply to

msg-group-id

message group ID

msg-to

message destination

Instance Attribute Summary

Attributes inherited from BasicOptionParser

#options

Instance Method Summary collapse

Methods inherited from SRCommonOptionParser

#parse

Methods inherited from BasicOptionParser

#parse

Constructor Details

#initialize(args) ⇒ SenderOptionParser

Initialization and parsing of basic, common and specific sender options

Parameters

args

arguments to parse



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/options/sender_option_parser.rb', line 42

def initialize(args)
  # Initialization of basic and common options
  super()
  # Sender usage
  @opt_parser.banner = "Usage: <sender_program> [OPTIONS]"

  # Sender specific options with default values

  # Number of messages option
  @options.count = Defaults::DEFAULT_COUNT
  # Message property option
  @options.msg_properties = Defaults::DEFAULT_MSG_PROPERTIES
  # Message content option
  @options.msg_content = Defaults::DEFAULT_MSG_CONTENT
  # Message content type option
  @options.msg_content_type = Defaults::DEFAULT_MSG_CONTENT_TYPE
  # Content type option
  @options.content_type = Defaults::DEFAULT_CONTENT_TYPE
  # Message durability
  @options.msg_durable = Defaults::DEFAULT_MSG_DURABLE
  # Message TTL
  @options.msg_ttl = Defaults::DEFAULT_MSG_TTL
  # Message correlation ID option
  @options.msg_correlation_id = Defaults::DEFAULT_CORR_ID
  # Address to send reply to
  @options.msg_reply_to = Defaults::DEFAULT_MSG_REPLY_TO
  # Message group ID option
  @options.msg_group_id = Defaults::DEFAULT_GROUP_ID
  # Message priority option
  @options.msg_priority = Defaults::DEFAULT_MSG_PRIORITY
  # Message ID option
  @options.msg_id = Defaults::DEFAULT_MSG_ID
  # Message user ID option
  @options.msg_user_id = Defaults::DEFAULT_MSG_USER_ID
  # Message subject option
  @options.msg_subject = Defaults::DEFAULT_MSG_SUBJECT
  # Anonymous producer option
  @options.anonymous = Defaults::DEFAULT_ANONYMOUS
  # Message destination
  @options.msg_to = Defaults::DEFAULT_TO

  # List of blocks executed at the end of parsing
  # when all other options are already applied
  @do_last = []

  # Number of messages
  @opt_parser.on(
    "-c",
    "--count COUNT",
    Integer,
    "number of messages to send (default: #{Defaults::DEFAULT_COUNT})"
  ) do |count|
    @options.count = count
  end

  # Message content
  @opt_parser.on(
    "-m",
    "--msg-content CONTENT",
    String,
    "message content (default: "+(
      if Defaults::DEFAULT_MSG_CONTENT.nil?
        "nil"
      else
        Defaults::DEFAULT_MSG_CONTENT
      end
    )+")"
  ) do |msg_content|
    @do_last << Proc.new {
      @options.msg_content = manual_cast(@options.content_type, msg_content)
    }
  end

  # Message content from file
  @opt_parser.on(
    "--msg-content-from-file FILE",
    String,
    "message content from file"
  ) do |file|
    @do_last << Proc.new {
      @options.msg_content = manual_cast(@options.content_type, File.read(file))
    }
  end

  # Message content type
  @opt_parser.on(
    "--msg-content-type TYPE",
    String,
    "AMQP message content type"
  ) do |msg_content_type|
    @options.msg_content_type = msg_content_type
  end

  # Content type
  @opt_parser.on(
    "--content-type TYPE",
    %w(string int long float bool),
    "cast --msg-content, -list-item, and =values of -map-item" +
    "and --msg-property to this type (default: #{Defaults::DEFAULT_CONTENT_TYPE})"
  ) do |content_type|
    @options.content_type = content_type
  end

  # Message property
  @opt_parser.on(
    "-P",
    "--msg-property KEY=VALUE",
    String,
    "property specified as KEY=VALUE (use '~' instead of '=' for auto-casting)"
  ) do |msg_property|
    _ = parse_kv(msg_property)  # ensure correct option format
    @do_last << Proc.new {
      if @options.msg_properties.nil?
        @options.msg_properties = {}
      end

      key, value = parse_kv(msg_property)
      @options.msg_properties[key] = value
    }
  end
  # Message map content
  @opt_parser.on(
    "-M",
    "--msg-content-map-item KEY=VALUE",
    String,
    "map item specified as KEY=VALUE (use '~' instead of '=' for auto-casting)"
  ) do |msg_content_map_item|
    _ = parse_kv(msg_content_map_item)  # ensure correct option format
    @do_last << Proc.new {
      if @options.msg_content.nil?
        @options.msg_content = {}
      end

      key, value = parse_kv(msg_content_map_item)
      @options.msg_content[key] = value
    }
  end

  # Message list content
  @opt_parser.on(
    "-L",
    "--msg-content-list-item VALUE",
    "list item"
  ) do |msg_content_list_item|
    @do_last << Proc.new {
      if @options.msg_content.nil?
        @options.msg_content = []
      end

      if msg_content_list_item.start_with? "~"
        value = msg_content_list_item[1..-1]
        @options.msg_content.push(auto_cast(value))
      else
       @options.msg_content.push(
            manual_cast(@options.content_type, msg_content_list_item))
      end
    }
  end

  # Message durability
  @opt_parser.on(
    "--msg-durable DURABILITY",
    BOOLEAN_STRINGS,
    "message durability (yes/no|True/False|true/false, default: "+
    "#{Defaults::DEFAULT_MSG_DURABLE})"
  ) do |msg_durable|
    @options.msg_durable = StringUtils.str_to_bool(msg_durable)
  end

  # Message TTL
  @opt_parser.on(
    "--msg-ttl TTL",
    Integer,
    "message Time-To-Live (ms) (default: #{Defaults::DEFAULT_MSG_TTL})"
  ) do |msg_ttl|
    @options.msg_ttl = msg_ttl
  end

  # Message correlation id
  @opt_parser.on(
    "--msg-correlation-id ID",
    String,
    "message correlation ID"
  ) do |msg_correlation_id|
    @options.msg_correlation_id = msg_correlation_id
  end

  # Address to send reply to
  @opt_parser.on(
    "--msg-reply-to ADDRESS",
    String,
    "address to send reply to"
  ) do |msg_reply_to|
    @options.msg_reply_to = msg_reply_to
  end

  # Message group id
  @opt_parser.on(
    "--msg-group-id ID",
    String,
    "message group ID"
  ) do |msg_group_id|
    @options.msg_group_id = msg_group_id
  end

  # Message priority
  @opt_parser.on(
    "--msg-priority PRIORITY",
    Integer,
    "message priority"
  ) do |msg_priority|
    @options.msg_priority = msg_priority
  end

  # Message ID
  @opt_parser.on(
    "--msg-id ID",
    String,
    "message ID"
  ) do |msg_id|
    @options.msg_id = msg_id
  end

  # Message user ID
  @opt_parser.on(
    "--msg-user-id ID",
    String,
    "message user ID"
  ) do |msg_user_id|
    @options.msg_user_id = msg_user_id
  end

  # Message subject
  @opt_parser.on(
    "--msg-subject SUBJECT",
    String,
    "message subject"
  ) do |msg_subject|
    @options.msg_subject = msg_subject
  end

  # Anonymous producer
  @opt_parser.on(
    "--anonymous [ANONYMOUS]",
    BOOLEAN_STRINGS,
    "send message by connection level anonymous sender" +
    " (default: #{Defaults::DEFAULT_ANONYMOUS})"
  ) do |anonymous|
    @options.anonymous = true
    @options.anonymous = StringUtils.str_to_bool(anonymous) if anonymous
  end

  # Duration mode
  duration_modes = %w(before-send after-send after-send-tx-action)
  @options.duration_mode = "before-send"
  @opt_parser.on(
    "--duration-mode MODE", duration_modes,
    "in use with --duration defines where to wait (allowed: #{duration_modes.join(', ')}, default: #{@options.duration_mode})"
  ) do |d|
    @options.duration_mode = d
  end

  # Destination
  @opt_parser.on(
    "--msg-to ADDRESS",
    String,
    "destination ADDRESS"
  ) do |msg_to|
    @options.msg_to = msg_to
  end

  # Parse basic, common and specific options for sender client
  parse(args)
  # Apply options which need to be applied last
  @do_last.each { |proc| proc.call }
end