class Bundler::Thor::Task

常量

FILE_REGEXP

公共类方法

new(name, description, long_description, wrap_long_description, usage, options = nil, options_relation = nil) 点击切换源代码
调用父类方法
# File bundler/vendor/thor/lib/thor/command.rb, line 5
def initialize(name, description, long_description, wrap_long_description, usage, options = nil, options_relation = nil)
  super(name.to_s, description, long_description, wrap_long_description, usage, options || {}, options_relation || {})
end

公共实例方法

formatted_usage(klass, namespace = true, subcommand = false) 点击切换源代码

返回格式化后的用法,将给定的必需参数和必需选项注入到给定的用法中。

# File bundler/vendor/thor/lib/thor/command.rb, line 42
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

  Array(usage).map do |specific_usage|
    formatted_specific_usage = formatted

    formatted_specific_usage += required_arguments_for(klass, specific_usage)

    # Add required options
    formatted_specific_usage += " #{required_options}"

    # Strip and go!
    formatted_specific_usage.strip
  end.join("\n")
end
hidden?() 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 15
def hidden?
  false
end
run(instance, args = []) 点击切换源代码

默认情况下,命令会调用 thor 类中的方法。您可以更改此实现来创建自定义命令。

# File bundler/vendor/thor/lib/thor/command.rb, line 21
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

受保护的实例方法

handle_argument_error?(instance, error, caller) 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 114
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)
    saned.empty? || saned.size == 1
  end
end
handle_no_method_error?(instance, error, caller) 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 121
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) 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 104
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) 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 87
def not_debugging?(instance)
  !(instance.class.respond_to?(:debugging) && instance.class.debugging)
end
private_method?(instance) 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 100
def private_method?(instance)
  !(instance.private_methods & [name.to_s, name.to_sym]).empty?
end
required_arguments_for(klass, usage) 点击切换源代码

添加带必需参数的用法

# File bundler/vendor/thor/lib/thor/command.rb, line 77
def required_arguments_for(klass, usage)
  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
end
required_options() 点击切换源代码
# File bundler/vendor/thor/lib/thor/command.rb, line 91
def required_options
  @required_options ||= options.map { |_, o| o.usage if o.required? }.compact.sort.join(" ")
end