Sha256: 48319e315b5a479726cc488b921a46a68e83202e50ebaea1382d552d1592b63b

Contents?: true

Size: 1.62 KB

Versions: 222

Compression:

Stored size: 1.62 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)
        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

222 entries across 222 versions & 12 rubygems

Version Path
engineyard-serverside-1.5.35.pre.1 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.33 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.32 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.6.0.pre5 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.30 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.6.0.pre4 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.6.0.pre3 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.6.0.pre2 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.6.0.pre lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre3 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre2 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre1 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre.timestamps2 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.29.pre.timestamps lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.28.pre.timestamps4 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.28.pre.timestamps3 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.28.pre.timestamps2 lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.28.pre.timestamps lib/vendor/thor/lib/thor/parser/argument.rb
engineyard-serverside-1.5.28 lib/vendor/thor/lib/thor/parser/argument.rb