Skip to content

Commit

Permalink
Allow to override the inverse name
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Jun 1, 2021
1 parent 6f25cc6 commit 15dd942
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,7 @@ def human_name
end

def usage(padding = 0)
sample = if banner && !banner.to_s.empty?
"#{switch_name}=#{banner}".dup
else
switch_name
end

sample = "[#{sample}]".dup unless required?
sample << ", [#{dasherize('no-' + human_name)}]" if inverse?
sample = [ sample_banner, inverse_sample ].compact.join(", ")

if aliases.empty?
(" " * padding) << sample
Expand All @@ -98,11 +91,6 @@ def usage(padding = 0)
end
end

def inverse?
return false if (name == "force") || name.start_with?("no-")
boolean? && @inverse.nil? || @inverse.eql?(true)
end

VALID_TYPES.each do |type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{type}?
Expand All @@ -113,6 +101,26 @@ def #{type}?

protected

def sample_banner
sample_banner = if banner && !banner.to_s.empty?
"#{switch_name}=#{banner}".dup
else
switch_name
end
required? ? sample_banner : "[#{sample_banner}]"
end

def inverse_sample
return unless boolean? && name !~ /^(force|no-.*)$/

case @inverse
when Symbol, String
"[#{dasherize(@inverse.to_s)}]"
when nil, true
"[#{dasherize('no-' + human_name)}]"
end
end

def validate!
raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
validate_default_type!
Expand Down
4 changes: 4 additions & 0 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def option(name, options = {})
expect(option("bar", type: :boolean, :inverse => false).usage).to_not include("[--no-bar]")
end

it "allow to override the inverse option" do
expect(option("colorful", type: :boolean, :inverse => :monochromatic).usage).to include("[--monochromatic]")
end

it "creates the inversion flag by default" do
expect(option("bar", type: :boolean).usage).to include("[--no-bar]")
end
Expand Down

0 comments on commit 15dd942

Please sign in to comment.