Class: Slop::BoolOption
Overview
Cast the option argument to true or false. Override default_value to default to false instead of nil. This option type does not expect an argument. However, the API supports value being passed. This is to ensure it can capture an explicit false value
Constant Summary
collapse
- FALSE_VALUES =
[false, 'false', 'no', 'off', '0'].freeze
- TRUE_VALUES =
[true, 'true', 'yes', 'on', '1'].freeze
- VALID_VALUES =
(FALSE_VALUES + TRUE_VALUES).freeze
Constants inherited
from Option
Option::DEFAULT_CONFIG
Instance Attribute Summary collapse
Attributes inherited from Option
#block, #config, #count, #desc, #flags
Instance Method Summary
collapse
Methods inherited from Option
#ensure_call, #finish, #flag, #help?, #initialize, #key, #null?, #required?, #reset, #suppress_errors?, #tail, #tail?, #to_s, #underscore_flags?, #validate_type?
Constructor Details
This class inherits a constructor from Slop::Option
Instance Attribute Details
#explicit_value ⇒ Object
Returns the value of attribute explicit_value.
22
23
24
|
# File 'lib/slop/types.rb', line 22
def explicit_value
@explicit_value
end
|
Instance Method Details
#call(value) ⇒ Object
38
39
40
41
|
# File 'lib/slop/types.rb', line 38
def call(value)
self.explicit_value = value
!force_false?
end
|
#default_value ⇒ Object
55
56
57
|
# File 'lib/slop/types.rb', line 55
def default_value
config[:default] || false
end
|
#expects_argument? ⇒ Boolean
59
60
61
|
# File 'lib/slop/types.rb', line 59
def expects_argument?
false
end
|
#force_false? ⇒ Boolean
51
52
53
|
# File 'lib/slop/types.rb', line 51
def force_false?
FALSE_VALUES.include?(explicit_value)
end
|
#valid?(value) ⇒ Boolean
28
29
30
31
32
33
34
35
36
|
# File 'lib/slop/types.rb', line 28
def valid?(value)
return true unless config[:validate_type]
return true if value.is_a?(String) && value.start_with?("--")
value.nil? || VALID_VALUES.include?(value)
end
|
#value ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/slop/types.rb', line 43
def value
if force_false?
false
else
super
end
end
|