Merge pull request #20532 from Homebrew/cli-parser-switch-env

cli/parser: convert switch env values to boolean
This commit is contained in:
Ruoyu Zhong 2025-08-21 18:22:20 +00:00 committed by GitHub
commit aeb42b2614
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -231,7 +231,8 @@ module Homebrew
end end
env_value = value_for_env(env) env_value = value_for_env(env)
set_switch(*names, value: env_value, from: :env) unless env_value.nil? value = env_value&.present?
set_switch(*names, value:, from: :env) unless value.nil?
end end
alias switch_option switch alias switch_option switch

View File

@ -10,12 +10,17 @@ RSpec.describe Homebrew::CLI::Parser do
described_class.new(Cmd) do described_class.new(Cmd) do
switch "--more-verbose", description: "Flag for higher verbosity" switch "--more-verbose", description: "Flag for higher verbosity"
switch "--pry", env: :pry switch "--pry", env: :pry
switch "--foo", env: :foo
switch "--bar", env: :bar
switch "--hidden", hidden: true switch "--hidden", hidden: true
end end
end end
before do before do
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true) allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with("HOMEBREW_FOO", nil).and_return("")
allow(ENV).to receive(:fetch).with("HOMEBREW_BAR", nil).and_return("1")
end end
context "when using binary options" do context "when using binary options" do
@ -113,6 +118,8 @@ RSpec.describe Homebrew::CLI::Parser do
it "maps environment var to an option" do it "maps environment var to an option" do
args = parser.parse([]) args = parser.parse([])
expect(args.pry?).to be true expect(args.pry?).to be true
expect(args.foo?).to be false
expect(args.bar?).to be true
end end
end end