From b533e08902c9a23e7a796bdd643fb74a25d165dc Mon Sep 17 00:00:00 2001 From: empunkt <102794+nevesenin@users.noreply.github.com> Date: Mon, 12 Jun 2023 16:04:14 +0200 Subject: [PATCH 1/2] Print default in help when option type is :boolean and dafault is false --- lib/thor/parser/option.rb | 4 ++++ spec/parser/option_spec.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/thor/parser/option.rb b/lib/thor/parser/option.rb index f53d6d70..c8469511 100644 --- a/lib/thor/parser/option.rb +++ b/lib/thor/parser/option.rb @@ -104,6 +104,10 @@ def aliases_for_usage end end + def show_default? + super || [TrueClass, FalseClass].any? { |c| default.is_a?(c) } + end + VALID_TYPES.each do |type| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{type}? diff --git a/spec/parser/option_spec.rb b/spec/parser/option_spec.rb index 1b93f132..0f30b731 100644 --- a/spec/parser/option_spec.rb +++ b/spec/parser/option_spec.rb @@ -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 From c9e762a24b4b6ffcec70287a67c3f9d3e31aee1a Mon Sep 17 00:00:00 2001 From: empunkt <102794+nevesenin@users.noreply.github.com> Date: Tue, 20 Jun 2023 06:02:21 +0200 Subject: [PATCH 2/2] Implement suggested changes --- lib/thor/parser/option.rb | 7 ++++++- spec/parser/option_spec.rb | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/thor/parser/option.rb b/lib/thor/parser/option.rb index c8469511..b1a99431 100644 --- a/lib/thor/parser/option.rb +++ b/lib/thor/parser/option.rb @@ -105,7 +105,12 @@ def aliases_for_usage end def show_default? - super || [TrueClass, FalseClass].any? { |c| default.is_a?(c) } + case default + when TrueClass, FalseClass + true + else + super + end end VALID_TYPES.each do |type| diff --git a/spec/parser/option_spec.rb b/spec/parser/option_spec.rb index 0f30b731..54aba1bb 100644 --- a/spec/parser/option_spec.rb +++ b/spec/parser/option_spec.rb @@ -272,14 +272,14 @@ def option(name, options = {}) describe "#print_default" do it "prints boolean with true default value" do - expect(option(:foo, { + 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, { + expect(option(:foo, { required: false, type: :boolean, default: false