Method: AppKernel::Function::Options#canonicalize

Defined in:
lib/appkernel/function.rb

#canonicalize(args, errors, augment = true) ⇒ Object

first, exctract all hash options if we find one we love, use it otherwise, if there’s a slurpy option, throw it on the pile otherwise, it’s an error.



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
# File 'lib/appkernel/function.rb', line 183

def canonicalize(args, errors, augment = true)
  indexed = @indexed.compact
  positionals, parameters, rest = comb(args)
  unless @greedy
    errors.add(nil,"too many arguments (#{positionals.length} for #{indexed.length})") if positionals.length > indexed.length
    for hash in rest
      for k,v in hash
        errors.add(k, "unknown option '#{k}'")
      end
    end
    return unless errors.empty?
  end
  @presets.dup.tap do |canonical|
    canonical[@greedy.name] = rest if @greedy
    for name,value in parameters
      canonical[name] = @options[name].resolve(value)
    end          
    positionals.length.times do |i|          
      if opt = indexed[i]
        canonical[opt.name] = opt.resolve(positionals[i])
      end
    end
    if @greedy
      canonical[@greedy.name] = @greedy.resolve(rest)
    end          
    if augment
      for k in @defaults
        canonical[k] = @options[k].default unless canonical[k]
      end
      canonical.reject! {|k,v| v.nil? || (@options[k] && @options[k].list? && v.empty?)}
      for k in @required - canonical.keys
        errors.add(k, "missing required option '#{k}'")
      end
    end          
  end      
end