Sha256: 21bd796d852885ef914b8c8b693fc9d400105aa84276b91d2370021aa5818e17

Contents?: true

Size: 1.75 KB

Versions: 328

Compression:

Stored size: 1.75 KB

Contents

class Bundler::Thor
  class Argument #:nodoc:
    VALID_TYPES = [:numeric, :hash, :array, :string]

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

    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

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

    def required?
      required
    end

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

  protected

    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 array." if @enum && !@enum.is_a?(Array)
    end

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

    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
  end
end

Version data entries

328 entries across 328 versions & 7 rubygems

Version Path
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/parser/argument.rb