diff --git a/lib/thor/parser/option.rb b/lib/thor/parser/option.rb index deac367f7..c91e708b3 100644 --- a/lib/thor/parser/option.rb +++ b/lib/thor/parser/option.rb @@ -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 @@ -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}? @@ -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! diff --git a/spec/parser/option_spec.rb b/spec/parser/option_spec.rb index 6a5b84eec..41299635f 100644 --- a/spec/parser/option_spec.rb +++ b/spec/parser/option_spec.rb @@ -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