diff options
| author | Martin Afanasjew | 2016-04-19 07:24:21 +0200 |
|---|---|---|
| committer | Martin Afanasjew | 2016-04-20 13:46:10 +0200 |
| commit | 557ad956fdd9e3f403ea743d55a066341fab8132 (patch) | |
| tree | 64cd727aee2ac0d8190ecf48f06e63e217a84e6f | |
| parent | 4f8e3cae5e05c05b3681b3afd2f7ac480c522eb1 (diff) | |
| download | brew-557ad956fdd9e3f403ea743d55a066341fab8132.tar.bz2 | |
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.
| -rw-r--r-- | Library/Homebrew/cmd/help.rb | 49 | ||||
| -rw-r--r-- | Library/brew.rb | 3 |
2 files changed, 29 insertions, 23 deletions
diff --git a/Library/Homebrew/cmd/help.rb b/Library/Homebrew/cmd/help.rb index f8524121c..c8fc33430 100644 --- a/Library/Homebrew/cmd/help.rb +++ b/Library/Homebrew/cmd/help.rb @@ -33,9 +33,15 @@ EOS # NOTE The reason the string is at the top is so 25 lines is easy to measure! 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). - if empty_argv + if flags[:empty_argv] $stderr.puts HOMEBREW_HELP exit 1 end @@ -46,24 +52,18 @@ module Homebrew 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? + # Resume execution in `brew.rb` for external/unknown commands. + return if path.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 + puts command_help(path) exit 0 end private - def help_for_command(cmd) - cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd) - cmd_path = if File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.sh") + def command_path(cmd) + if File.exist?(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") 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") HOMEBREW_LIBRARY_PATH/"dev-cmd/#{cmd}.rb" end - return if cmd_path.nil? + end - cmd_path.read. - split("\n"). - grep(/^#:/). - map do |line| - line.slice(2..-1).sub(/^ \* /, "#{Tty.highlight}brew#{Tty.reset} "). - gsub(/`(.*?)`/, "#{Tty.highlight}\\1#{Tty.reset}"). - gsub(/<(.*?)>/, "#{Tty.em}\\1#{Tty.reset}") - end.join("\n") + def command_help(path) + help_lines = path.read.lines.grep(/^#:/) + if help_lines.empty? + opoo "No help text in: #{path}" if ARGV.homebrew_developer? + 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.em}\\1#{Tty.reset}") + end.join + end end end diff --git a/Library/brew.rb b/Library/brew.rb index 839ab6e36..eb4fe0606 100644 --- a/Library/brew.rb +++ b/Library/brew.rb @@ -74,7 +74,8 @@ begin # arguments themselves. if empty_argv || help_flag 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 if internal_cmd |
