Skip to content
Snippets Groups Projects
Unverified Commit ae08b15c authored by Gautham Goli's avatar Gautham Goli Committed by GitHub
Browse files

Merge pull request #6479 from GauthamGoli/argv-cleanup-5

cli_args: Fix options_only and flags_only
parents 0e1a237c e88f6b9d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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"
......
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment