Merge pull request #4012 from GauthamGoli/global-options

cli_parser: Common switch options to be accessible across brew
This commit is contained in:
Mike McQuaid 2018-04-09 13:50:08 -07:00 committed by GitHub
commit 915ab415e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 21 deletions

View File

@ -18,11 +18,13 @@ module Homebrew
def switch(*names, description: nil, env: nil) def switch(*names, description: nil, env: nil)
description = option_to_description(*names) if description.nil? description = option_to_description(*names) if description.nil?
names, env = common_switch(*names) if names.first.is_a?(Symbol) global_switch = names.first.is_a?(Symbol)
names, env = common_switch(*names) if global_switch
@parser.on(*names, description) do @parser.on(*names, description) do
enable_switch(*names) enable_switch(*names, global_switch)
end end
enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil? enable_switch(*names, global_switch) if !env.nil? &&
!ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
end end
def comma_array(name, description: nil) def comma_array(name, description: nil)
@ -53,23 +55,29 @@ module Homebrew
end end
def parse(cmdline_args = ARGV) def parse(cmdline_args = ARGV)
@parser.parse!(cmdline_args) @parser.parse(cmdline_args)
@parsed_args @parsed_args
end end
private private
def enable_switch(*names) def enable_switch(*names, global_switch)
names.each do |name| names.each do |name|
if global_switch
Homebrew.args["#{option_to_name(name)}?"] = true
next
end
@parsed_args["#{option_to_name(name)}?"] = true @parsed_args["#{option_to_name(name)}?"] = true
end end
end end
# These are common/global switches accessible throughout Homebrew
def common_switch(name) def common_switch(name)
case name case name
when :quiet then [["-q", "--quiet"], :quiet] when :quiet then [["-q", "--quiet"], :quiet]
when :verbose then [["-v", "--verbose"], :verbose] when :verbose then [["-v", "--verbose"], :verbose]
when :debug then [["-d", "--debug"], :debug] when :debug then [["-d", "--debug"], :debug]
when :force then [["-f", "--force"], :force]
else name else name
end end
end end

View File

@ -79,6 +79,7 @@ module Homebrew
switch "--no-commit" switch "--no-commit"
switch "--json" switch "--json"
switch :verbose switch :verbose
switch :debug
flag "--root-url" flag "--root-url"
end end
@ -113,7 +114,7 @@ module Homebrew
linked_libraries = Keg.file_linked_libraries(file, string) linked_libraries = Keg.file_linked_libraries(file, string)
result ||= !linked_libraries.empty? result ||= !linked_libraries.empty?
if @args.verbose? if Homebrew.args.verbose?
print_filename.call(string, file) unless linked_libraries.empty? print_filename.call(string, file) unless linked_libraries.empty?
linked_libraries.each do |lib| linked_libraries.each do |lib|
puts " #{Tty.bold}-->#{Tty.reset} links to #{lib}" puts " #{Tty.bold}-->#{Tty.reset} links to #{lib}"
@ -136,7 +137,7 @@ module Homebrew
end end
end end
next unless @args.verbose? && !text_matches.empty? next unless Homebrew.args.verbose? && !text_matches.empty?
print_filename.call(string, file) print_filename.call(string, file)
text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset| text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset|
puts " #{Tty.bold}-->#{Tty.reset} match '#{match}' at offset #{Tty.bold}0x#{offset}#{Tty.reset}" puts " #{Tty.bold}-->#{Tty.reset} match '#{match}' at offset #{Tty.bold}0x#{offset}#{Tty.reset}"
@ -157,7 +158,7 @@ module Homebrew
absolute_symlinks_start_with_string << pn if link.to_s.start_with?(string) absolute_symlinks_start_with_string << pn if link.to_s.start_with?(string)
end end
if @args.verbose? if Homebrew.args.verbose?
unless absolute_symlinks_start_with_string.empty? unless absolute_symlinks_start_with_string.empty?
opoo "Absolute symlink starting with #{string}:" opoo "Absolute symlink starting with #{string}:"
absolute_symlinks_start_with_string.each do |pn| absolute_symlinks_start_with_string.each do |pn|
@ -298,7 +299,7 @@ module Homebrew
end end
skip_relocation = relocatable && !keg.require_relocation? skip_relocation = relocatable && !keg.require_relocation?
end end
puts if !relocatable && @args.verbose? puts if !relocatable && Homebrew.args.verbose?
rescue Interrupt rescue Interrupt
ignore_interrupts { bottle_path.unlink if bottle_path.exist? } ignore_interrupts { bottle_path.unlink if bottle_path.exist? }
raise raise

View File

@ -11,8 +11,10 @@ module Homebrew
module_function module_function
def edit def edit
args = Homebrew::CLI::Parser.parse do Homebrew::CLI::Parser.parse do
switch "--force" switch :force
switch :verbose
switch :debug
end end
unless (HOMEBREW_REPOSITORY/".git").directory? unless (HOMEBREW_REPOSITORY/".git").directory?
@ -41,7 +43,7 @@ module Homebrew
paths = ARGV.named.map do |name| paths = ARGV.named.map do |name|
path = Formulary.path(name) path = Formulary.path(name)
raise FormulaUnavailableError, name unless path.file? || args.force? raise FormulaUnavailableError, name unless path.file? || Homebrew.args.force?
path path
end end

