Merge pull request #7537 from MikeMcQuaid/cli_parser_formulae

Replace ARGV#flags_only with Homebrew.args.flags_only
This commit is contained in:
Mike McQuaid 2020-05-11 08:47:12 +01:00 committed by GitHub
commit 35e487064e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 24 deletions

View File

@ -5,6 +5,8 @@ require "ostruct"
module Homebrew module Homebrew
module CLI module CLI
class Args < OpenStruct class Args < OpenStruct
attr_reader :options_only, :flags_only
# undefine tap to allow --tap argument # undefine tap to allow --tap argument
undef tap undef tap
@ -12,6 +14,8 @@ module Homebrew
super() super()
@processed_options = [] @processed_options = []
@options_only = args_options_only(argv)
@flags_only = args_flags_only(argv)
# Can set these because they will be overwritten by freeze_named_args! # Can set these because they will be overwritten by freeze_named_args!
# (whereas other values below will only be overwritten if passed). # (whereas other values below will only be overwritten if passed).
@ -46,16 +50,9 @@ module Homebrew
@processed_options += processed_options @processed_options += processed_options
@processed_options.freeze @processed_options.freeze
end
def options_only @options_only = args_options_only(cli_args)
@options_only ||= cli_args.select { |arg| arg.start_with?("-") } @flags_only = args_flags_only(cli_args)
.freeze
end
def flags_only
@flags_only ||= cli_args.select { |arg| arg.start_with?("--") }
.freeze
end end
def passthrough def passthrough
@ -204,6 +201,16 @@ module Homebrew
@cli_args.freeze @cli_args.freeze
end end
def args_options_only(args)
args.select { |arg| arg.start_with?("-") }
.freeze
end
def args_flags_only(args)
args.select { |arg| arg.start_with?("--") }
.freeze
end
def downcased_unique_named def downcased_unique_named
# Only lowercase names, not paths, bottle filenames or URLs # Only lowercase names, not paths, bottle filenames or URLs
named.map do |arg| named.map do |arg|

View File

@ -3,6 +3,7 @@
require "cli/args" require "cli/args"
require "optparse" require "optparse"
require "set" require "set"
require "formula"
COMMAND_DESC_WIDTH = 80 COMMAND_DESC_WIDTH = 80
OPTION_DESC_WIDTH = 43 OPTION_DESC_WIDTH = 43
@ -188,7 +189,7 @@ module Homebrew
end end
def formula_options def formula_options
@args.formulae.each do |f| formulae.each do |f|
next if f.options.empty? next if f.options.empty?
f.options.each do |o| f.options.each do |o|
@ -343,6 +344,28 @@ module Homebrew
option, = @parser.make_switch(args) option, = @parser.make_switch(args)
@processed_options << [option.short.first, option.long.first, option.arg, option.desc.first] @processed_options << [option.short.first, option.long.first, option.arg, option.desc.first]
end end
def formulae
named_args = @argv.reject { |arg| arg.start_with?("-") }
spec = if @argv.include?("--HEAD")
:head
elsif @argv.include?("--devel")
:devel
else
:stable
end
# Only lowercase names, not paths, bottle filenames or URLs
named_args.map do |arg|
next if arg.match?(HOMEBREW_CASK_TAP_CASK_REGEX)
if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
Formulary.factory(arg, spec)
else
Formulary.find_with_priority(arg.downcase, spec)
end
end.compact.uniq(&:name)
end
end end
class OptionConstraintError < RuntimeError class OptionConstraintError < RuntimeError

View File

@ -1,10 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module HomebrewArgvExtension module HomebrewArgvExtension
def flags_only
select { |arg| arg.start_with?("--") }
end
def value(name) def value(name)
arg_prefix = "--#{name}=" arg_prefix = "--#{name}="
flag_with_value = find { |arg| arg.start_with?(arg_prefix) } flag_with_value = find { |arg| arg.start_with?(arg_prefix) }

View File

@ -41,7 +41,7 @@ class SoftwareSpec
@bottle_specification = BottleSpecification.new @bottle_specification = BottleSpecification.new
@patches = [] @patches = []
@options = Options.new @options = Options.new
@flags = ARGV.flags_only @flags = Homebrew.args.flags_only
@deprecated_flags = [] @deprecated_flags = []
@deprecated_options = [] @deprecated_options = []
@build = BuildOptions.new(Options.create(@flags), options) @build = BuildOptions.new(Options.create(@flags), options)

View File

@ -31,14 +31,6 @@ describe HomebrewArgvExtension do
end end
end end
describe "#flags_only" do
let(:argv) { ["--foo", "-vds", "a", "b", "cdefg"] }
it "returns an array of flags" do
expect(subject.flags_only).to eq ["--foo"]
end
end
describe "#empty?" do describe "#empty?" do
let(:argv) { [] } let(:argv) { [] }

View File

@ -76,7 +76,8 @@ describe Messages do
# rubocop:disable RSpec/VerifiedDoubles # rubocop:disable RSpec/VerifiedDoubles
context "when the --display-times argument is present" do context "when the --display-times argument is present" do
before do before do
allow(Homebrew).to receive(:args).and_return(double(display_times?: true)) allow(Homebrew).to receive(:args).and_return \
double(display_times?: true, flags_only: ["--display-times"])
end end
context "when install_times is empty" do context "when install_times is empty" do