Merge pull request #3971 from GauthamGoli/irb-args

irb: Use Parser to parse args
This commit is contained in:
Mike McQuaid 2018-03-25 10:01:47 +01:00 committed by GitHub
commit ee39940196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 11 deletions

View File

@ -10,13 +10,12 @@ module Homebrew
instance_eval(&block)
end
def switch(*names, description: nil)
def switch(*names, description: nil, env: nil)
description = option_to_description(*names) if description.nil?
@parser.on(*names, description) do
names.each do |name|
@parsed_args["#{option_to_name(name)}?"] = true
end
enable_switch(*names)
end
enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
end
def comma_array(name, description: nil)
@ -50,6 +49,14 @@ module Homebrew
@parser.parse!(cmdline_args)
@parsed_args
end
private
def enable_switch(*names)
names.each do |name|
@parsed_args["#{option_to_name(name)}?"] = true
end
end
end
end
end

View File

@ -5,6 +5,8 @@
#: If `--pry` is passed or HOMEBREW_PRY is set, pry will be
#: used instead of irb.
require "cli_parser"
class Symbol
def f(*args)
Formulary.factory(to_s, *args)
@ -21,7 +23,12 @@ module Homebrew
module_function
def irb
if ARGV.include? "--examples"
args = Homebrew::CLI::Parser.new do
switch "--examples"
switch "--pry", env: :pry
end.parse
if args.examples?
puts "'v8'.f # => instance of the v8 formula"
puts ":hub.f.installed?"
puts ":lua.f.methods - 1.methods"
@ -29,7 +36,7 @@ module Homebrew
return
end
if ARGV.pry?
if args.pry?
Homebrew.install_gem_setup_path! "pry"
require "pry"
Pry.config.prompt_name = "brew"
@ -45,7 +52,7 @@ module Homebrew
ohai "Interactive Homebrew Shell"
puts "Example commands available with: brew irb --examples"
if ARGV.pry?
if args.pry?
Pry.start
else
IRB.start

View File

@ -268,10 +268,6 @@ module HomebrewArgvExtension
include? "--fetch-HEAD"
end
def pry?
include?("--pry") || !ENV["HOMEBREW_PRY"].nil?
end
# eg. `foo -ns -i --bar` has three switches, n, s and i
def switch?(char)
return false if char.length > 1

View File

@ -6,6 +6,7 @@ describe Homebrew::CLI::Parser do
described_class.new do
switch "-v", "--verbose", description: "Flag for verbosity"
switch "--more-verbose", description: "Flag for higher verbosity"
switch "--pry", env: :pry
end
}
@ -34,6 +35,12 @@ describe Homebrew::CLI::Parser do
it "raises an exception when an invalid option is passed" do
expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/)
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
describe "test long flag options" do