View File

@ -2,11 +2,17 @@
#: Display the path where <formula> is located. #: Display the path where <formula> is located.
require "formula" require "formula"
require "cli_parser"
module Homebrew module Homebrew
module_function module_function
def formula def formula
Homebrew::CLI::Parser.parse do
switch :debug
switch :verbose
end
raise FormulaUnspecifiedError if ARGV.named.empty? raise FormulaUnspecifiedError if ARGV.named.empty?
ARGV.resolved_formulae.each { |f| puts f.path } ARGV.resolved_formulae.each { |f| puts f.path }
end end

View File

@ -6,6 +6,11 @@ module Homebrew
module_function module_function
def mirror def mirror
Homebrew::CLI::Parser.parse do
switch :debug
switch :verbose
end
odie "This command requires at least formula argument!" if ARGV.named.empty? odie "This command requires at least formula argument!" if ARGV.named.empty?
bintray_user = ENV["HOMEBREW_BINTRAY_USER"] bintray_user = ENV["HOMEBREW_BINTRAY_USER"]

View File

@ -84,6 +84,8 @@ module Homebrew
switch "--no-pbcopy" switch "--no-pbcopy"
switch "--no-publish" switch "--no-publish"
switch "--warn-on-publish-failure" switch "--warn-on-publish-failure"
switch :verbose
switch :debug
flag "--bintray-org", required: true flag "--bintray-org", required: true
flag "--test-bot-user", required: true flag "--test-bot-user", required: true
end end

View File

@ -1,6 +1,9 @@
#: * `tap-new` <user>`/`<repo>: #: * `tap-new` <user>`/`<repo>:
#: Generate the template files for a new tap. #: Generate the template files for a new tap.
require "tap"
require "cli_parser"
module Homebrew module Homebrew
module_function module_function
@ -12,6 +15,11 @@ module Homebrew
end end
def tap_new def tap_new
Homebrew::CLI::Parser.parse do
switch :debug
switch :verbose
end
raise "A tap argument is required" if ARGV.named.empty? raise "A tap argument is required" if ARGV.named.empty?
tap = Tap.fetch(ARGV.named.first) tap = Tap.fetch(ARGV.named.first)

View File

@ -25,9 +25,10 @@ module Homebrew
args = Homebrew::CLI::Parser.parse do args = Homebrew::CLI::Parser.parse do
switch "--no-compat" switch "--no-compat"
switch "--generic" switch "--generic"
switch "-v", "--verbose"
switch "--coverage" switch "--coverage"
switch "--online" switch "--online"
switch :debug
switch :verbose
flag "--only", required: true flag "--only", required: true
flag "--seed", required: true flag "--seed", required: true
end end

View File

@ -23,6 +23,8 @@ module Homebrew
args = Homebrew::CLI::Parser.parse do args = Homebrew::CLI::Parser.parse do
switch "--to-tag" switch "--to-tag"
switch "--keep-tmp" switch "--keep-tmp"
switch :verbose
switch :debug
flag "--commit", required: true flag "--commit", required: true
flag "--before", required: true flag "--before", required: true
end end

View File

@ -1,5 +1,6 @@
require "pathname" require "pathname"
require "English" require "English"
require "ostruct"
require "pp" require "pp"
require "extend/ARGV" require "extend/ARGV"
@ -30,14 +31,16 @@ module Homebrew
extend FileUtils extend FileUtils
class << self class << self
attr_writer :failed attr_writer :failed, :raise_deprecation_exceptions, :auditing, :args
def failed? def failed?
@failed ||= false @failed ||= false
@failed == true @failed == true
end end
attr_writer :raise_deprecation_exceptions, :auditing def args
@args ||= OpenStruct.new
end
def raise_deprecation_exceptions? def raise_deprecation_exceptions?
@raise_deprecation_exceptions == true @raise_deprecation_exceptions == true

View File

@ -16,24 +16,25 @@ describe Homebrew::CLI::Parser do
} }
it "parses short option" do it "parses short option" do
args = parser.parse(["-v"]) parser.parse(["-v"])
expect(args).to be_verbose expect(Homebrew.args).to be_verbose
end end
it "parses a single valid option" do it "parses a single valid option" do
args = parser.parse(["--verbose"]) parser.parse(["--verbose"])
expect(args).to be_verbose expect(Homebrew.args).to be_verbose
end end
it "parses a valid option along with few unnamed args" do it "parses a valid option along with few unnamed args" do
args = %w[--verbose unnamed args] args = %w[--verbose unnamed args]
parser.parse(args) parser.parse(args)
expect(args).to eq %w[unnamed args] expect(Homebrew.args).to be_verbose
expect(args).to eq %w[--verbose unnamed args]
end end
it "parses a single option and checks other options to be nil" do it "parses a single option and checks other options to be nil" do
args = parser.parse(["--verbose"]) args = parser.parse(["--verbose"])
expect(args).to be_verbose expect(Homebrew.args).to be_verbose
expect(args.more_verbose?).to be nil expect(args.more_verbose?).to be nil
end end