help: handle help output (move from 'brew.rb')

Keep the footprint of `brew.rb` small. Handle fetching/displaying an
appropriate help text (taking into account various external conditions)
in the `help` command.
This commit is contained in:
Martin Afanasjew 2016-04-17 03:45:10 +02:00
parent 144d86f733
commit 3503806e77
2 changed files with 29 additions and 25 deletions

View File

@ -32,14 +32,38 @@ 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 def help(cmd = nil, empty_argv = false)
puts HOMEBREW_HELP # Handle `brew` (no arguments).
if empty_argv
$stderr.puts HOMEBREW_HELP
exit 1
end
# Handle `brew (-h|--help|--usage|-?|help)` (no other arguments).
if cmd.nil?
puts HOMEBREW_HELP
exit 0
end
# Get help text and if `nil` (external commands), resume in `brew.rb`.
help_text = help_for_command(cmd)
return if help_text.nil?
# Display help for internal command (or generic help if undocumented).
if help_text.empty?
opoo "No help available for '#{cmd}' command."
help_text = HOMEBREW_HELP
end
puts help_text
exit 0
end end
def help_s def help_s
HOMEBREW_HELP HOMEBREW_HELP
end end
private
def help_for_command(cmd) def help_for_command(cmd)
cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd) cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
cmd_path = if File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh") cmd_path = if File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh")

View File

@ -68,29 +68,9 @@ begin
# #
# It should never affect external commands so they can handle usage # It should never affect external commands so they can handle usage
# arguments themselves. # arguments themselves.
if empty_argv || help_flag
if empty_argv require "cmd/help"
$stderr.puts ARGV.usage Homebrew.help cmd, empty_argv # Never returns, except for external command.
exit 1
elsif help_flag
if cmd.nil?
puts ARGV.usage
exit 0
else
# Handle both internal ruby and shell commands
require "cmd/help"
help_text = Homebrew.help_for_command(cmd)
if help_text.nil?
# External command, let it handle help by itself
elsif help_text.empty?
opoo "No help available for '#{cmd}' command."
puts ARGV.usage
exit 0
else
puts help_text
exit 0
end
end
end end
if internal_cmd if internal_cmd