desc: fix search options

This commit is contained in:
EricFromCanada 2020-03-10 23:18:40 -04:00
parent 30c4383e86
commit d068d87b1d

View File

@ -19,41 +19,42 @@ module Homebrew
Formula descriptions are cached; the cache is created on the Formula descriptions are cached; the cache is created on the
first search, making that search slower than subsequent ones. first search, making that search slower than subsequent ones.
EOS EOS
flag "-s", "--search=", switch "-s", "--search",
description: "Search both names and descriptions for <text>. If <text> is flanked by "\ description: "Search both names and descriptions for <text>. If <text> is flanked by "\
"slashes, it is interpreted as a regular expression." "slashes, it is interpreted as a regular expression."
flag "-n", "--name=", switch "-n", "--name",
description: "Search just names for <text>. If <text> is flanked by slashes, it is "\ description: "Search just names for <text>. If <text> is flanked by slashes, it is "\
"interpreted as a regular expression." "interpreted as a regular expression."
flag "-d", "--description=", switch "-d", "--description",
description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\ description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\
"it is interpreted as a regular expression." "it is interpreted as a regular expression."
switch :verbose switch :verbose
conflicts "--search=", "--name=", "--description=" conflicts "--search", "--name", "--description"
min_named 1
end end
end end
def desc def desc
desc_args.parse desc_args.parse
search_type = [] search_type = if args.search?
search_type << :either if args.search :either
search_type << :name if args.name elsif args.name?
search_type << :desc if args.description :name
odie "You must provide a search term." if search_type.present? && args.no_named? elsif args.description?
:desc
results = if search_type.empty? end
raise FormulaUnspecifiedError if args.no_named?
results = if search_type.nil?
desc = {} desc = {}
args.formulae.each { |f| desc[f.full_name] = f.desc } args.formulae.each { |f| desc[f.full_name] = f.desc }
Descriptions.new(desc) Descriptions.new(desc)
else else
arg = args.named.join(" ") query = args.named.join(" ")
string_or_regex = query_regexp(arg) string_or_regex = query_regexp(query)
CacheStoreDatabase.use(:descriptions) do |db| CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db) cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type.first, cache_store) Descriptions.search(string_or_regex, search_type, cache_store)
end end
end end