Merge pull request #8352 from reitermarkus/negative-flags

Fix parsing of negative options.
This commit is contained in:
Markus Reiter 2020-08-14 20:37:47 +02:00 committed by GitHub
commit 99109738bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -73,6 +73,12 @@ module Homebrew
description = option_to_description(*names) if description.nil?
process_option(*names, description)
@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)
end

View File

@ -15,7 +15,7 @@ describe Homebrew::CLI::Parser do
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
end
context "when using negative options" do
context "when using binary options" do
subject(:parser) {
described_class.new do
switch "--[no-]positive"
@ -33,6 +33,30 @@ describe Homebrew::CLI::Parser do
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
it "passes through invalid options" do
args = parser.parse(["-v", "named-arg", "--not-a-valid-option"], ignore_invalid_options: true)