Module: Thor::Base

Included in:
Thor, Group
Defined in:
lib/thor/base.rb,
lib/thor/shell.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) args

Returns the value of attribute args



23
24
25
# File 'lib/thor/base.rb', line 23

def args
  @args
end

- (Object) options

Returns the value of attribute options



23
24
25
# File 'lib/thor/base.rb', line 23

def options
  @options
end

- (Object) parent_options

Returns the value of attribute parent_options



23
24
25
# File 'lib/thor/base.rb', line 23

def parent_options
  @parent_options
end

Class Method Details

+ (Object) included(base)

:nodoc:



84
85
86
87
88
# File 'lib/thor/base.rb', line 84

def included(base) #:nodoc:
  base.send :extend,  ClassMethods
  base.send :include, Invocation
  base.send :include, Shell
end

+ (Object) register_klass_file(klass)

Whenever a class inherits from Thor or Thor::Group, we should track the class and the file on Thor::Base. This is the method responsable for it.



111
112
113
114
115
116
117
# File 'lib/thor/base.rb', line 111

def register_klass_file(klass) #:nodoc:
  file = caller[1].match(/(.*):\d+/)[1]
  Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)

  file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
  file_subclasses << klass unless file_subclasses.include?(klass)
end

+ (Object) shell

Returns the shell used in all Thor classes. If you are in a Unix platform it will use a colored log, otherwise it will use a basic one without color.



8
9
10
11
12
13
14
15
16
# File 'lib/thor/shell.rb', line 8

def self.shell
  @shell ||= if ENV['THOR_SHELL'] && ENV['THOR_SHELL'].size > 0
    Thor::Shell.const_get(ENV['THOR_SHELL'])
  elsif ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw/) && !(ENV['ANSICON']))
    Thor::Shell::Basic
  else
    Thor::Shell::Color
  end
end

+ (Object) shell=(klass)

Sets the shell used in all Thor classes.



20
21
22
# File 'lib/thor/shell.rb', line 20

def self.shell=(klass)
  @shell = klass
end

+ (Object) subclass_files

Returns the files where the subclasses are kept.

Returns

Hash[path<String> => Class]



104
105
106
# File 'lib/thor/base.rb', line 104

def subclass_files
  @subclass_files ||= Hash.new{ |h,k| h[k] = [] }
end

+ (Object) subclasses

Returns the classes that inherits from Thor or Thor::Group.

Returns

Array



95
96
97
# File 'lib/thor/base.rb', line 95

def subclasses
  @subclasses ||= []
end

Instance Method Details

- (Base) initialize(args = [], options = {}, config = {})

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/thor/base.rb', line 41

def initialize(args=[], options={}, config={})
  parse_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.

  if options.is_a?(Array)
    task_options  = config.delete(:task_options) # hook for start
    parse_options = parse_options.merge(task_options) if task_options
    array_options, hash_options = options, {}
  else
    # Handle the case where the class was explicitly instantiated
    # with pre-parsed options.
    array_options, hash_options = [], 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.
  stop_on_unknown = self.class.stop_on_unknown_option? config[:current_task]
  opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown)
  self.options = opts.parse(array_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