From af278b15de03ea410b5d057518747446bbf8b8c4 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sun, 10 May 2020 15:10:36 +0100 Subject: [PATCH 1/2] cli/parser: add formulae method. This removes the coupling on args.formulae before it has been parsed. --- Library/Homebrew/cli/parser.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 4a7d2ca681..53efc00584 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -3,6 +3,7 @@ require "cli/args" require "optparse" require "set" +require "formula" COMMAND_DESC_WIDTH = 80 OPTION_DESC_WIDTH = 43 @@ -188,7 +189,7 @@ module Homebrew end def formula_options - @args.formulae.each do |f| + formulae.each do |f| next if f.options.empty? f.options.each do |o| @@ -343,6 +344,28 @@ module Homebrew option, = @parser.make_switch(args) @processed_options << [option.short.first, option.long.first, option.arg, option.desc.first] 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 class OptionConstraintError < RuntimeError From fa0d45481737b1c85b76fdc32ee816777f371d7c Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sun, 10 May 2020 15:12:25 +0100 Subject: [PATCH 2/2] Replace ARGV#flags_only with Homebrew.args.flags_only Take two on https://github.com/Homebrew/brew/pull/7490 --- Library/Homebrew/cli/args.rb | 25 ++++++++++++++++--------- Library/Homebrew/extend/ARGV.rb | 4 ---- Library/Homebrew/software_spec.rb | 2 +- Library/Homebrew/test/ARGV_spec.rb | 8 -------- Library/Homebrew/test/messages_spec.rb | 3 ++- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb index 801d2e7041..202b5557db 100644 --- a/Library/Homebrew/cli/args.rb +++ b/Library/Homebrew/cli/args.rb @@ -5,6 +5,8 @@ require "ostruct" module Homebrew module CLI class Args < OpenStruct + attr_reader :options_only, :flags_only + # undefine tap to allow --tap argument undef tap @@ -12,6 +14,8 @@ module Homebrew super() @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! # (whereas other values below will only be overwritten if passed). @@ -46,16 +50,9 @@ module Homebrew @processed_options += processed_options @processed_options.freeze - end - def options_only - @options_only ||= cli_args.select { |arg| arg.start_with?("-") } - .freeze - end - - def flags_only - @flags_only ||= cli_args.select { |arg| arg.start_with?("--") } - .freeze + @options_only = args_options_only(cli_args) + @flags_only = args_flags_only(cli_args) end def passthrough @@ -204,6 +201,16 @@ module Homebrew @cli_args.freeze 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 # Only lowercase names, not paths, bottle filenames or URLs named.map do |arg| diff --git a/Library/Homebrew/extend/ARGV.rb b/Library/Homebrew/extend/ARGV.rb index 27f21a228f..c75c81b67c 100644 --- a/Library/Homebrew/extend/ARGV.rb +++ b/Library/Homebrew/extend/ARGV.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true module HomebrewArgvExtension - def flags_only - select { |arg| arg.start_with?("--") } - end - def value(name) arg_prefix = "--#{name}=" flag_with_value = find { |arg| arg.start_with?(arg_prefix) } diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 676e7e524f..4202711a8a 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -41,7 +41,7 @@ class SoftwareSpec @bottle_specification = BottleSpecification.new @patches = [] @options = Options.new - @flags = ARGV.flags_only + @flags = Homebrew.args.flags_only @deprecated_flags = [] @deprecated_options = [] @build = BuildOptions.new(Options.create(@flags), options) diff --git a/Library/Homebrew/test/ARGV_spec.rb b/Library/Homebrew/test/ARGV_spec.rb index 5b32127930..dd1699af31 100644 --- a/Library/Homebrew/test/ARGV_spec.rb +++ b/Library/Homebrew/test/ARGV_spec.rb @@ -31,14 +31,6 @@ describe HomebrewArgvExtension do 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 let(:argv) { [] } diff --git a/Library/Homebrew/test/messages_spec.rb b/Library/Homebrew/test/messages_spec.rb index 8b743b7bdf..9bf48c58aa 100644 --- a/Library/Homebrew/test/messages_spec.rb +++ b/Library/Homebrew/test/messages_spec.rb @@ -76,7 +76,8 @@ describe Messages do # rubocop:disable RSpec/VerifiedDoubles context "when the --display-times argument is present" 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 context "when install_times is empty" do