Add :quiet, :verbose, :debug common switches to be specified

This commit is contained in:
Gautham Goli 2018-03-29 03:20:14 +05:30
parent 0f4e22e28d
commit 8ef1fb30a0
4 changed files with 21 additions and 5 deletions

View File

@ -16,6 +16,7 @@ module Homebrew
def switch(*names, description: nil, env: nil)
description = option_to_description(*names) if description.nil?
names, env = common_switch(*names) if names.first.is_a?(Symbol)
@parser.on(*names, description) do
enable_switch(*names)
end
@ -46,7 +47,7 @@ module Homebrew
end
def option_to_description(*names)
names.map { |name| name.sub(/\A--?/, "").tr("-", " ") }.sort.last
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.sort.last
end
def parse(cmdline_args = ARGV)
@ -61,6 +62,15 @@ module Homebrew
@parsed_args["#{option_to_name(name)}?"] = true
end
end
def common_switch(name)
case name
when :quiet then [["-q", "--quiet"], :quiet]
when :verbose then [["-v", "--verbose"], :verbose]
when :debug then [["-d", "--debug"], :debug]
else name
end
end
end
end
end

View File

@ -63,6 +63,8 @@ module Homebrew
switch "--display-cop-names"
switch "--display-filename"
switch "-D", "--audit-debug", description: "Activates debugging and profiling"
switch :verbose
switch :debug
comma_array "--only"
comma_array "--except"
comma_array "--only-cops"

View File

@ -72,7 +72,6 @@ module Homebrew
def bottle
@args = Homebrew::CLI::Parser.parse do
switch "--merge"
switch "-v", "--verbose"
switch "--skip-relocation"
switch "--force-core-tap"
switch "--no-rebuild"
@ -80,6 +79,7 @@ module Homebrew
switch "--write"
switch "--no-commit"
switch "--json"
switch :verbose
flag "--root-url"
end

View File

@ -2,9 +2,14 @@ require_relative "../cli_parser"
describe Homebrew::CLI::Parser do
describe "test switch options" do
before do
allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1")
allow(ENV).to receive(:[]).with("HOMEBREW_VERBOSE")
end
subject(:parser) {
described_class.new do
switch "-v", "--verbose", description: "Flag for verbosity"
switch :verbose, description: "Flag for verbosity"
switch "--more-verbose", description: "Flag for higher verbosity"
switch "--pry", env: :pry
end
@ -37,7 +42,6 @@ describe Homebrew::CLI::Parser do
end
it "maps environment var to an option" do
allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1")
args = parser.parse([])
expect(args.pry?).to be true
end