module ThorAddons::Options

Attributes

current_command_name[R]
defaults[R]
invocations[R]

Public Class Methods

new(args = [], local_options = {}, config = {}) click to toggle source
Calls superclass method
# File lib/thor-addons/options.rb, line 7
def initialize(args = [], local_options = {}, config = {})
  @defaults = Helpers::Defaults.load(self.class, config.dup)
  @invocations = config[:invocations]

  if config[:current_command].respond_to?(:name)
    @current_command_name = config[:current_command].name
  end

  super(args, local_options, config)
end

Public Instance Methods

envs_aliases() click to toggle source
# File lib/thor-addons/options.rb, line 26
def envs_aliases
  {}
end
options() click to toggle source
Calls superclass method
# File lib/thor-addons/options.rb, line 30
def options
  original = Helpers::OptionsHash.new(super)

  return original unless with_env? || with_config_file?

  new_options = Helpers::Defaults.remove(original, defaults)
  update_options!(new_options, original[:config_file])

  opts = Helpers::OptionsHash.new(
    Helpers::Defaults.add(new_options, defaults)
  )

  validate_options(opts)
end
with_config_file?() click to toggle source
# File lib/thor-addons/options.rb, line 22
def with_config_file?
  true
end
with_env?() click to toggle source
# File lib/thor-addons/options.rb, line 18
def with_env?
  true
end

Private Instance Methods

update_config_options!(opts, cfg_file) click to toggle source
# File lib/thor-addons/options.rb, line 50
        def update_config_options!(opts, cfg_file)
  opts.merge!(
    Helpers::OptionsConfigFile.parse(
      cfg_file,
      invocations,
      current_command_name,
      defaults
    )
  )
end
update_env_options!(opts) click to toggle source
# File lib/thor-addons/options.rb, line 61
        def update_env_options!(opts)
  opts.merge!(Helpers::OptionsENV.parse(defaults, envs_aliases))
end
update_options!(opts, cfg_file) click to toggle source
# File lib/thor-addons/options.rb, line 45
        def update_options!(opts, cfg_file)
  update_config_options!(opts, cfg_file) if with_config_file? && cfg_file
  update_env_options!(opts) if with_env?
end
validate_options(opts) click to toggle source
# File lib/thor-addons/options.rb, line 65
        def validate_options(opts)
  opts.each do |key, value|
    type = defaults[key][:type]
    is_valid = Helpers::OptionType.new(value, type).valid?

    raise TypeError, "'#{key}' should be a #{type}" unless is_valid
  end
end