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
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
skylight-4.3.0 lib/skylight/vendor/cli/thor/parser/argument.rb
skylight-4.2.3 lib/skylight/vendor/cli/thor/parser/argument.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
skylight-4.2.2 lib/skylight/vendor/cli/thor/parser/argument.rb
engineyard-serverside-2.8.0 lib/vendor/thor/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
engineyard-serverside-2.8.0.pre4 lib/vendor/thor/lib/thor/parser/argument.rb
skylight-4.2.1 lib/skylight/vendor/cli/thor/parser/argument.rb
engineyard-serverside-2.8.0.pre3 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-2.8.0.pre2 lib/vendor/thor/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
skylight-4.2.0 lib/skylight/vendor/cli/thor/parser/argument.rb
engineyard-serverside-2.8.0.pre lib/vendor/thor/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/thor-0.18.1/lib/thor/parser/argument.rb
engineyard-serverside-2.7.0.pre lib/vendor/thor/lib/thor/parser/argument.rb
skylight-4.2.0.beta3 lib/skylight/vendor/cli/thor/parser/argument.rb