Class: Thor::Task

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

Direct Known Subclasses

DynamicTask, HiddenTask

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Task.



5
6
7
# File 'lib/bundler/vendor/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

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



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

def description
  @description
end

#long_descriptionObject

Returns the value of attribute long_description

Returns:

  • (Object)

    the current value of long_description



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

def long_description
  @long_description
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



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

def options
  @options
end

#usageObject

Returns the value of attribute usage

Returns:

  • (Object)

    the current value of usage



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

def usage
  @usage
end

Instance Method Details

#formatted_usage(klass, namespace = true, subcommand = false) ⇒ Object

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bundler/vendor/thor/task.rb', line 33

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

  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

#hidden?Boolean

Returns:

  • (Boolean)


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

def hidden?
  false
end

#initialize_copy(other) ⇒ Object

:nodoc:



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

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

#run(instance, args = []) ⇒ Object

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
# File 'lib/bundler/vendor/thor/task.rb', line 20

def run(instance, args=[])
  public_method?(instance) ?
    instance.send(name, *args) : instance.class.handle_no_task_error(name)
rescue ArgumentError => e
  handle_argument_error?(instance, e, caller) ?
    instance.class.handle_argument_error(self, e) : (raise e)
rescue NoMethodError => e
  handle_no_method_error?(instance, e, caller) ?
    instance.class.handle_no_task_error(name) : (raise e)
end