Sha256: 3d1b02adc289e108ea7dc5ce36d14ed61253d9e1ed8b1db3b6027bf1a6586efd

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

module Kommand
  module Scripts
    class Argument
      attr_reader :value, :valid, :summary

      def value=(val)
        raise "Invalid value for `#{key}` argument: '#{val}'" if (!@valid.nil? && !@valid.include?(val)) && (!val.nil? && !val.empty?)
        @value = val
      end

      def key
        if unnamed?
          value
        else
          @keys.is_a?(Array) ? @keys[0] : @keys
        end
      end

      def keys
        if unnamed?
          [value]
        else
          @keys.is_a?(Array) ? @keys : [@keys]
        end
      end

      def name
        keys.sort { |a,b| a.length <=> b.length }.last.gsub(/^--/, '')
      end

      # is this an unnamed argument?
      def unnamed?
        @keys == nil
      end

      # is this argument valid?
      def valid?
        !((validate? && !@valid.include?(val)) && (!val.nil? && !val.empty?))
      end

      # should this argument validate?
      def validate?
        !@valid.nil?
      end

      protected

      def initialize(keys, *args)
        @keys = (keys.is_a?(String) && keys.match(/,?\s/) ? keys.gsub(/,?\s+/, " ").split(" ") : keys)
        
        val = args.slice!(0) if %w(String Symbol Integer).include?(args[0].class.name)

        if args.length > 0 && args[0].is_a?(Hash)
          opts = args.slice!(0)
          @valid = (opts[:valid].is_a?(Array) ? opts[:valid].map(&:to_s) : [opts[:valid].to_s]) if opts.has_key?(:valid)
          @summary = opts[:summary] if opts.has_key?(:summary)
        end
        
        self.value = val
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kommand-0.0.4 lib/kommand/scripts/argument.rb
kommand-0.0.3 lib/kommand/scripts/argument.rb
kommand-0.0.2 lib/kommand/scripts/argument.rb