Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ 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 - false if you don't want an inverse option of your boolean type, String or Symbol
# if you want to override the inverse option name.
#
def method_option(name, options = {})
unless [ Symbol, String ].any? { |klass| name.is_a?(klass) }
Expand Down
49 changes: 38 additions & 11 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(name, options = {})
@group = options[:group].to_s.capitalize if options[:group]
@aliases = normalize_aliases(options[:aliases])
@hide = options[:hide]
@inverse = options[:inverse]
end

# This parse quick options given as method_options. It makes several
Expand Down Expand Up @@ -81,17 +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?

if boolean? && name != "force" && !name.match(/\A(no|skip)[\-_]/)
sample << ", [#{dasherize('no-' + human_name)}], [#{dasherize('skip-' + human_name)}]"
end
sample = [ sample_banner, inverse_sample ].compact.join(", ")

aliases_for_usage.ljust(padding) + sample
end
Expand All @@ -113,6 +104,23 @@ def show_default?
end
end

# The switch given through :inverse that sets a boolean option to false,
# replacing the default --no-<option> and --skip-<option> switches.
def inverse_switch_name
return unless boolean?

case @inverse
when Symbol, String
dasherize(@inverse.to_s)
end
end

# Whether --no-<option> and --skip-<option> can be used to set this option
# to false. Only boolean options can suppress them through :inverse.
def default_inverse?
!boolean? || @inverse.nil? || @inverse == true
end

VALID_TYPES.each do |type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{type}?
Expand All @@ -123,6 +131,25 @@ 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 if !boolean? || name == "force" || name.match(/\A(no|skip)[\-_]/)

if inverse_switch_name
"[#{inverse_switch_name}]"
elsif default_inverse?
"[#{dasherize('no-' + human_name)}], [#{dasherize('skip-' + human_name)}]"
end
end

def validate!
raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
validate_default_type!
Expand Down
26 changes: 19 additions & 7 deletions lib/thor/parser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false, disabl

@shorts = {}
@switches = {}
@inverses = {}
@extra = []
@stopped_parsing_after_extra_index = nil
@is_treated_as_value = false

options.each do |option|
@switches[option.switch_name] = option
@inverses[option.inverse_switch_name] = option if option.inverse_switch_name

option.aliases.each do |name|
@shorts[name] ||= option.switch_name
Expand Down Expand Up @@ -232,11 +234,21 @@ def switch?(arg)
end

def switch_option(arg)
if match = no_or_skip?(arg) # rubocop:disable Lint/AssignmentInCondition
@switches[arg] || @switches["--#{match}"]
else
@switches[arg]
end
option = @switches[arg] || @inverses[arg]
return option if option

match = no_or_skip?(arg)
option = @switches["--#{match}"] if match
option if option && option.default_inverse?
end

# Check if the given switch sets a boolean option to false, either through
# --no-<option>, --skip-<option> or a custom :inverse switch.
#
def inverse_switch?(switch)
return false if @switches.key?(switch)

@inverses.key?(switch) || !no_or_skip?(switch).nil?
end

# Check if the given argument is actually a shortcut.
Expand All @@ -262,10 +274,10 @@ def parse_boolean(switch)
shift
false
else
@switches.key?(switch) || !no_or_skip?(switch)
!inverse_switch?(switch)
end
else
@switches.key?(switch) || !no_or_skip?(switch)
!inverse_switch?(switch)
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ def option(name, options = {})
expect(option(:foo, required: false, type: :string, banner: "").usage).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 eq("[--bar]")
end

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

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

it "creates the inversion flag when requested" do
expect(option("bar", type: :boolean, inverse: true).usage).to eq("[--bar], [--no-bar], [--skip-bar]")
end

describe "with required values" do
it "does not show the usage between brackets" do
expect(parse(:foo, :required).usage).to eq("--foo=FOO")
Expand Down
73 changes: 73 additions & 0 deletions spec/parser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,79 @@ def remaining
create verbose: Thor::Option.new("verbose", type: :boolean, aliases: "-v", repeatable: true)
expect(parse("-v", "-v", "-v")["verbose"].count).to eq(3)
end

describe "with :inverse" do
def create_inverse(inverse, name = "foo")
create name.to_sym => Thor::Option.new(name, type: :boolean, default: true, inverse: inverse)
end

it "accepts --[no-]opt and --[skip-]opt variants when true" do
create_inverse true
expect(parse("--no-foo")["foo"]).to eq(false)
create_inverse true
expect(parse("--skip-foo")["foo"]).to eq(false)
end

it "does not accept --[no-]opt and --[skip-]opt variants when false" do
create_inverse false
expect(parse("--no-foo")["foo"]).to eq(true)
expect(remaining).to eq(%w(--no-foo))
create_inverse false
expect(parse("--skip-foo")["foo"]).to eq(true)
expect(remaining).to eq(%w(--skip-foo))
end

it "raises an error for --[no-]opt variant when false and checking unknown options" do
create_inverse false
parse("--no-foo")
expect { check_unknown! }.to raise_error(Thor::UnknownArgumentError, /Unknown switches "--no-foo"/)
end

it "still accepts --opt assignment when false" do
create_inverse false
expect(parse("--foo")["foo"]).to eq(true)
create_inverse false
expect(parse("--foo=false")["foo"]).to eq(false)
end

it "accepts a custom inverse switch, setting false for value" do
create_inverse :monochrome, "color"
expect(parse("--monochrome")["color"]).to eq(false)
end

it "doesn't eat the next part of the param with a custom inverse switch" do
create_inverse :monochrome, "color"
expect(parse("--monochrome", "bar")).to eq("color" => false)
expect(remaining).to eq(%w(bar))
end

it "accepts a custom inverse switch in the human name format" do
create_inverse :black_and_white, "color"
expect(parse("--black-and-white")["color"]).to eq(false)
create_inverse :black_and_white, "color"
expect(parse("--black_and_white")["color"]).to eq(false)
end

it "does not accept --[no-]opt and --[skip-]opt variants with a custom inverse switch" do
create_inverse :monochrome, "color"
expect(parse("--no-color")["color"]).to eq(true)
create_inverse :monochrome, "color"
expect(parse("--skip-color")["color"]).to eq(true)
end

it "accepts a custom inverse switch that starts with no-" do
create_inverse :no_colors, "color"
expect(parse("--no-colors")["color"]).to eq(false)
create_inverse :no_colors, "color"
expect(parse("--no-color")["color"]).to eq(true)
end

it "is ignored by non-boolean options" do
create foo: Thor::Option.new("foo", type: :string, inverse: false)
expect(parse("--no-foo")["foo"]).to eq(nil)
expect(remaining).to eq([])
end
end
end

describe "with :hash type" do
Expand Down