class Bundler::Thor::Task

Parent:
Struct.new(:name, :description, :long_description, :usage, :options, :ancestor_name)

Constants

FILE_REGEXP

Public Class Methods

new(name, description, long_description, usage, options = nil) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 5
def initialize(name, description, long_description, usage, options = nil)
  super(name.to_s, description, long_description, usage, options || {})
end
Calls superclass method

Public Instance Methods

formatted_usage(klass, namespace = true, subcommand = false) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 41
def formatted_usage(klass, namespace = true, subcommand = false)
  if ancestor_name
    formatted = "#{ancestor_name} ".dup # add space
  elsif namespace
    namespace = klass.namespace
    formatted = "#{namespace.gsub(/^(default)/, '')}:".dup
  end
  formatted ||= "#{klass.namespace.split(':').last} ".dup if subcommand

  formatted ||= "".dup

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

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

  # Strip and go!
  formatted.strip
end

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

hidden?() Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 14
def hidden?
  false
end
run(instance, args = []) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 20
def run(instance, args = [])
  arity = nil

  if private_method?(instance)
    instance.class.handle_no_command_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_command_error(name)
  end
rescue ArgumentError => e
  handle_argument_error?(instance, e, caller) ? instance.class.handle_argument_error(self, e, args, arity) : (raise e)
rescue NoMethodError => e
  handle_no_method_error?(instance, e, caller) ? instance.class.handle_no_command_error(name) : (raise e)
end

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

Protected Instance Methods

handle_argument_error?(instance, error, caller) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 97
def handle_argument_error?(instance, error, caller)
  not_debugging?(instance) && (error.message =~ /wrong number of arguments/ || error.message =~ /given \d*, expected \d*/) && begin
    saned = sans_backtrace(error.backtrace, caller)
    # Ruby 1.9 always include the called method in the backtrace
    saned.empty? || (saned.size == 1 && RUBY_VERSION >= "1.9")
  end
end
handle_no_method_error?(instance, error, caller) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 105
def handle_no_method_error?(instance, error, caller)
  not_debugging?(instance) &&
    error.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
end
local_method?(instance, name) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 87
def local_method?(instance, name)
  methods = instance.public_methods(false) + instance.private_methods(false) + instance.protected_methods(false)
  !(methods & [name.to_s, name.to_sym]).empty?
end
not_debugging?(instance) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 70
def not_debugging?(instance)
  !(instance.class.respond_to?(:debugging) && instance.class.debugging)
end
private_method?(instance) Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 83
def private_method?(instance)
  !(instance.private_methods & [name.to_s, name.to_sym]).empty?
end
required_options() Show source
# File lib/bundler/vendor/thor/lib/thor/command.rb, line 74
def required_options
  @required_options ||= options.map { |_, o| o.usage if o.required? }.compact.sort.join(" ")
end

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.