Class: Thor::Task

Inherits:
Struct
  • Object
show all
Defined in:
lib/thor/task.rb

Direct Known Subclasses

DynamicTask, HiddenTask

Constant Summary

FILE_REGEXP =
/^#{Regexp.escape(File.dirname(__FILE__))}/

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Task) initialize(name, description, long_description, usage, options = nil)



5
6
7
# File 'lib/thor/task.rb', line 5

def initialize(name, description, long_description, usage, options=nil)
  super(name.to_s, description, long_description, usage, options || {})
end

Instance Attribute Details

- (Object) description

Returns the value of attribute description



2
3
4
# File 'lib/thor/task.rb', line 2

def description
  @description
end

- (Object) long_description

Returns the value of attribute long_description



2
3
4
# File 'lib/thor/task.rb', line 2

def long_description
  @long_description
end

- (Object) name

Returns the value of attribute name



2
3
4
# File 'lib/thor/task.rb', line 2

def name
  @name
end

- (Object) options

Returns the value of attribute options



2
3
4
# File 'lib/thor/task.rb', line 2

def options
  @options
end

- (Object) usage

Returns the value of attribute usage



2
3
4
# File 'lib/thor/task.rb', line 2

def usage
  @usage
end

Instance Method Details

- (Object) formatted_usage(klass, namespace = true, subcommand = false)

Returns the formatted usage by injecting given required arguments and required options into the given usage.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/thor/task.rb', line 43

def formatted_usage(klass, namespace = true, subcommand = false)
  if namespace
    namespace = klass.namespace
    formatted = "#{namespace.gsub(/^(default)/,'')}:"
  end
  formatted = "#{klass.namespace.split(':').last} " if subcommand

  formatted ||= ""

  # Add usage with required arguments
  formatted << if klass && !klass.arguments.empty?
    usage.to_s.gsub(/^#{name}/) do |match|
      match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
    end
  else
    usage.to_s
  end

  # Add required options
  formatted << " #{required_options}"

  # Strip and go!
  formatted.strip
end

- (Boolean) hidden?



14
15
16
# File 'lib/thor/task.rb', line 14

def hidden?
  false
end

- (Object) initialize_copy(other)

:nodoc:



9
10
11
12
# File 'lib/thor/task.rb', line 9

def initialize_copy(other) #:nodoc:
  super(other)
  self.options = other.options.dup if other.options
end

- (Object) run(instance, args = [])

By default, a task invokes a method in the thor class. You can change this implementation to create custom tasks.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thor/task.rb', line 20

def run(instance, args=[])
  arity = nil

  if private_method?(instance)
    instance.class.handle_no_task_error(name)
  elsif public_method?(instance)
    arity = instance.method(name).arity
    instance.__send__(name, *args)
  elsif local_method?(instance, :method_missing)
    instance.__send__(:method_missing, name.to_sym, *args)
  else
    instance.class.handle_no_task_error(name)
  end
rescue ArgumentError => e
  handle_argument_error?(instance, e, caller) ?
    instance.class.handle_argument_error(self, e, arity) : (raise e)
rescue NoMethodError => e
  handle_no_method_error?(instance, e, caller) ?
    instance.class.handle_no_task_error(name) : (raise e)
end