From c63fff38124cdb0c7015b367518dcd061de88fc4 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Sat, 25 Jul 2020 14:16:14 -0300 Subject: [PATCH 1/7] Allow to suppress automatic creation of --no-boolean flags --- lib/thor/parser/option.rb | 11 +++++++---- spec/parser/option_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/thor/parser/option.rb b/lib/thor/parser/option.rb index 6a6761b6e..deac367f7 100644 --- a/lib/thor/parser/option.rb +++ b/lib/thor/parser/option.rb @@ -13,6 +13,7 @@ def initialize(name, options = {}) @group = options[:group].to_s.capitalize if options[:group] @aliases = Array(options[:aliases]) @hide = options[:hide] + @inverse = options[:inverse] end # This parse quick options given as method_options. It makes several @@ -88,10 +89,7 @@ def usage(padding = 0) end sample = "[#{sample}]".dup unless required? - - if boolean? - sample << ", [#{dasherize('no-' + human_name)}]" unless (name == "force") || name.start_with?("no-") - end + sample << ", [#{dasherize('no-' + human_name)}]" if inverse? if aliases.empty? (" " * padding) << sample @@ -100,6 +98,11 @@ 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}? diff --git a/spec/parser/option_spec.rb b/spec/parser/option_spec.rb index 5884c21a6..6a5b84eec 100644 --- a/spec/parser/option_spec.rb +++ b/spec/parser/option_spec.rb @@ -119,6 +119,18 @@ def option(name, options = {}) expect(option("--foo").switch_name).to eq("--foo") end + it "suppresses the creation of a --no-option when explicitly requested" do + expect(option("bar", type: :boolean, :inverse => false).usage).to_not include("[--no-bar]") + end + + it "creates the inversion flag by default" do + expect(option("bar", type: :boolean).usage).to include("[--no-bar]") + end + + it "creates the inversion flag when requested" do + expect(option("bar", type: :boolean, :inverse => true).usage).to include("[--no-bar]") + end + it "returns the human name" do expect(option("foo").human_name).to eq("foo") expect(option("--foo").human_name).to eq("foo") From 6f25cc6151a68c87075faf229e16452fdc91fe89 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Sat, 25 Jul 2020 14:20:21 -0300 Subject: [PATCH 2/7] Add documentation for new inverse option --- lib/thor.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/thor.rb b/lib/thor.rb index c84de3088..289d5b2c4 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -151,6 +151,7 @@ def method_options(options = nil) # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean. # :banner - String to show on usage notes. # :hide - If you want to hide this option from the help. + # :inverse - Set to false if you want to suppress --no-