Skip to content

Commit c63fff3

Browse files
committed
Allow to suppress automatic creation of --no-boolean flags
1 parent ce18deb commit c63fff3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/thor/parser/option.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(name, options = {})
1313
@group = options[:group].to_s.capitalize if options[:group]
1414
@aliases = Array(options[:aliases])
1515
@hide = options[:hide]
16+
@inverse = options[:inverse]
1617
end
1718

1819
# This parse quick options given as method_options. It makes several
@@ -88,10 +89,7 @@ def usage(padding = 0)
8889
end
8990

9091
sample = "[#{sample}]".dup unless required?
91-
92-
if boolean?
93-
sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-")
94-
end
92+
sample << ", [#{dasherize('no-' + human_name)}]" if inverse?
9593

9694
if aliases.empty?
9795
(" " * padding) << sample
@@ -100,6 +98,11 @@ def usage(padding = 0)
10098
end
10199
end
102100

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

spec/parser/option_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ def option(name, options = {})
119119
expect(option("--foo").switch_name).to eq("--foo")
120120
end
121121

122+
it "suppresses the creation of a --no-option when explicitly requested" do
123+
expect(option("bar", type: :boolean, :inverse => false).usage).to_not include("[--no-bar]")
124+
end
125+
126+
it "creates the inversion flag by default" do
127+
expect(option("bar", type: :boolean).usage).to include("[--no-bar]")
128+
end
129+
130+
it "creates the inversion flag when requested" do
131+
expect(option("bar", type: :boolean, :inverse => true).usage).to include("[--no-bar]")
132+
end
133+
122134
it "returns the human name" do
123135
expect(option("foo").human_name).to eq("foo")
124136
expect(option("--foo").human_name).to eq("foo")

0 commit comments

Comments
 (0)