Class: Optopus::Options

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

Overview

CheckerContext

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



114
115
116
117
# File 'lib/optopus.rb', line 114

def initialize
  @parser = OptionParser.new
  @opts_args = []
end

Instance Method Details

#add(name, args, desc, block) ⇒ Object



127
128
129
130
# File 'lib/optopus.rb', line 127

def add(name, args, desc, block)
  args, defval, required, multiple = fix_args(args, desc)
  @opts_args << [name.to_sym, args, defval, block, required, multiple]
end

#add_after(block) ⇒ Object



142
143
144
# File 'lib/optopus.rb', line 142

def add_after(block)
  @on_after = block
end

#add_before(block) ⇒ Object



138
139
140
# File 'lib/optopus.rb', line 138

def add_before(block)
  @on_before = block
end

#add_error(block) ⇒ Object



146
147
148
# File 'lib/optopus.rb', line 146

def add_error(block)
  @on_error = block
end

#add_file(args, desc) ⇒ Object



132
133
134
135
136
# File 'lib/optopus.rb', line 132

def add_file(args, desc)
  raise 'two or more config_file is defined' if @file_args
  args, defval, required, multiple = fix_args(args, desc)
  @file_args = args
end

#banner=(v) ⇒ Object



119
# File 'lib/optopus.rb', line 119

def banner=(v)         ; @parser.banner = v         ; end

#default_argv=(v) ⇒ Object



123
# File 'lib/optopus.rb', line 123

def default_argv=(v)   ; @parser.default_argv = v   ; end

#parse!Object



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 # file_args_checker

  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

#program_name=(v) ⇒ Object



120
# File 'lib/optopus.rb', line 120

def program_name=(v)   ; @parser.program_name = v   ; end

#release=(v) ⇒ Object



125
# File 'lib/optopus.rb', line 125

def release=(v)        ; @parser.release = v        ; end

#summary_indent=(v) ⇒ Object



122
# File 'lib/optopus.rb', line 122

def summary_indent=(v) ; @parser.summary_indent = v ; end

#summary_width=(v) ⇒ Object



121
# File 'lib/optopus.rb', line 121

def summary_width=(v)  ; @parser.psummary_width = v ; end

#version=(v) ⇒ Object



124
# File 'lib/optopus.rb', line 124

def version=(v)        ; @parser.version = v        ; end