Class: ConfigParser::Switch

Inherits:
Flag
  • Object
show all
Defined in:
lib/config_parser/switch.rb

Overview

Switch represents a special type of Option where both positive (–flag) and negative (–no-flag) flags map to self.

Constant Summary

Constants included from Utils

Utils::DELIMITER, Utils::LONG_FLAG, Utils::NEST, Utils::OPTION, Utils::OPTION_BREAK, Utils::SHORT_FLAG, Utils::SWITCH

Instance Attribute Summary collapse

Attributes inherited from Flag

#assigned, #callback, #default, #desc, #hint, #key, #long, #nest_keys, #short

Instance Method Summary collapse

Methods inherited from Flag

#assign, #assign_default, #inspect, #nest, #process, #reset, #to_s

Methods included from Utils

guess_hint, guess_option_type, guess_option_type_by_arg_name, guess_option_type_by_value, longify, next_arg, option?, parse_attrs, prefix_long, shortify, wrap

Constructor Details

#initialize(attrs = {}) ⇒ Switch

Returns a new instance of Switch.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/config_parser/switch.rb', line 15

def initialize(attrs={})
  attrs[:default] = true unless attrs.has_key?(:default)
  super

  raise ArgumentError, "no long specified" unless long
  @prefix = attrs[:prefix] || 'no'
  @negative_long = prefix_long(long, "#{prefix}-")
end

Instance Attribute Details

#negative_longObject (readonly)

The negative long flag, determined from long and prefix.



13
14
15
# File 'lib/config_parser/switch.rb', line 13

def negative_long
  @negative_long
end

#prefixObject (readonly)

The negative mapping prefix, defaults to ‘no’



10
11
12
# File 'lib/config_parser/switch.rb', line 10

def prefix
  @prefix
end

Instance Method Details

#flagsObject

Returns an array of flags mapping to self (ie [long, negative_long, short]).



25
26
27
# File 'lib/config_parser/switch.rb', line 25

def flags
  [long, negative_long, short].compact
end

#parse(flag, value = nil, argv = [], config = {}) ⇒ Object

Assigns default into config for positive flags and !default for negative flags. The boolean value is then processed and assigned into config. Raises an error if a value is provided (switches take none).



32
33
34
35
36
37
# File 'lib/config_parser/switch.rb', line 32

def parse(flag, value=nil, argv=[], config={})
  raise "value specified for #{flag}: #{value.inspect}" if value

  value = (flag == negative_long ? !default : default)
  assign(config, process(value))
end