Class: ConfigParser::Switch
- Defined in:
- lib/config_parser/switch.rb
Overview
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.
Constant Summary
Constants inherited from Option
Instance Attribute Summary collapse
-
#negative_long ⇒ Object
readonly
The negative long switch, determined from long.
Attributes inherited from Option
#arg_name, #block, #desc, #long, #short
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Switch
constructor
Initializes a new Switch.
-
#parse(switch, value, argv) ⇒ Object
Calls the block with false if the negative long is specified, or calls the block with true in all other cases.
-
#switches ⇒ Object
Returns an array of non-nil switches mapping to self (ie [long, negative_long, short]).
Methods inherited from Option
Constructor Details
#initialize(options = {}) ⇒ Switch
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.
15 16 17 18 19 20 |
# File 'lib/config_parser/switch.rb', line 15 def initialize(={}) 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 |
Instance Attribute Details
#negative_long ⇒ Object (readonly)
The negative long switch, determined from long.
10 11 12 |
# File 'lib/config_parser/switch.rb', line 10 def negative_long @negative_long end |
Instance Method Details
#parse(switch, value, argv) ⇒ Object
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.
31 32 33 34 35 |
# File 'lib/config_parser/switch.rb', line 31 def parse(switch, value, argv) raise "value specified for switch: #{switch}" if value value = (switch == negative_long ? false : true) block ? block.call(value) : value end |
#switches ⇒ Object
Returns an array of non-nil switches mapping to self (ie [long, negative_long, short]).
24 25 26 |
# File 'lib/config_parser/switch.rb', line 24 def switches [long, negative_long, short].compact end |