aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/help.rb
blob: f8524121cdaf1bf4bea182dc8f1c3ac4ee3696de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
HOMEBREW_HELP = <<-EOS
Example usage:
  brew (info|home|options) [FORMULA...]
  brew install FORMULA...
  brew uninstall FORMULA...
  brew search [TEXT|/PATTERN/]
  brew list [FORMULA...]
  brew update
  brew upgrade [FORMULA...]
  brew (pin|unpin) [FORMULA...]

Troubleshooting:
  brew doctor
  brew install -vd FORMULA
  brew (--env|config)

Brewing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]
  https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Formula-Cookbook.md

Further help:
  man brew
  brew help [COMMAND]
  brew home
EOS

# NOTE Keep the lenth of vanilla --help less than 25 lines!
# This is because the default Terminal height is 25 lines. Scrolling sucks
# and concision is important. If more help is needed we should start
# specialising help like the gem command does.
# NOTE Keep lines less than 80 characters! Wrapping is just not cricket.
# 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)
    # 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

  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")
      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"
    elsif File.exist?(HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.rb")
      HOMEBREW_LIBRARY_PATH/"cmd/#{cmd}.rb"
    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?

    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")
  end
end