Skip to content

Commit

Permalink
Merge pull request #849 from nevesenin/bool_false_default
Browse files Browse the repository at this point in the history
Print default in help when option type is :boolean and default is false
  • Loading branch information
rafaelfranca authored Jun 20, 2023
2 parents 9b9db75 + c9e762a commit 5fb6206
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ def aliases_for_usage
end
end

def show_default?
case default
when TrueClass, FalseClass
true
else
super
end
end

VALID_TYPES.each do |type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{type}?
Expand Down
17 changes: 17 additions & 0 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,21 @@ def option(name, options = {})
end
end
end

describe "#print_default" do
it "prints boolean with true default value" do
expect(option(:foo, {
required: false,
type: :boolean,
default: true
}).print_default).to eq(true)
end
it "prints boolean with false default value" do
expect(option(:foo, {
required: false,
type: :boolean,
default: false
}).print_default).to eq(false)
end
end
end

0 comments on commit 5fb6206

Please sign in to comment.