Sha256: cbce7e0cbbe6cc972a33bf4851f51935f949e18c190b2dd21e4ab1a62bba0e56

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 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
        @keys.is_a?(Array) ? @keys[0] : @keys
      end

      def keys
        @keys.is_a?(Array) ? @keys : [@keys]
      end

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

1 entries across 1 versions & 1 rubygems

Version Path
kommand-0.0.1 lib/kommand/scripts/argument.rb