diff options
author | Lee Jarvis <leejarvis@fastmail.com> | 2023-02-26 18:25:59 +0000 |
---|---|---|
committer | Lee Jarvis <leejarvis@fastmail.com> | 2023-02-26 18:25:59 +0000 |
commit | de796008bba708263b5cf093b48af7a6ebeb1d1a (patch) | |
tree | b9de37ecb366535a05073b292e4b7f47c564ced2 | |
parent | 274e35b55c1cf4de653b0b408f8bc363ca04cd12 (diff) | |
download | slop-de796008bba708263b5cf093b48af7a6ebeb1d1a.tar.gz |
Fix booleans without validate_type with arguments
If we're not using `validate_type`, we shouldn't try to check a boolean
flag's argument to check if it's valid (actually, we probably shouldn't
even do that WITH using `validate_type`, but that's a separate issue).
For now, this will prevent broken boolean flags.
Fixes #279
-rw-r--r-- | lib/slop/types.rb | 5 | ||||
-rw-r--r-- | test/types_test.rb | 7 |
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/slop/types.rb b/lib/slop/types.rb index a08537d..d68fe76 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -26,6 +26,11 @@ module Slop VALID_VALUES = (FALSE_VALUES + TRUE_VALUES).freeze def valid?(value) + # If we don't want to validate the type, then we don't care if the value + # is valid or not. Otherwise we would prevent boolean flags followed by + # arguments from being parsed correctly. + return true unless config[:validate_type] + return true if value.is_a?(String) && value.start_with?("--") value.nil? || VALID_VALUES.include?(value) end diff --git a/test/types_test.rb b/test/types_test.rb index c6f0864..05e6265 100644 --- a/test/types_test.rb +++ b/test/types_test.rb @@ -66,6 +66,13 @@ describe Slop::BoolOption do @result.parser.parse %w(--verbose foo) end end + + # Like above but without validate_type + it "returns true if used and ignores the value" do + @result.parser.parse %w(--quiet foo) + + assert_equal true, @result[:quiet] + end end describe Slop::IntegerOption do |