Parser: Add env named argument for switch to check environment variables
This commit is contained in:
parent
164f47a108
commit
99438e8e44
@ -10,13 +10,12 @@ module Homebrew
|
|||||||
instance_eval(&block)
|
instance_eval(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def switch(*names, description: nil)
|
def switch(*names, description: nil, env: nil)
|
||||||
description = option_to_description(*names) if description.nil?
|
description = option_to_description(*names) if description.nil?
|
||||||
@parser.on(*names, description) do
|
@parser.on(*names, description) do
|
||||||
names.each do |name|
|
enable_switch(*names)
|
||||||
@parsed_args["#{option_to_name(name)}?"] = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def comma_array(name, description: nil)
|
def comma_array(name, description: nil)
|
||||||
@ -50,6 +49,14 @@ module Homebrew
|
|||||||
@parser.parse!(cmdline_args)
|
@parser.parse!(cmdline_args)
|
||||||
@parsed_args
|
@parsed_args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def enable_switch(*names)
|
||||||
|
names.each do |name|
|
||||||
|
@parsed_args["#{option_to_name(name)}?"] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
#: used instead of irb.
|
#: used instead of irb.
|
||||||
|
|
||||||
require "cli_parser"
|
require "cli_parser"
|
||||||
require "utils/env"
|
|
||||||
|
|
||||||
class Symbol
|
class Symbol
|
||||||
def f(*args)
|
def f(*args)
|
||||||
@ -26,7 +25,7 @@ module Homebrew
|
|||||||
def irb
|
def irb
|
||||||
args = Homebrew::CLI::Parser.new do
|
args = Homebrew::CLI::Parser.new do
|
||||||
switch "--examples"
|
switch "--examples"
|
||||||
switch "--pry"
|
switch "--pry", env: :pry
|
||||||
end.parse
|
end.parse
|
||||||
|
|
||||||
if args.examples?
|
if args.examples?
|
||||||
@ -37,7 +36,7 @@ module Homebrew
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if args.pry? || Utils::EnvVars.pry?
|
if args.pry?
|
||||||
Homebrew.install_gem_setup_path! "pry"
|
Homebrew.install_gem_setup_path! "pry"
|
||||||
require "pry"
|
require "pry"
|
||||||
Pry.config.prompt_name = "brew"
|
Pry.config.prompt_name = "brew"
|
||||||
@ -53,7 +52,7 @@ module Homebrew
|
|||||||
|
|
||||||
ohai "Interactive Homebrew Shell"
|
ohai "Interactive Homebrew Shell"
|
||||||
puts "Example commands available with: brew irb --examples"
|
puts "Example commands available with: brew irb --examples"
|
||||||
if args.pry? || Utils::EnvVars.pry?
|
if args.pry?
|
||||||
Pry.start
|
Pry.start
|
||||||
else
|
else
|
||||||
IRB.start
|
IRB.start
|
||||||
|
@ -6,6 +6,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
described_class.new do
|
described_class.new do
|
||||||
switch "-v", "--verbose", description: "Flag for verbosity"
|
switch "-v", "--verbose", description: "Flag for verbosity"
|
||||||
switch "--more-verbose", description: "Flag for higher verbosity"
|
switch "--more-verbose", description: "Flag for higher verbosity"
|
||||||
|
switch "--pry", env: :pry
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +35,12 @@ describe Homebrew::CLI::Parser do
|
|||||||
it "raises an exception when an invalid option is passed" do
|
it "raises an exception when an invalid option is passed" do
|
||||||
expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/)
|
expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "maps environment var to an option" do
|
||||||
|
allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1")
|
||||||
|
args = parser.parse([])
|
||||||
|
expect(args.pry?).to be true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "test long flag options" do
|
describe "test long flag options" do
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
module Utils
|
|
||||||
module EnvVars
|
|
||||||
class << self
|
|
||||||
def pry?
|
|
||||||
!ENV["HOMEBREW_PRY"].nil?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
x
Reference in New Issue
Block a user