Skip to content

Commit 15dd942

Browse files
committed
Allow to override the inverse name
1 parent 6f25cc6 commit 15dd942

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

lib/thor/parser/option.rb

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,7 @@ def human_name
8282
end
8383

8484
def usage(padding = 0)
85-
sample = if banner && !banner.to_s.empty?
86-
"#{switch_name}=#{banner}".dup
87-
else
88-
switch_name
89-
end
90-
91-
sample = "[#{sample}]".dup unless required?
92-
sample << ", [#{dasherize('no-' + human_name)}]" if inverse?
85+
sample = [ sample_banner, inverse_sample ].compact.join(", ")
9386

9487
if aliases.empty?
9588
(" " * padding) << sample
@@ -98,11 +91,6 @@ def usage(padding = 0)
9891
end
9992
end
10093

101-
def inverse?
102-
return false if (name == "force") || name.start_with?("no-")
103-
boolean? && @inverse.nil? || @inverse.eql?(true)
104-
end
105-
10694
VALID_TYPES.each do |type|
10795
class_eval <<-RUBY, __FILE__, __LINE__ + 1
10896
def #{type}?
@@ -113,6 +101,26 @@ def #{type}?
113101

114102
protected
115103

104+
def sample_banner
105+
sample_banner = if banner && !banner.to_s.empty?
106+
"#{switch_name}=#{banner}".dup
107+
else
108+
switch_name
109+
end
110+
required? ? sample_banner : "[#{sample_banner}]"
111+
end
112+
113+
def inverse_sample
114+
return unless boolean? && name !~ /^(force|no-.*)$/
115+
116+
case @inverse
117+
when Symbol, String
118+
"[#{dasherize(@inverse.to_s)}]"
119+
when nil, true
120+
"[#{dasherize('no-' + human_name)}]"
121+
end
122+
end
123+
116124
def validate!
117125
raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
118126
validate_default_type!

spec/parser/option_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def option(name, options = {})
123123
expect(option("bar", type: :boolean, :inverse => false).usage).to_not include("[--no-bar]")
124124
end
125125

126+
it "allow to override the inverse option" do
127+
expect(option("colorful", type: :boolean, :inverse => :monochromatic).usage).to include("[--monochromatic]")
128+
end
129+
126130
it "creates the inversion flag by default" do
127131
expect(option("bar", type: :boolean).usage).to include("[--no-bar]")
128132
end

0 commit comments

Comments
 (0)