From 1bcf46b7907d4fc566a1382ad4eda4d0e9354e63 Mon Sep 17 00:00:00 2001 From: Eugene Otto Date: Fri, 30 Sep 2022 02:24:13 -0700 Subject: Fix explicitly false booleans When a boolean option is explicitly set to `false`, e.g.: ```bash --option=false ``` it enters slop as the string value `'false'`. This commit updates the `BoolOption` option handler to interpret `'false'` and various other falsey values (`false`, `'false'`, `'no'`, `'off'`, `'0'`) as logically false. --- README.md | 6 ++++-- lib/slop/types.rb | 4 +++- test/types_test.rb | 8 +++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5d806f1..3876428 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,19 @@ opts = Slop.parse do |o| o.bool '-v', '--verbose', 'enable verbose mode' o.bool '-q', '--quiet', 'suppress output (quiet mode)' o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host' + o.bool '-k', '--use-keychain', 'store passphrase in OS keychain' o.on '--version', 'print the version' do puts Slop::VERSION exit end end -ARGV #=> -v --login alice --host 192.168.0.1 -m post --check-ssl-certificate +ARGV #=> -v --login alice --host 192.168.0.1 -m post --check-ssl-certificate --use-keychain false opts[:host] #=> 192.168.0.1 opts[:login] #=> alice opts[:method] #=> :post +opts[:use_keychain] #=> false opts.verbose? #=> true opts.quiet? #=> false opts.check_ssl_certificate? #=> true @@ -53,7 +55,7 @@ Built in Option types are as follows: ```ruby o.string #=> Slop::StringOption, expects an argument -o.bool #=> Slop::BoolOption, no argument, aliased to BooleanOption +o.bool #=> Slop::BoolOption, argument optional, aliased to BooleanOption o.integer #=> Slop::IntegerOption, expects an argument, aliased to IntOption o.float #=> Slop::FloatOption, expects an argument o.array #=> Slop::ArrayOption, expects an argument diff --git a/lib/slop/types.rb b/lib/slop/types.rb index 4ccd42e..83e4a0b 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -21,6 +21,8 @@ module Slop class BoolOption < Option attr_accessor :explicit_value + FALSE_VALUES = [false, 'false', 'no', 'off', '0'].freeze + def call(value) self.explicit_value = value !force_false? @@ -35,7 +37,7 @@ module Slop end def force_false? - explicit_value == false + FALSE_VALUES.include?(explicit_value) end def default_value diff --git a/test/types_test.rb b/test/types_test.rb index a45302a..ea2ba8d 100644 --- a/test/types_test.rb +++ b/test/types_test.rb @@ -34,9 +34,11 @@ describe Slop::BoolOption do @verbose = @options.bool "--verbose" @quiet = @options.bool "--quiet" @inversed = @options.bool "--inversed", default: true + @explicit = @options.bool "--explicit" @bloc = @options.bool("--bloc"){|val| (@bloc_val ||= []) << val} @result = @options.parse %w(--verbose --no-inversed - --bloc --no-bloc) + --bloc --no-bloc + --explicit=false) end it "returns true if used" do @@ -54,6 +56,10 @@ describe Slop::BoolOption do it "will invert the value passed to &block via --no- prefix" do assert_equal [true, false], @bloc_val end + + it "returns false when explicitly false" do + assert_equal false, @result[:explicit] + end end describe Slop::IntegerOption do -- cgit v1.2.1