Port Homebrew::Cmd::Options
This commit is contained in:
parent
c5dfac1f2c
commit
57442ab67e
@ -1,70 +1,71 @@
|
|||||||
# typed: true
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "abstract_command"
|
||||||
require "formula"
|
require "formula"
|
||||||
require "options"
|
require "options"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module Cmd
|
||||||
|
class OptionsCmd < AbstractCommand
|
||||||
|
cmd_args do
|
||||||
|
description <<~EOS
|
||||||
|
Show install options specific to <formula>.
|
||||||
|
EOS
|
||||||
|
switch "--compact",
|
||||||
|
description: "Show all options on a single line separated by spaces."
|
||||||
|
switch "--installed",
|
||||||
|
description: "Show options for formulae that are currently installed."
|
||||||
|
switch "--eval-all",
|
||||||
|
description: "Evaluate all available formulae and casks, whether installed or not, to show their " \
|
||||||
|
"options."
|
||||||
|
flag "--command=",
|
||||||
|
description: "Show options for the specified <command>."
|
||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
conflicts "--installed", "--all", "--command"
|
||||||
def options_args
|
|
||||||
Homebrew::CLI::Parser.new do
|
|
||||||
description <<~EOS
|
|
||||||
Show install options specific to <formula>.
|
|
||||||
EOS
|
|
||||||
switch "--compact",
|
|
||||||
description: "Show all options on a single line separated by spaces."
|
|
||||||
switch "--installed",
|
|
||||||
description: "Show options for formulae that are currently installed."
|
|
||||||
switch "--eval-all",
|
|
||||||
description: "Evaluate all available formulae and casks, whether installed or not, to show their " \
|
|
||||||
"options."
|
|
||||||
flag "--command=",
|
|
||||||
description: "Show options for the specified <command>."
|
|
||||||
|
|
||||||
conflicts "--installed", "--all", "--command"
|
named_args :formula
|
||||||
|
|
||||||
named_args :formula
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def options
|
|
||||||
args = options_args.parse
|
|
||||||
|
|
||||||
all = args.eval_all?
|
|
||||||
|
|
||||||
if all
|
|
||||||
puts_options(Formula.all(eval_all: args.eval_all?).sort, args:)
|
|
||||||
elsif args.installed?
|
|
||||||
puts_options(Formula.installed.sort, args:)
|
|
||||||
elsif args.command.present?
|
|
||||||
cmd_options = Commands.command_options(args.command)
|
|
||||||
odie "Unknown command: #{args.command}" if cmd_options.nil?
|
|
||||||
|
|
||||||
if args.compact?
|
|
||||||
puts cmd_options.sort.map(&:first) * " "
|
|
||||||
else
|
|
||||||
cmd_options.sort.each { |option, desc| puts "#{option}\n\t#{desc}" }
|
|
||||||
puts
|
|
||||||
end
|
end
|
||||||
elsif args.no_named?
|
|
||||||
raise FormulaUnspecifiedError
|
|
||||||
else
|
|
||||||
puts_options args.named.to_formulae, args:
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def puts_options(formulae, args:)
|
sig { override.void }
|
||||||
formulae.each do |f|
|
def run
|
||||||
next if f.options.empty?
|
all = args.eval_all?
|
||||||
|
|
||||||
if args.compact?
|
if all
|
||||||
puts f.options.as_flags.sort * " "
|
puts_options(Formula.all(eval_all: args.eval_all?).sort)
|
||||||
else
|
elsif args.installed?
|
||||||
puts f.full_name if formulae.length > 1
|
puts_options(Formula.installed.sort)
|
||||||
Options.dump_for_formula f
|
elsif args.command.present?
|
||||||
puts
|
cmd_options = Commands.command_options(T.must(args.command))
|
||||||
|
odie "Unknown command: #{args.command}" if cmd_options.nil?
|
||||||
|
|
||||||
|
if args.compact?
|
||||||
|
puts cmd_options.sort.map(&:first) * " "
|
||||||
|
else
|
||||||
|
cmd_options.sort.each { |option, desc| puts "#{option}\n\t#{desc}" }
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
elsif args.no_named?
|
||||||
|
raise FormulaUnspecifiedError
|
||||||
|
else
|
||||||
|
puts_options args.named.to_formulae
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def puts_options(formulae)
|
||||||
|
formulae.each do |f|
|
||||||
|
next if f.options.empty?
|
||||||
|
|
||||||
|
if args.compact?
|
||||||
|
puts f.options.as_flags.sort * " "
|
||||||
|
else
|
||||||
|
puts f.full_name if formulae.length > 1
|
||||||
|
Options.dump_for_formula f
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "cmd/options"
|
||||||
require "cmd/shared_examples/args_parse"
|
require "cmd/shared_examples/args_parse"
|
||||||
|
|
||||||
RSpec.describe "brew options" do
|
RSpec.describe Homebrew::Cmd::OptionsCmd do
|
||||||
it_behaves_like "parseable arguments"
|
it_behaves_like "parseable arguments"
|
||||||
|
|
||||||
it "prints a given Formula's options", :integration_test do
|
it "prints a given Formula's options", :integration_test do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user