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
|
# File 'lib/optopus.rb', line 150
def parse!
options = {}
has_arg_v = false
has_arg_h = false
options.instance_eval("def config_file; @__config_file__; end")
file_args_checker = lambda do |v|
if v.kind_of?(Hash)
config = v
else
config = YAML.load_file(v)
@on_before.call(config) if @on_before
end
config.keys.each do |key|
next unless key.kind_of?(Symbol)
config[key.to_s.gsub('_', '-')] = config[key]
end
options.instance_variable_set(:@__config_file__, config)
@opts_args.each do |name, args, defval, block, required, multiple|
if args[1].kind_of?(String) and args[1] =~ /-+([^\s=]+)/
key = $1
else
key = name.to_s
end
value = nil
[key, key.gsub(/[-_]/, '-'), key.gsub(/[-_]/, '_')].each do |k|
if value = config[k]
key = k
break
end
end
next unless value
check_block = lambda do |unit|
unit = orig_val = unit.to_s
if type = args.find {|i| i.kind_of?(Class) }
pat, conv = OptionParser::DefaultList.atype[type]
if pat and pat !~ unit
raise OptionParser::InvalidArgument.new(v, "(#{key}: #{unit})")
end
unit = conv.call(unit) if conv
elsif type = args.find {|i| i.kind_of?(Array) }
unless type.map {|i| i.to_s }.include?(unit.to_s)
raise OptionParser::InvalidArgument.new(key, unit)
end
unit = unit.to_s.to_sym
end
if unit and block
begin
CheckerContext.evaluate(v, unit, &block)
rescue OptionParser::ParseError => e
errmsg = "#{e.message}: #{key}=#{orig_val}"
raise OptionParser::ParseError, errmsg
end
end
return unit
end
if multiple
value = [value] unless value.kind_of?(Array)
options[name] = value.map {|i| check_block.call(i) }
else
options[name] = check_block.call(value)
end
end
end
if @file_args
@parser.on(*@file_args, &file_args_checker)
elsif @on_before
config = {}
@on_before.call(config)
file_args_checker.call(config)
end
@opts_args.each do |name, args, defval, block, required, multiple|
options[name] = defval unless defval.nil?
has_arg_v = (args.first == '-v')
has_arg_h = (args.first == '-h')
@parser.on(*args) do |*v|
value = v.first || true
if multiple
options[name] ||= []
options[name] << value
else
options[name] = value
end
CheckerContext.evaluate(v, value, &block) if block
end
end
unless has_arg_v
@parser.on_tail('-v', '--version', 'show version') do
v = @parser.ver or abort("#{@parser.program_name}: version unknown")
puts v
exit
end
end
unless has_arg_h
@parser.on_tail('-h', '--help', 'show this message') do
puts @parser.help
exit
end
end
@parser.parse!
@opts_args.each do |name, args, defval, block, required, multiple|
if required and not options.has_key?(name)
raise OptionParser::NotGiven, args.first
end
end
(options.config_file || {}).each do |key, value|
next if key.kind_of?(Symbol)
key = key.to_s.gsub('-', '_').to_sym
options[key] = value unless options.has_key?(key)
end
CheckerContext.evaluate([], options, &@on_after) if @on_after
return options
rescue => e
if @on_error
@on_error.call(e)
else
raise e
end
end
|