Class: CLIOptions

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/hyrum.rb

Class Method Summary collapse

Class Method Details

.build_and_validate(input) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hyrum.rb', line 44

def self.build_and_validate(input)
  # apply defaults and coercions
  cli_options = new(input)

  # validate the options
  contract_result = CLIOptionsContract.new.call(cli_options.to_h)

  if contract_result.errors.any?
    error_messages = contract_result.errors.to_h.map do |key, errors|
      error_text = errors.is_a?(Array) ? errors.join(', ') : errors
      "Error with #{key}: #{error_text}"
    end
    raise Hyrum::ScriptOptionsError, error_messages.join("\n")
  end

  contract_result
end