help: refactor (again) and fix code style issues
Turns out making `empty_argv` a boolean argument for `Homebrew.help` was not the best idea and having command-to-path mapping and help extraction in a single method is not flexible enough. Also only complain about missing help text when `HOMEBREW_DEVELOPER=1` and otherwise just print the generic help text.
This commit is contained in:
parent
4f8e3cae5e
commit
557ad956fd
@ -33,9 +33,15 @@ EOS
|
|||||||
# NOTE The reason the string is at the top is so 25 lines is easy to measure!
|
# NOTE The reason the string is at the top is so 25 lines is easy to measure!
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
def help(cmd = nil, empty_argv = false)
|
def help(cmd = nil, flags = {})
|
||||||
|
# Resolve command aliases and find file containing the implementation.
|
||||||
|
if cmd
|
||||||
|
cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
|
||||||
|
path = command_path(cmd)
|
||||||
|
end
|
||||||
|
|
||||||
# Handle `brew` (no arguments).
|
# Handle `brew` (no arguments).
|
||||||
if empty_argv
|
if flags[:empty_argv]
|
||||||
$stderr.puts HOMEBREW_HELP
|
$stderr.puts HOMEBREW_HELP
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
@ -46,24 +52,18 @@ module Homebrew
|
|||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get help text and if `nil` (external commands), resume in `brew.rb`.
|
# Resume execution in `brew.rb` for external/unknown commands.
|
||||||
help_text = help_for_command(cmd)
|
return if path.nil?
|
||||||
return if help_text.nil?
|
|
||||||
|
|
||||||
# Display help for internal command (or generic help if undocumented).
|
# Display help for internal command (or generic help if undocumented).
|
||||||
if help_text.empty?
|
puts command_help(path)
|
||||||
opoo "No help available for '#{cmd}' command."
|
|
||||||
help_text = HOMEBREW_HELP
|
|
||||||
end
|
|
||||||
puts help_text
|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def help_for_command(cmd)
|
def command_path(cmd)
|
||||||
cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
|
if File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh")
|
||||||
cmd_path = if File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh")
|
|
||||||
HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh"
|
HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh"
|
||||||
elsif ARGV.homebrew_developer? && File.exist?(HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.sh")
|
elsif ARGV.homebrew_developer? && File.exist?(HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.sh")
|
||||||
HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.sh"
|
HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.sh"
|
||||||
@ -72,15 +72,20 @@ module Homebrew
|
|||||||
elsif ARGV.homebrew_developer? && File.exist?(HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.rb")
|
elsif ARGV.homebrew_developer? && File.exist?(HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.rb")
|
||||||
HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.rb"
|
HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.rb"
|
||||||
end
|
end
|
||||||
return if cmd_path.nil?
|
end
|
||||||
|
|
||||||
cmd_path.read.
|
def command_help(path)
|
||||||
split("\n").
|
help_lines = path.read.lines.grep(/^#:/)
|
||||||
grep(/^#:/).
|
if help_lines.empty?
|
||||||
map do |line|
|
opoo "No help text in: #{path}" if ARGV.homebrew_developer?
|
||||||
line.slice(2..-1).sub(/^ \* /, "#{Tty.highlight}brew#{Tty.reset} ").
|
HOMEBREW_HELP
|
||||||
|
else
|
||||||
|
help_lines.map do |line|
|
||||||
|
line.slice(2..-1).
|
||||||
|
sub(/^ \* /, "#{Tty.highlight}brew#{Tty.reset} ").
|
||||||
gsub(/`(.*?)`/, "#{Tty.highlight}\\1#{Tty.reset}").
|
gsub(/`(.*?)`/, "#{Tty.highlight}\\1#{Tty.reset}").
|
||||||
gsub(/<(.*?)>/, "#{Tty.em}\\1#{Tty.reset}")
|
gsub(/<(.*?)>/, "#{Tty.em}\\1#{Tty.reset}")
|
||||||
end.join("\n")
|
end.join
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -74,7 +74,8 @@ begin
|
|||||||
# arguments themselves.
|
# arguments themselves.
|
||||||
if empty_argv || help_flag
|
if empty_argv || help_flag
|
||||||
require "cmd/help"
|
require "cmd/help"
|
||||||
Homebrew.help cmd, empty_argv # Never returns, except for external command.
|
Homebrew.help cmd, :empty_argv => empty_argv
|
||||||
|
# `Homebrew.help` never returns, except for external/unknown commands.
|
||||||
end
|
end
|
||||||
|
|
||||||
if internal_cmd
|
if internal_cmd
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user