module Thor::Base

Public Class Methods

new(args = [], local_options = {}, config = {}) click to toggle source

It receives arguments in an Array and two hashes, one for options and other for configuration.

Notice that it does not check if all required arguments were supplied. It should be done by the parser.

Parameters

args>

An array of objects. The objects are applied to their respective accessors declared with argument.

options<Hash>

An options hash that will be available as self.options. The hash given is converted to a hash with indifferent access, magic predicates (options.skip?) and then frozen.

config<Hash>

Configuration for this Thor class.

# File lib/thor/rich_options/base.rb, line 26
def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength
  parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options

  # The start method splits inbound arguments at the first argument
  # that looks like an option (starts with - or --). It then calls
  # new, passing in the two halves of the arguments Array as the
  # first two parameters.

  command_options = config.delete(:command_options) # hook for start
  parse_options = parse_options.merge(command_options) if command_options
  if local_options.is_a?(Array)
    array_options, hash_options = local_options, {}
  else
    # Handle the case where the class was explicitly instantiated
    # with pre-parsed options.
    array_options, hash_options = [], local_options
  end

  # Let Thor::Options parse the options first, so it can remove
  # declared options from the array. This will leave us with
  # a list of arguments that weren't declared.
  current_command = config[:current_command]
  stop_on_unknown = self.class.stop_on_unknown_option? current_command

  # Give a relation of options.
  # After parsing, Thor::Options check whether right relations are kept
  relations = {:exclusive_option_names => [], :at_least_one_option_names => []}
  unless current_command.nil? or current_command.options_relation.nil?
    relations = current_command.options_relation
  end

  self.class.class_exclusive_option_names.map{ |n| relations[:exclusive_option_names] << n}
  self.class.class_at_least_one_option_names.map{ |n| relations[:at_least_one_option_names] << n}

  opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown, relations)

  self.options = opts.parse(array_options)
  self.options = config[:class_options].merge(options) if config[:class_options]

  # If unknown options are disallowed, make sure that none of the
  # remaining arguments looks like an option.
  opts.check_unknown! if self.class.check_unknown_options?(config)

  # Add the remaining arguments from the options parser to the
  # arguments passed in to initialize. Then remove any positional
  # arguments declared using #argument (this is primarily used
  # by Thor::Group). Tis will leave us with the remaining
  # positional arguments.
  to_parse  = args
  to_parse += opts.remaining unless self.class.strict_args_position?(config)

  thor_args = Thor::Arguments.new(self.class.arguments)
  thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
  @args = thor_args.remaining
end