aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/help.rb
blob: a0c44854b688cc979731a97d3c6e6c73ec827e5f (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
87
88
89
90
91
92
HOMEBREW_HELP = <<-EOS.unindent.freeze
  Example usage:
    brew search [TEXT|/REGEX/]
    brew (info|home|options) [FORMULA...]
    brew install FORMULA...
    brew update
    brew upgrade [FORMULA...]
    brew uninstall FORMULA...
    brew list [FORMULA...]

  Troubleshooting:
    brew config
    brew doctor
    brew install -vd FORMULA

  Developers:
    brew create [URL [--no-fetch]]
    brew edit [FORMULA...]
    https://docs.brew.sh/Formula-Cookbook.html

  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!

require "commands"

module Homebrew
  module_function

  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 = Commands.path(cmd)
      path ||= which("brew-#{cmd}")
      path ||= which("brew-#{cmd}.rb")
    end

    # Display command-specific (or generic) help in response to `UsageError`.
    if (error_message = flags[:usage_error])
      $stderr.puts path ? command_help(path) : HOMEBREW_HELP
      $stderr.puts
      onoe error_message
      exit 1
    end

    # Handle `brew` (no arguments).
    if flags[: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

    # Resume execution in `brew.rb` for unknown commands.
    return if path.nil?

    # Display help for internal command (or generic help if undocumented).
    puts command_help(path)
    exit 0
  end

  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.bold}brew#{Tty.reset} ")
            .gsub(/`(.*?)`/, "#{Tty.bold}\\1#{Tty.reset}")
            .gsub(%r{<([^\s]+?://[^\s]+?)>}) { |url| Formatter.url(url) }
            .gsub(/<(.*?)>/, "#{Tty.underline}\\1#{Tty.reset}")
            .gsub("@hide_from_man_page", "")
      end.join.strip
    end
  end
end