123456789_123456789_123456789_123456789_123456789_

Class: Bundler::Thor::Argument

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/bundler/vendor/thor/lib/thor/parser/argument.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, options = {}) ⇒ Argument

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 8

def initialize(name, options = {})
  class_name = self.class.name.split("::").last

  type = options[:type]

  raise ArgumentError, "#{class_name} name can't be nil."                         if name.nil?
  raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s."  if type && !valid_type?(type)

  @name        = name.to_s
  @description = options[:desc]
  @required    = options.key?(:required) ? options[:required] : true
  @type        = (type || :string).to_sym
  @default     = options[:default]
  @banner      = options[:banner] || default_banner
  @enum        = options[:enum]

  validate! # Trigger specific validations
end

Instance Attribute Details

#default (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

#description (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

#enum (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

#human_name (readonly)

Alias for #name.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 6

alias_method :human_name, :name

#name (readonly) Also known as: #human_name

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

#required (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

#required?Boolean (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 42

def required?
  required
end

#show_default?Boolean (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 46

def show_default?
  case default
  when Array, String, Hash
    !default.empty?
  else
    default
  end
end

#type (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 5

attr_reader :name, :description, :enum, :required, :type, :default, :banner

Instance Method Details

#default_banner (protected)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 74

def default_banner
  case type
  when :boolean
    nil
  when :string, :default
    human_name.upcase
  when :numeric
    "N"
  when :hash
    "key:value"
  when :array
    "one two three"
  end
end

#enum_to_s

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 55

def enum_to_s
  if enum.respond_to? :join
    enum.join(", ")
  else
    "#{enum.first}..#{enum.last}"
  end
end

#usage

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 38

def usage
  required? ? banner : "[#{banner}]"
end

#valid_type?(type) ⇒ Boolean (protected)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 70

def valid_type?(type)
  self.class::VALID_TYPES.include?(type.to_sym)
end

#validate! (protected)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/parser/argument.rb', line 65

def validate!
  raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
  raise ArgumentError, "An argument cannot have an enum other than an enumerable." if @enum && !@enum.is_a?(Enumerable)
end