args: Add passthrough method and tests

This commit is contained in:
Gautham Goli 2019-09-25 14:21:06 +05:30
parent 393c8dfbf1
commit e88f6b9da9
4 changed files with 13 additions and 8 deletions

View File

@ -21,16 +21,14 @@ module Homebrew
end end
def cli_args def cli_args
return @cli_args unless @cli_args.nil? return @cli_args if @cli_args
@cli_args = [] @cli_args = []
processed_options.each do |short, long| processed_options.each do |short, long|
option = long || short option = long || short
switch = "#{option_to_name(option)}?".to_sym switch = "#{option_to_name(option)}?".to_sym
flag = option_to_name(option).to_sym flag = option_to_name(option).to_sym
if @table[switch].instance_of? TrueClass if @table[switch] == true || @table[flag] == true
@cli_args << option
elsif @table[flag].instance_of? TrueClass
@cli_args << option @cli_args << option
elsif @table[flag].instance_of? String elsif @table[flag].instance_of? String
@cli_args << option + "=" + @table[flag] @cli_args << option + "=" + @table[flag]
@ -48,6 +46,10 @@ module Homebrew
def flags_only def flags_only
@flags_only ||= cli_args.select { |arg| arg.start_with?("--") } @flags_only ||= cli_args.select { |arg| arg.start_with?("--") }
end end
def passthrough
options_only - CLI::Parser.global_options.values.map(&:first).flatten
end
end end
end end
end end

View File

@ -25,7 +25,6 @@ module Homebrew
raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1 raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1
cd HOMEBREW_REPOSITORY cd HOMEBREW_REPOSITORY
cat_args = Homebrew.args.options_only - CLI::Parser.global_options.values.map(&:first).flatten safe_system "cat", formulae.first.path, *Homebrew.args.passthrough
safe_system "cat", formulae.first.path, *cat_args
end end
end end

View File

@ -70,8 +70,7 @@ module Homebrew
puts Formatter.columns(full_names) puts Formatter.columns(full_names)
else else
ENV["CLICOLOR"] = nil ENV["CLICOLOR"] = nil
ls_args = Homebrew.args.options_only - CLI::Parser.global_options.values.map(&:first).flatten safe_system "ls", *Homebrew.args.passthrough << HOMEBREW_CELLAR
safe_system "ls", *ls_args << HOMEBREW_CELLAR
end end
elsif args.verbose? || !$stdout.tty? elsif args.verbose? || !$stdout.tty?
system_command! "find", args: ARGV.kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true system_command! "find", args: ARGV.kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true

View File

@ -230,5 +230,10 @@ describe Homebrew::CLI::Parser do
parser.parse(["--foo", "--bar=value", "-v", "-s", "a", "b", "cdefg"]) parser.parse(["--foo", "--bar=value", "-v", "-s", "a", "b", "cdefg"])
expect(Homebrew.args.flags_only).to eq %w[--foo --bar=value --verbose] expect(Homebrew.args.flags_only).to eq %w[--foo --bar=value --verbose]
end 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
end end