Skip to content

Commit

Permalink
Support --no- prefix for inverting boolean options
Browse files Browse the repository at this point in the history
closes #168
  • Loading branch information
leejarvis committed May 21, 2015
1 parent 0caaa41 commit 191e3bb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ opts.int "-p", "--port", "a port", default: 80
opts.separator ""
opts.separator "Extra options:"
opts.array "--files", "a list of files to import"
opts.bool "-v", "--verbose", "enable verbose mode"
opts.bool "-v", "--verbose", "enable verbose mode", default: true

parser = Slop::Parser.new(opts)
result = parser.parse(["--hostname", "192.168.0.1"])
result = parser.parse(["--hostname", "192.168.0.1", "--no-verbose"])

result.to_hash #=> { hostname: "192.168.0.1", port: 80,
# files: [], verbose: false }
Expand Down
2 changes: 2 additions & 0 deletions lib/slop/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def process(option, arg)
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.
Expand Down
21 changes: 19 additions & 2 deletions lib/slop/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ def call(value)

# 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
Expand Down
13 changes: 9 additions & 4 deletions test/types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

describe Slop::BoolOption do
before do
@options = Slop::Options.new
@age = @options.bool "--verbose"
@age = @options.bool "--quiet"
@result = @options.parse %w(--verbose)
@options = Slop::Options.new
@verbose = @options.bool "--verbose"
@quiet = @options.bool "--quiet"
@inversed = @options.bool "--inversed", default: true
@result = @options.parse %w(--verbose --no-inversed)
end

it "returns true if used" do
Expand All @@ -15,6 +16,10 @@
it "returns false if not used" do
assert_equal false, @result[:quiet]
end

it "can be inversed via --no- prefix" do
assert_equal false, @result[:inversed]
end
end

describe Slop::IntegerOption do
Expand Down

0 comments on commit 191e3bb

Please sign in to comment.