diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 7c9a0cced3284d0dccd88a0fa856d482d9cc3b5f..1f6ab8a08d2b1e7f0857692c385e250d0b79b41a 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -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 diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index 22eed65a54daef9a68a20d5a57fb851999328cc8..97ac930d4daeefa503f06a01e188fe9d6c408b5a 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -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)