diff options
author | Lee Jarvis <ljjarvis@gmail.com> | 2015-05-21 09:12:14 +0100 |
---|---|---|
committer | Lee Jarvis <ljjarvis@gmail.com> | 2015-05-21 09:13:31 +0100 |
commit | 191e3bb386fdb1b8c4e87e9d2504d06b630d147d (patch) | |
tree | 7f09b814b4b68121cc3b82f8350562a20f578f13 /lib | |
parent | 0caaa41b203cab8c577f861229407848d2daf7f7 (diff) | |
download | slop-191e3bb386fdb1b8c4e87e9d2504d06b630d147d.tar.gz |
Support --no- prefix for inverting boolean options
closes #168
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/parser.rb | 2 | ||||
-rw-r--r-- | lib/slop/types.rb | 21 |
2 files changed, 21 insertions, 2 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index 70994a7..1bfa134 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -92,6 +92,8 @@ module Slop def try_process(flag, arg) if option = matching_option(flag) process(option, arg) + elsif flag.start_with?("--no-") && option = matching_option(flag.sub("no-", "")) + process(option, false) elsif flag =~ /\A-[^-]/ && flag.size > 2 # try and process as a set of grouped short flags. drop(1) removes # the prefixed -, then we add them back to each flag separately. diff --git a/lib/slop/types.rb b/lib/slop/types.rb index ad7461c..672aef3 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -8,12 +8,29 @@ module Slop # 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. + # 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 class BoolOption < Option - def call(_value) + attr_accessor :explicit_value + + def call(value) + self.explicit_value = value true end + def value + if force_false? + false + else + super + end + end + + def force_false? + explicit_value == false + end + def default_value config[:default] || false end |