69 lines
1.9 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "descriptions"
2018-06-05 10:55:00 +02:00
require "search"
require "description_cache_store"
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2018-06-05 10:55:00 +02:00
extend Search
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
2018-10-25 12:27:12 +05:30
def desc_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`desc` [<options>] <text>|`/`<text>`/`|<formula> [<text>|`/`<text>`/`|<formula> ...]
2018-10-25 12:27:12 +05:30
Display <formula>'s name and one-line description.
Formula descriptions are cached; the cache is created on the
first search, making that search slower than subsequent ones.
EOS
2020-03-10 23:18:40 -04:00
switch "-s", "--search",
description: "Search both names and descriptions for <text>. If <text> is flanked by "\
2019-08-06 13:23:19 -04:00
"slashes, it is interpreted as a regular expression."
2020-03-10 23:18:40 -04:00
switch "-n", "--name",
description: "Search just names for <text>. If <text> is flanked by slashes, it is "\
2019-08-06 13:23:19 -04:00
"interpreted as a regular expression."
2020-03-10 23:18:40 -04:00
switch "-d", "--description",
description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\
2019-08-06 13:23:19 -04:00
"it is interpreted as a regular expression."
2020-07-30 18:40:10 +02:00
2020-03-10 23:18:40 -04:00
conflicts "--search", "--name", "--description"
2021-01-10 14:26:40 -05:00
named_args :formula, min: 1
2018-10-25 12:27:12 +05:30
end
end
def desc
2020-07-30 18:40:10 +02:00
args = desc_args.parse
2018-10-25 12:27:12 +05:30
2020-03-10 23:18:40 -04:00
search_type = if args.search?
:either
elsif args.name?
:name
elsif args.description?
:desc
end
2018-09-17 02:45:00 +02:00
2020-03-10 23:18:40 -04:00
results = if search_type.nil?
desc = {}
args.named.to_formulae.each { |f| desc[f.full_name] = f.desc }
Descriptions.new(desc)
else
2020-03-10 23:18:40 -04:00
query = args.named.join(" ")
string_or_regex = query_regexp(query)
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
2020-03-10 23:18:40 -04:00
Descriptions.search(string_or_regex, search_type, cache_store)
end
end
results.print
end
end