Sha256: bbf056557269f3cd58b7578b8b3f850c68fa861ce15a6fc11e554f2ebe2f9c50

Contents?: true

Size: 1.82 KB

Versions: 473

Compression:

Stored size: 1.82 KB

Contents

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

    attr_reader :name, :description, :enum, :required, :type, :default, :banner
    alias :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!
        if required? && !default.nil?
          raise ArgumentError, "An argument cannot be required and have default value."
        elsif @enum && !@enum.is_a?(Array)
          raise ArgumentError, "An argument cannot have an enum other than an array."
        end
      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

473 entries across 448 versions & 35 rubygems

Version Path
skylight-6.1.0.beta lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.4 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.3 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.3.5 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.2 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.1 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.0 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.0.beta2 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-6.0.0.beta lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.3.4 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.3.3 lib/skylight/vendor/cli/thor/parser/argument.rb
engineyard-serverside-3.0.4 lib/vendor/thor/lib/thor/parser/argument.rb
skylight-5.3.2 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.3.1 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.3.0 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-5.2.0 lib/skylight/vendor/cli/thor/parser/argument.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
skylight-5.2.0.beta2 lib/skylight/vendor/cli/thor/parser/argument.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
skylight-5.2.0.beta lib/skylight/vendor/cli/thor/parser/argument.rb