Sha256: 7fc09f6fb35f9bbaca38b70b38a6f8f228ad8c84defaa68a31b8a2bb72170abe

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

require 'clamp/attribute_declaration'
require 'clamp/option'

module Clamp
  class Option

    module Declaration

      include Clamp::AttributeDeclaration

      def option(switches, type, description, opts = {}, &block)
        option = Clamp::Option.new(switches, type, description, opts)
        my_declared_options << option
        define_accessors_for(option, &block)
      end

      def has_options?
        !declared_options.empty?
      end

      def find_option(switch)
        recognised_options.find { |o| o.handles?(switch) }
      end

      protected 

      def declared_options
        my_declared_options + inherited_declared_options
      end

      private

      def recognised_options
        declared_options + standard_options
      end

      def my_declared_options
        @my_declared_options ||= []
      end

      def inherited_declared_options
        if superclass.respond_to?(:declared_options)
          superclass.declared_options
        else
          []
        end
      end

      HELP_OPTION = Clamp::Option.new("--help", :flag, "print help", :attribute_name => :help_requested)

      def standard_options
        [HELP_OPTION]
      end

    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
clamp-0.1.6 lib/clamp/option/declaration.rb
clamp-0.1.5 lib/clamp/option/declaration.rb
clamp-0.1.4 lib/clamp/option/declaration.rb
clamp-0.1.3 lib/clamp/option/declaration.rb
clamp-0.1.2 lib/clamp/option/declaration.rb
clamp-0.1.1 lib/clamp/option/declaration.rb
clamp-0.1.0 lib/clamp/option/declaration.rb