Sha256: c2574f805d4040feed055d2bc6bb9c793ce3e2a9d94318d17d5ec39382f84f48

Contents?: true

Size: 1.6 KB

Versions: 252

Compression:

Stored size: 1.6 KB

Contents

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

    attr_reader :name, :description, :required, :type, :default, :banner
    alias :human_name :name

    def initialize(name, description=nil, required=true, type=:string, default=nil, banner=nil)
      class_name = self.class.name.split("::").last

      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 = description
      @required    = required || false
      @type        = (type || :string).to_sym
      @default     = default
      @banner      = banner || default_banner

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

252 entries across 198 versions & 22 rubygems

Version Path
arcabouco-0.2.13 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.5 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
classiccms-0.7.5 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.4 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.4 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
classiccms-0.7.3 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
classiccms-0.7.3 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
wip-0.3.0.pre2 lib/wip/vendor/thor/parser/argument.rb
wip-0.3.0.pre1 lib/wip/vendor/thor/parser/argument.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/thor-0.14.6/lib/thor/parser/argument.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/thor-0.14.6/lib/thor/parser/argument.rb
wijet-thor-0.14.10 lib/thor/parser/argument.rb
classiccms-0.7.2 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.2 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
classiccms-0.7.1 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
classiccms-0.7.1 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.0 vendor/bundle/gems/thor-0.14.6/lib/thor/parser/argument.rb
classiccms-0.7.0 vendor/bundle/gems/thor-0.15.2/lib/thor/parser/argument.rb
wijet-thor-0.14.9 lib/thor/parser/argument.rb
bundler-1.2.5 lib/bundler/vendor/thor/parser/argument.rb