/
command_extensions.rb
270 lines (207 loc) · 7.94 KB
/
command_extensions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
module HammerCLI
class CommandExtensions
class << self
attr_accessor :delegatee
def logger
Logging.logger[to_s]
end
def inheritable?
@inheritable
end
end
ALLOWED_EXTENSIONS = %i[
option command_options before_print data output help request
request_headers headers request_options options request_params params
option_sources predefined_options use_option option_family
].freeze
def initialize(options = {})
@only = options[:only] || ALLOWED_EXTENSIONS
@only = [@only] unless @only.is_a?(Array)
@except = options[:except] || []
@except = [@except] unless @except.is_a?(Array)
@inheritable = options[:inheritable]
end
def inheritable?
return @inheritable unless @inheritable.nil?
self.class.inheritable? || false
end
def self.method_missing(message, *args, &block)
if @delegatee ||= nil
@delegatee.send(message, *args, &block)
else
super
end
end
# DSL
def self.inheritable(boolean)
@inheritable = boolean
end
def self.option(switches, type, description, opts = {}, &block)
@options ||= []
@options << { switches: switches,
type: type,
description: description,
opts: opts, block: block }
end
def self.use_option(*names)
@predefined_option_names = names
end
def self.before_print(&block)
@before_print_block = block
end
def self.output(&block)
@output_extension_block = block
end
def self.help(&block)
@help_extension_block = block
end
def self.request_headers(&block)
@request_headers_block = block
end
def self.request_options(&block)
@request_options_block = block
end
def self.request_params(&block)
@request_params_block = block
end
def self.option_sources(&block)
@option_sources_block = block
end
def self.option_family(options = {}, &block)
@option_family_extensions ||= []
@option_family_extensions << {
options: options,
block: block
}
end
# Object
def extend_options
allowed = @only & %i[command_options option]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_options(@command_class)
end
def extend_predefined_options
allowed = @only & %i[predefined_options use_option]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_predefined_options(@command_class)
end
def extend_before_print(data)
allowed = @only & %i[before_print data]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_before_print(data, @command_object, @command_class)
end
def extend_output
allowed = @only & %i[output]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_output(@command_class, @command_object ||= nil)
end
def extend_help
allowed = @only & %i[help]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_help(@command_class)
end
def extend_request_headers(headers)
allowed = @only & %i[request_headers headers request]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_request_headers(headers, @command_object, @command_class)
end
def extend_request_options(options)
allowed = @only & %i[request_options options request]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_request_options(options, @command_object, @command_class)
end
def extend_request_params(params)
allowed = @only & %i[request_params params request]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_request_params(params, @command_object, @command_class)
end
def extend_option_sources(sources)
allowed = @only & %i[option_sources]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_option_sources(sources, @command_object, @command_class)
end
def extend_option_family
allowed = @only & %i[option_family]
return if allowed.empty? || (allowed & @except).any?
self.class.extend_option_family(@command_class)
end
def delegatee(command_class)
self.class.delegatee = command_class
end
def command_class(command_class)
@command_class = command_class
end
def command_object(command_object)
@command_object = command_object
end
def details
except = @except.empty? ? '*nothing*' : @except
details = if @only == ALLOWED_EXTENSIONS
"*all* except #{except}"
else
"#{@only} only"
end
"#{self.class} for #{details}"
end
# Class
def self.extend_options(command_class)
return unless @options ||= nil
@options.each do |option|
command_class.send(:option,
option[:switches],
option[:type],
option[:description],
option[:opts],
&option[:block])
logger.debug("Added option for #{command_class}: #{option}")
end
end
def self.extend_predefined_options(command_class)
@predefined_option_names ||= nil
command_class.send(:use_option, *@predefined_option_names)
logger.debug("Added predefined options for #{command_class}: #{@predefined_option_names}")
end
def self.extend_before_print(data, command_object, command_class)
return unless @before_print_block ||= nil
@before_print_block.call(data, command_object, command_class)
logger.debug("Called block for #{@delegatee} data:\n\t#{@before_print_block}")
end
def self.extend_output(command_class, command_object)
return unless @output_extension_block ||= nil
@output_extension_block.call(command_class.output_definition, command_object, command_class)
logger.debug("Called block for #{@delegatee} output definition:\n\t#{@output_extension_block}")
end
def self.extend_help(command_class)
return unless @help_extension_block ||= nil
command_class.help_extension_blocks << @help_extension_block
logger.debug("Saved block for #{@delegatee} help definition:\n\t#{@help_extension_block}")
end
def self.extend_request_headers(headers, command_object, command_class)
return unless @request_headers_block ||= nil
@request_headers_block.call(headers, command_object, command_class)
logger.debug("Called block for #{@delegatee} request headers:\n\t#{@request_headers_block}")
end
def self.extend_request_options(options, command_object, command_class)
return unless @request_options_block ||= nil
@request_options_block.call(options, command_object, command_class)
logger.debug("Called block for #{@delegatee} request options:\n\t#{@request_options_block}")
end
def self.extend_request_params(params, command_object, command_class)
return unless @request_params_block ||= nil
@request_params_block.call(params, command_object, command_class)
logger.debug("Called block for #{@delegatee} request params:\n\t#{@request_params_block}")
end
def self.extend_option_sources(sources, command_object, command_class)
return unless @option_sources_block ||= nil
@option_sources_block.call(sources, command_object, command_class)
logger.debug("Called block for #{@delegatee} option sources:\n\t#{@option_sources_block}")
end
def self.extend_option_family(command_class)
(@option_family_extensions ||= []).each do |extension|
extension[:options][:creator] = command_class
command_class.send(:option_family, extension[:options], &extension[:block])
logger.debug("Called option family block for #{command_class}:\n\t#{extension[:block]}")
end
end
end
end