From 57442ab67e4672f8003eca59d85d95ce3d76074a Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 1 Apr 2024 09:10:51 -0700 Subject: [PATCH] Port Homebrew::Cmd::Options --- Library/Homebrew/cmd/options.rb | 111 +++++++++++----------- Library/Homebrew/test/cmd/options_spec.rb | 3 +- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb index 6d93d75833..733e9f0a9e 100644 --- a/Library/Homebrew/cmd/options.rb +++ b/Library/Homebrew/cmd/options.rb @@ -1,70 +1,71 @@ # typed: true # frozen_string_literal: true +require "abstract_command" require "formula" require "options" module Homebrew - module_function + module Cmd + class OptionsCmd < AbstractCommand + cmd_args do + description <<~EOS + Show install options specific to . + 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 ." - sig { returns(CLI::Parser) } - def options_args - Homebrew::CLI::Parser.new do - description <<~EOS - Show install options specific to . - 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 ." + conflicts "--installed", "--all", "--command" - conflicts "--installed", "--all", "--command" - - 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 + named_args :formula end - elsif args.no_named? - raise FormulaUnspecifiedError - else - puts_options args.named.to_formulae, args: - end - end - def puts_options(formulae, args:) - formulae.each do |f| - next if f.options.empty? + sig { override.void } + def run + all = args.eval_all? - if args.compact? - puts f.options.as_flags.sort * " " - else - puts f.full_name if formulae.length > 1 - Options.dump_for_formula f - puts + if all + puts_options(Formula.all(eval_all: args.eval_all?).sort) + elsif args.installed? + puts_options(Formula.installed.sort) + elsif args.command.present? + 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 diff --git a/Library/Homebrew/test/cmd/options_spec.rb b/Library/Homebrew/test/cmd/options_spec.rb index b7498aed15..53848baaf8 100644 --- a/Library/Homebrew/test/cmd/options_spec.rb +++ b/Library/Homebrew/test/cmd/options_spec.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true +require "cmd/options" require "cmd/shared_examples/args_parse" -RSpec.describe "brew options" do +RSpec.describe Homebrew::Cmd::OptionsCmd do it_behaves_like "parseable arguments" it "prints a given Formula's options", :integration_test do