Skip to content

Commit

Permalink
passing :opt => true or :opt => false now sets the default boolean va…
Browse files Browse the repository at this point in the history
…lue. :opt => :boolean will not set anything. This change the API slightly but allows you to now specify a default status for a flag
  • Loading branch information
Charles Jolley committed Jan 12, 2009
1 parent 479e5a8 commit bb6de6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/thor/options.rb
Expand Up @@ -95,7 +95,12 @@ def initialize(switches)

# normalize type
case type
when TrueClass then type = :boolean
when TrueClass
@defaults[nice_name] = true
type = :boolean
when FalseClass
@defaults[nice_name] = false
type = :boolean
when String
@defaults[nice_name] = type
type = :optional
Expand Down
12 changes: 5 additions & 7 deletions spec/options_spec.rb
Expand Up @@ -18,7 +18,7 @@ def parse(*args)
end

it "doesn't auto-alias switches that have multiple names given" do
create ["--foo", "--bar"] => true
create ["--foo", "--bar"] => :boolean
parse("-f")["foo"].must_not be
end

Expand Down Expand Up @@ -51,13 +51,13 @@ def parse(*args)
end

it "accepts --[no-]opt variant for booleans, setting false for value" do
create "--foo" => :boolean
create "--foo" => false
parse("--foo")["foo"].must == true
parse("--no-foo")["foo"].must == false
end

it "will prefer 'no-opt' variant over inverting 'opt' if explicitly set" do
create "--no-foo" => :boolean
create "--no-foo" => true
parse("--no-foo")["no-foo"].must == true
end

Expand Down Expand Up @@ -112,7 +112,7 @@ def parse(*args)
end

it "and several switches returns an empty hash" do
create "--foo" => true, "--bar" => :optional
create "--foo" => :boolean, "--bar" => :optional
parse.must == {}
end

Expand All @@ -123,12 +123,10 @@ def parse(*args)
end

it "doesn't set nonexistant switches" do
create "--foo" => true, "--bar" => :boolean
create "--foo" => :boolean
parse("--foo")["bar"].must_not be
parse("--bar")["foo"].must_not be
opts = parse
opts["foo"].must_not be
opts["bar"].must_not be
end

describe " with several optional switches" do
Expand Down

0 comments on commit bb6de6a

Please sign in to comment.