aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cmd/desc.rb
blob: 53291602e1f94a6b9f938e9a0926f8c769dbd821 (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
#:  * `desc` <formula>:
#:    Display <formula>'s name and one-line description.
#:
#:  * `desc` [`-s`|`-n`|`-d`] (<text>|`/`<text>`/`):
#:    Search both name and description (`-s`), just the names (`-n`), or just  the
#:    descriptions (`-d`) for <text>. If <text> is flanked by slashes, it is interpreted
#:    as a regular expression. Formula descriptions are cached; the cache is created on
#:    the first search, making that search slower than subsequent ones.

require "descriptions"
require "cmd/search"

module Homebrew
  module_function

  def desc
    search_type = []
    search_type << :either if ARGV.flag? "--search"
    search_type << :name   if ARGV.flag? "--name"
    search_type << :desc   if ARGV.flag? "--description"

    if search_type.empty?
      raise FormulaUnspecifiedError if ARGV.named.empty?
      desc = {}
      ARGV.formulae.each { |f| desc[f.full_name] = f.desc }
      results = Descriptions.new(desc)
      results.print
    elsif search_type.size > 1
      odie "Pick one, and only one, of -s/--search, -n/--name, or -d/--description."
    elsif arg = ARGV.named.first
      regex = Homebrew.query_regexp(arg)
      results = Descriptions.search(regex, search_type.first)
      results.print
    else
      odie "You must provide a search term."
    end
  end
end