Method: Puppet::Interface::Action#validate_and_clean

Defined in:
lib/puppet/interface/action.rb

#validate_and_clean(original) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/puppet/interface/action.rb', line 341

def validate_and_clean(original)
  # The final set of arguments; effectively a hand-rolled shallow copy of
  # the original, which protects the caller from the surprises they might
  # get if they passed us a hash and we mutated it...
  result = {}

  # Check for multiple aliases for the same option, and canonicalize the
  # name of the argument while we are about it.
  overlap = Hash.new do |h, k| h[k] = [] end
  unknown = []
  original.keys.each do |name|
    option = get_option(name)
    if option
      canonical = option.name
      if result.has_key? canonical
        overlap[canonical] << name
      else
        result[canonical] = original[name]
      end
    elsif Puppet.settings.include? name
      result[name] = original[name]
    else
      unknown << name
    end
  end

  unless overlap.empty?
    overlap_list = overlap.map { |k, v| "(#{k}, #{v.sort.join(', ')})" }.join(", ")
    raise ArgumentError, _("Multiple aliases for the same option passed: %{overlap_list}") %
                         { overlap_list: overlap_list }
  end

  unless unknown.empty?
    unknown_list = unknown.sort.join(", ")
    raise ArgumentError, _("Unknown options passed: %{unknown_list}") % { unknown_list: unknown_list }
  end

  # Inject default arguments and check for missing mandating options.
  missing = []
  options.map { |x| get_option(x) }.each do |option|
    name = option.name
    next if result.has_key? name

    if option.has_default?
      result[name] = option.default
    elsif option.required?
      missing << name
    end
  end

  unless missing.empty?
    missing_list = missing.sort.join(', ')
    raise ArgumentError, _("The following options are required: %{missing_list}") % { missing_list: missing_list }
  end

  # All done.
  result
end