Sha256: 973ede9acd2fe35471cb31258fc3bdcca454c2f7bfaee15c62305b1eb1cb2b59

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

class ConfigParser
  
  # Switch represents a special type of Option where both a positive
  # (--switch) and negative (--no-switch) version of long should
  # map to self.  A short may be specified for Switch; it will always
  # be treated like the positive switch.
  class Switch < Option
    
    # The negative long switch, determined from long.
    attr_reader :negative_long
    
    # Initializes a new Switch.  Raises an error if an arg_name is
    # specified for self (as switches are intended to be boolean
    # in nature), or if no long option is specified.
    def initialize(options={})
      super
      raise ArgumentError, "arg_name specified for switch: #{arg_name}" if arg_name
      raise ArgumentError, "no long specified" unless long
      @negative_long = Utils.prefix_long(long, 'no-')
    end
    
    # Returns an array of non-nil switches mapping to self (ie 
    # [long, negative_long, short]).
    def switches
      [long, negative_long, short].compact
    end
    
    # Calls the block with false if the negative long is specified,
    # or calls the block with true in all other cases.  Raises an
    # error if a value is specified.
    def parse(switch, value, argv)
      raise "value specified for switch" if value
      value = (switch == negative_long ? false : true)
      block ? block.call(value) : value
    end

    private
    
    # helper returning long formatted for to_s
    def long_str # :nodoc:
      long ? Utils.prefix_long(long, '[no-]') : ''
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
configurable-0.4.0 lib/config_parser/switch.rb
configurable-0.3.0 lib/config_parser/switch.rb
configurable-0.4.1 lib/config_parser/switch.rb
configurable-0.4.2 lib/config_parser/switch.rb
configurable-0.5.0 lib/config_parser/switch.rb