diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb index bcb3563cd4..cc2f277e02 100644 --- a/Library/Homebrew/cli/args.rb +++ b/Library/Homebrew/cli/args.rb @@ -5,12 +5,50 @@ require "ostruct" module Homebrew module CLI class Args < OpenStruct + attr_accessor :processed_options # undefine tap to allow --tap argument undef tap def initialize(argv:) super @argv = argv + @processed_options = [] + end + + def option_to_name(option) + option.sub(/\A--?/, "") + .tr("-", "_") + end + + def cli_args + return @cli_args if @cli_args + + @cli_args = [] + processed_options.each do |short, long| + option = long || short + switch = "#{option_to_name(option)}?".to_sym + flag = option_to_name(option).to_sym + if @table[switch] == true || @table[flag] == true + @cli_args << option + elsif @table[flag].instance_of? String + @cli_args << option + "=" + @table[flag] + elsif @table[flag].instance_of? Array + @cli_args << option + "=" + @table[flag].join(",") + end + end + @cli_args + end + + def options_only + @options_only ||= cli_args.select { |arg| arg.start_with?("-") } + end + + def flags_only + @flags_only ||= cli_args.select { |arg| arg.start_with?("--") } + end + + def passthrough + options_only - CLI::Parser.global_options.values.map(&:first).flatten end end end diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 5d69ae0cf4..1e76709170 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -139,6 +139,7 @@ module Homebrew check_constraint_violations @args[:remaining] = remaining_args @args_parsed = true + @args.processed_options = @processed_options Homebrew.args = @args cmdline_args.freeze @parser diff --git a/Library/Homebrew/cmd/cat.rb b/Library/Homebrew/cmd/cat.rb index a8c360dcc0..8075034046 100644 --- a/Library/Homebrew/cmd/cat.rb +++ b/Library/Homebrew/cmd/cat.rb @@ -25,6 +25,6 @@ module Homebrew raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1 cd HOMEBREW_REPOSITORY - safe_system "cat", formulae.first.path, *ARGV.options_only + safe_system "cat", formulae.first.path, *Homebrew.args.passthrough end end diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb index cde69c3ad5..a6cd43d331 100644 --- a/Library/Homebrew/cmd/list.rb +++ b/Library/Homebrew/cmd/list.rb @@ -70,7 +70,7 @@ module Homebrew puts Formatter.columns(full_names) else ENV["CLICOLOR"] = nil - safe_system "ls", *ARGV.options_only << HOMEBREW_CELLAR + safe_system "ls", *Homebrew.args.passthrough << HOMEBREW_CELLAR end elsif args.verbose? || !$stdout.tty? system_command! "find", args: ARGV.kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true diff --git a/Library/Homebrew/cmd/log.rb b/Library/Homebrew/cmd/log.rb index 58c70ec8b0..84dab11a10 100644 --- a/Library/Homebrew/cmd/log.rb +++ b/Library/Homebrew/cmd/log.rb @@ -57,7 +57,7 @@ module Homebrew git -C "#{git_cd}" fetch --unshallow EOS end - args = ARGV.options_only + args = Homebrew.args.options_only args += ["--follow", "--", path] unless path.nil? system "git", "log", *args end diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index c19cce8f0b..e1488c2fd7 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -162,7 +162,7 @@ module Homebrew tab = Tab.for_keg(keg) end - build_options = BuildOptions.new(Options.create(ARGV.flags_only), f.options) + build_options = BuildOptions.new(Options.create(Homebrew.args.flags_only), f.options) options = build_options.used_options options |= f.build.used_options options &= f.options diff --git a/Library/Homebrew/dev-cmd/test.rb b/Library/Homebrew/dev-cmd/test.rb index 1d5ad41103..17eca7dab6 100644 --- a/Library/Homebrew/dev-cmd/test.rb +++ b/Library/Homebrew/dev-cmd/test.rb @@ -82,7 +82,7 @@ module Homebrew -- #{HOMEBREW_LIBRARY_PATH}/test.rb #{f.path} - ].concat(ARGV.options_only) + ].concat(Homebrew.args.options_only) if f.head? args << "--HEAD" diff --git a/Library/Homebrew/reinstall.rb b/Library/Homebrew/reinstall.rb index 51862302a1..067d8b38e8 100644 --- a/Library/Homebrew/reinstall.rb +++ b/Library/Homebrew/reinstall.rb @@ -16,7 +16,7 @@ module Homebrew backup keg end - build_options = BuildOptions.new(Options.create(ARGV.flags_only), f.options) + build_options = BuildOptions.new(Options.create(Homebrew.args.flags_only), f.options) options = build_options.used_options options |= f.build.used_options options &= f.options diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index a0cfa8c5cf..5d5171f57b 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -210,4 +210,30 @@ describe Homebrew::CLI::Parser do expect { parser.parse(["--switch-b"]) }.to raise_error(RuntimeError, /Arguments were already parsed!/) end end + + describe "test argv extensions" do + subject(:parser) { + described_class.new do + switch "--foo" + flag "--bar" + switch "-s" + switch :verbose + end + } + + it "#options_only" do + parser.parse(["--foo", "--bar=value", "-v", "-s", "a", "b", "cdefg"]) + expect(Homebrew.args.options_only).to eq %w[--foo --bar=value -s --verbose] + end + + it "#flags_only" do + parser.parse(["--foo", "--bar=value", "-v", "-s", "a", "b", "cdefg"]) + expect(Homebrew.args.flags_only).to eq %w[--foo --bar=value --verbose] + end + + it "#passthrough" do + parser.parse(["--foo", "--bar=value", "-v", "-s", "a", "b", "cdefg"]) + expect(Homebrew.args.passthrough).to eq %w[--foo --bar=value -s] + end + end end