Cleanup description handling

This commit is contained in:
Rylan Polster 2021-01-24 16:42:28 -05:00
parent 67e03a0675
commit 0c5edf4004
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
3 changed files with 29 additions and 27 deletions

View File

@ -230,22 +230,27 @@ module Commands
end end
end end
def command_description(command) def command_description(command, short: false)
path = self.path(command) path = self.path(command)
return if path.blank? return if path.blank?
if cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path) if cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path)
if short
cmd_parser.description.split(".").first
else
cmd_parser.description cmd_parser.description
end
else else
comment_lines = path.read.lines.grep(/^#:/) comment_lines = path.read.lines.grep(/^#:/)
# skip the comment's initial usage summary lines # skip the comment's initial usage summary lines
comment_lines.slice(2..-1)&.each do |line| comment_lines.slice(2..-1)&.each do |line|
if /^#: (?<desc>\w.*+)$/ =~ line if /^#: (?<desc>\w.*+)$/ =~ line
return desc.split(".").first if short
return desc return desc
end end
end end
[]
end end
end end

View File

@ -107,6 +107,8 @@ module Homebrew
def update_shell_completions! def update_shell_completions!
commands = Commands.commands(external: false, aliases: true).sort commands = Commands.commands(external: false, aliases: true).sort
puts "Writing completions to #{COMPLETIONS_DIR}"
(COMPLETIONS_DIR/"bash/brew").atomic_write generate_bash_completion_file(commands) (COMPLETIONS_DIR/"bash/brew").atomic_write generate_bash_completion_file(commands)
(COMPLETIONS_DIR/"zsh/_brew").atomic_write generate_zsh_completion_file(commands) (COMPLETIONS_DIR/"zsh/_brew").atomic_write generate_zsh_completion_file(commands)
end end
@ -130,7 +132,7 @@ module Homebrew
next if option.blank? next if option.blank?
name = option.first name = option.first
desc = format_description option.second desc = option.second
if name.start_with? "--[no-]" if name.start_with? "--[no-]"
options[name.remove("[no-]")] = desc options[name.remove("[no-]")] = desc
options[name.sub("[no-]", "no-")] = desc options[name.sub("[no-]", "no-")] = desc
@ -197,7 +199,7 @@ module Homebrew
options = command_options(command).sort.map do |opt, desc| options = command_options(command).sort.map do |opt, desc|
next opt if desc.blank? next opt if desc.blank?
"#{opt}[#{desc}]" "#{opt}[#{format_description desc}]"
end end
if types = Commands.named_args_type(command) if types = Commands.named_args_type(command)
named_args_strings, named_args_types = types.partition { |type| type.is_a? String } named_args_strings, named_args_types = types.partition { |type| type.is_a? String }
@ -233,10 +235,10 @@ module Homebrew
variables[:builtin_command_descriptions] = commands.map do |command| variables[:builtin_command_descriptions] = commands.map do |command|
next if Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key? command next if Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key? command
description = Commands.command_description(command) description = Commands.command_description(command, short: true)
next if description.blank? next if description.blank?
description = format_description description.split(".").first description = format_description description
"'#{command}:#{description}'" "'#{command}:#{description}'"
end.compact end.compact

View File

@ -154,7 +154,7 @@ describe Homebrew::Completions do
end end
describe ".format_description" do describe ".format_description" do
it "escapes single quotes for shells" do it "escapes single quotes" do
expect(described_class.format_description("Homebrew's")).to eq "Homebrew'\\''s" expect(described_class.format_description("Homebrew's")).to eq "Homebrew'\\''s"
end end
@ -174,24 +174,24 @@ describe Homebrew::Completions do
describe ".command_options" do describe ".command_options" do
it "returns an array of options for a ruby command" do it "returns an array of options for a ruby command" do
expected_options = { expected_options = {
"--debug" => "Display any debugging information", "--debug" => "Display any debugging information.",
"--help" => "Show this message", "--help" => "Show this message.",
"--hide" => "Act as if none of the specified hidden are installed. hidden should be " \ "--hide" => "Act as if none of the specified <hidden> are installed. <hidden> should be " \
"a comma-separated list of formulae", "a comma-separated list of formulae.",
"--quiet" => "Make some output more quiet", "--quiet" => "Make some output more quiet.",
"--verbose" => "Make some output more verbose", "--verbose" => "Make some output more verbose.",
} }
expect(described_class.command_options("missing")).to eq expected_options expect(described_class.command_options("missing")).to eq expected_options
end end
it "returns an array of options for a shell command" do it "returns an array of options for a shell command" do
expected_options = { expected_options = {
"--debug" => "Display a trace of all shell commands as they are executed", "--debug" => "Display a trace of all shell commands as they are executed.",
"--force" => "Always do a slower, full update check (even if unnecessary)", "--force" => "Always do a slower, full update check (even if unnecessary).",
"--help" => "Show this message", "--help" => "Show this message.",
"--merge" => "Use `git merge` to apply updates (rather than `git rebase`)", "--merge" => "Use `git merge` to apply updates (rather than `git rebase`).",
"--preinstall" => "Run on auto-updates (e.g. before `brew install`). Skips some slower steps", "--preinstall" => "Run on auto-updates (e.g. before `brew install`). Skips some slower steps.",
"--verbose" => "Print the directories checked and `git` operations performed", "--verbose" => "Print the directories checked and `git` operations performed.",
} }
expect(described_class.command_options("update")).to eq expected_options expect(described_class.command_options("update")).to eq expected_options
end end
@ -211,14 +211,9 @@ describe Homebrew::Completions do
expect(described_class.command_options("help")).to eq({}) expect(described_class.command_options("help")).to eq({})
end end
it "will list global options only once if overriden" do it "will override global options with local descriptions" do
# count = 0
options = described_class.command_options("upgrade") options = described_class.command_options("upgrade")
# options.each_key do |opt| expect(options["--verbose"]).to eq "Print the verification and postinstall steps."
# count += 1 if opt == "--verbose"
# end
# expect(count).to eq 1
expect(options["--verbose"]).to eq "Print the verification and postinstall steps"
end end
end end