77 lines
2.6 KiB
Ruby
Raw Normal View History

2023-03-06 09:49:53 -08:00
# typed: true
# 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
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
description <<~EOS
2018-10-25 12:27:12 +05:30
Display <formula>'s name and one-line description.
The cache is created on the first search, making that search slower than subsequent ones.
2018-10-25 12:27:12 +05:30
EOS
2020-03-10 23:18:40 -04:00
switch "-s", "--search",
2022-06-28 10:09:59 +01:00
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",
2022-06-28 10:09:59 +01:00
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",
2022-06-28 10:09:59 +01:00
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."
switch "--eval-all",
description: "Evaluate all available formulae and casks, whether installed or not, to search their " \
2023-02-10 23:15:40 -05:00
"descriptions. Implied if `HOMEBREW_EVAL_ALL` is set."
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
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, :cask, :text_or_regex], 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
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
odisabled "brew desc", "brew desc --eval-all or HOMEBREW_EVAL_ALL"
end
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
if search_type.blank?
desc = {}
args.named.to_formulae_and_casks.each do |formula_or_cask|
if formula_or_cask.is_a? Formula
desc[formula_or_cask.full_name] = formula_or_cask.desc
else
description = formula_or_cask.desc.presence || Formatter.warning("[no description]")
desc[formula_or_cask.full_name] = "(#{formula_or_cask.name.join(", ")}) #{description}"
end
end
Descriptions.new(desc).print
else
2020-03-10 23:18:40 -04:00
query = args.named.join(" ")
string_or_regex = Search.query_regexp(query)
Search.search_descriptions(string_or_regex, args, search_type: search_type)
end
end
end