Sha256: 76511ec81645bfde34b27c9e33e792a0be159c9eed677840dd4cf494e7fc1918
Contents?: true
Size: 1.69 KB
Versions: 1
Compression:
Stored size: 1.69 KB
Contents
require 'clamp/option' module Clamp module OptionSupport def option(switches, argument_type, description, opts = {}, &block) option = Clamp::Option.new(switches, argument_type, description, opts) declare_option(option, &block) end def has_options? !declared_options.empty? end def declared_options my_declared_options + inherited_declared_options end def recognised_options declared_options + standard_options end def find_option(switch) recognised_options.find { |o| o.handles?(switch) } end private def my_declared_options @my_declared_options ||= [] end def declare_option(option, &block) my_declared_options << option declare_option_reader(option) declare_option_writer(option, &block) 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 def declare_option_reader(option) reader_name = option.attribute_name reader_name += "?" if option.flag? define_method(reader_name) do value = instance_variable_get("@#{option.attribute_name}") value = option.default_value if value.nil? value end end def declare_option_writer(option, &block) define_method("#{option.attribute_name}=") do |value| if block value = instance_exec(value, &block) end instance_variable_set("@#{option.attribute_name}", value) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
clamp-0.0.7 | lib/clamp/option_support.rb |