Merge pull request #8352 from reitermarkus/negative-flags
Fix parsing of negative options.
This commit is contained in:
commit
99109738bf
@ -73,6 +73,12 @@ module Homebrew
|
|||||||
description = option_to_description(*names) if description.nil?
|
description = option_to_description(*names) if description.nil?
|
||||||
process_option(*names, description)
|
process_option(*names, description)
|
||||||
@parser.public_send(method, *names, *wrap_option_desc(description)) do |value|
|
@parser.public_send(method, *names, *wrap_option_desc(description)) do |value|
|
||||||
|
value = if names.any? { |name| name.start_with?("--[no-]") }
|
||||||
|
value
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
set_switch(*names, value: value, from: :args)
|
set_switch(*names, value: value, from: :args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
|
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when using negative options" do
|
context "when using binary options" do
|
||||||
subject(:parser) {
|
subject(:parser) {
|
||||||
described_class.new do
|
described_class.new do
|
||||||
switch "--[no-]positive"
|
switch "--[no-]positive"
|
||||||
@ -33,6 +33,30 @@ describe Homebrew::CLI::Parser do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when using negative options" do
|
||||||
|
subject(:parser) {
|
||||||
|
described_class.new do
|
||||||
|
switch "--no-positive"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
it "does not set the positive name" do
|
||||||
|
args = parser.parse(["--no-positive"])
|
||||||
|
expect(args.positive?).to be nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails when using the positive name" do
|
||||||
|
expect {
|
||||||
|
parser.parse(["--positive"])
|
||||||
|
}.to raise_error(/invalid option/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets the negative name to true if the negative flag is passed" do
|
||||||
|
args = parser.parse(["--no-positive"])
|
||||||
|
expect(args.no_positive?).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when `ignore_invalid_options` is true" do
|
context "when `ignore_invalid_options` is true" do
|
||||||
it "passes through invalid options" do
|
it "passes through invalid options" do
|
||||||
args = parser.parse(["-v", "named-arg", "--not-a-valid-option"], ignore_invalid_options: true)
|
args = parser.parse(["-v", "named-arg", "--not-a-valid-option"], ignore_invalid_options: true)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user