Sha256: 2dd5b8df1f3cb36907949843a35ad78ee90a5509ab78024cdc64382cef0871b6

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Clamp

  class Option

    def initialize(switches, argument_type, description, options = {})
      @switches = Array(switches)
      @argument_type = argument_type
      @description = description
      @attribute_name = options[:attribute_name].to_s if options.has_key?(:attribute_name)
    end

    attr_reader :switches, :argument_type, :description

    def attribute_name
      @attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
    end
    
    def long_switch
      switches.find { |switch| switch =~ /^--/ }
    end

    def handles?(switch)
      recognised_switches.member?(switch)
    end

    def flag?
      @argument_type == :flag
    end
    
    def flag_value(switch)
      !(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
    end
    
    def help
      lhs = switches.join(", ")
      lhs += " " + argument_type unless flag?
      [lhs, description]
    end

    private
    
    def recognised_switches
      switches.map do |switch|
        if switch =~ /^--\[no-\](.*)/
          ["--#{$1}", "--no-#{$1}"]
        else
          switch
        end
      end.flatten
    end
    
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clamp-0.0.1 lib/clamp/option.rb