Port Homebrew::Cmd::CompletionsCmd

This commit is contained in:
Douglas Eichelberger 2024-03-29 16:26:55 -07:00
parent 02e2772e9d
commit b97f9b22e2
2 changed files with 35 additions and 37 deletions

View File

@ -1,49 +1,46 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "cli/parser" require "abstract_command"
require "completions" require "completions"
module Homebrew module Homebrew
module_function module Cmd
class CompletionsCmd < AbstractCommand
cmd_args do
description <<~EOS
Control whether Homebrew automatically links external tap shell completion files.
Read more at <https://docs.brew.sh/Shell-Completion>.
sig { returns(CLI::Parser) } `brew completions` [`state`]:
def completions_args Display the current state of Homebrew's completions.
Homebrew::CLI::Parser.new do
description <<~EOS
Control whether Homebrew automatically links external tap shell completion files.
Read more at <https://docs.brew.sh/Shell-Completion>.
`brew completions` [`state`]: `brew completions` (`link`|`unlink`):
Display the current state of Homebrew's completions. Link or unlink Homebrew's completions.
EOS
`brew completions` (`link`|`unlink`): named_args %w[state link unlink], max: 1
Link or unlink Homebrew's completions. end
EOS
sig { override.void }
named_args %w[state link unlink], max: 1 def run
end case args.named.first
end when nil, "state"
if Completions.link_completions?
sig { void } puts "Completions are linked."
def completions else
args = completions_args.parse puts "Completions are not linked."
end
case args.named.first when "link"
when nil, "state" Completions.link!
if Completions.link_completions? puts "Completions are now linked."
puts "Completions are linked." when "unlink"
else Completions.unlink!
puts "Completions are not linked." puts "Completions are no longer linked."
else
raise UsageError, "unknown subcommand: #{args.named.first}"
end
end end
when "link"
Completions.link!
puts "Completions are now linked."
when "unlink"
Completions.unlink!
puts "Completions are no longer linked."
else
raise UsageError, "unknown subcommand: #{args.named.first}"
end end
end end
end end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/completions"
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
RSpec.describe "brew completions" do RSpec.describe Homebrew::Cmd::CompletionsCmd do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "runs the status subcommand correctly", :integration_test do it "runs the status subcommand correctly", :integration_test do