diff --git a/Library/Homebrew/cli_parser.rb b/Library/Homebrew/cli_parser.rb index 0ea8af6a66..be8962363f 100644 --- a/Library/Homebrew/cli_parser.rb +++ b/Library/Homebrew/cli_parser.rb @@ -5,8 +5,8 @@ require "set" module Homebrew module CLI class Parser - def self.parse(&block) - new(&block).parse + def self.parse(args = ARGV, &block) + new(&block).parse(args) end def initialize(&block) @@ -60,17 +60,28 @@ module Homebrew @conflicts << options.map { |option| option_to_name(option) } end - def option_to_name(name) - name.sub(/\A--?/, "").tr("-", "_").delete("=") + def option_to_name(option) + option.sub(/\A--?/, "") + .tr("-", "_") + .delete("=") + end + + def name_to_option(name) + if name.length == 1 + "-#{name}" + else + "--#{name}" + end end def option_to_description(*names) names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max end - def parse(cmdline_args = ARGV) - @parser.parse(cmdline_args) + def parse(cmdline_args) + remaining_args = @parser.parse(cmdline_args) check_constraint_violations + Homebrew.args[:remaining] = remaining_args end private @@ -126,7 +137,9 @@ module Homebrew violations = mutually_exclusive_options_group.select do |option| option_passed? option end - raise OptionConflictError, violations if violations.length > 1 + + next if violations.count < 2 + raise OptionConflictError, violations.map(&method(:name_to_option)) end end @@ -163,9 +176,10 @@ module Homebrew class OptionConflictError < RuntimeError def initialize(args) - args_list = args.join("` and `") + args_list = args.map(&Formatter.public_method(:option)) + .join(" and ") super <<~EOS - `#{args_list}` are mutually exclusive + Options #{args_list} are mutually exclusive. EOS end end diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index a9afe5ad7d..5043468884 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -16,31 +16,45 @@ require "formula" require "missing_formula" require "descriptions" +require "cli_parser" module Homebrew module_function - def search - if ARGV.empty? + PACKAGE_MANAGERS = { + macports: ->(query) { "https://www.macports.org/ports.php?by=name&substr=#{query}" }, + fink: ->(query) { "http://pdb.finkproject.org/pdb/browse.php?summary=#{query}" }, + debian: ->(query) { "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all§ion=all" }, + opensuse: ->(query) { "https://software.opensuse.org/search?q=#{query}" }, + fedora: ->(query) { "https://apps.fedoraproject.org/packages/s/#{query}" }, + ubuntu: ->(query) { "https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all§ion=all" }, + }.freeze + + def search(argv = ARGV) + CLI::Parser.parse(argv) do + switch "--desc" + + package_manager_switches = PACKAGE_MANAGERS.keys.map { |name| "--#{name}" } + + package_manager_switches.each do |s| + switch s + end + + conflicts(*package_manager_switches) + end + + PACKAGE_MANAGERS.each do |name, url| + exec_browser url.call(URI.encode_www_form_component(args.remaining.join(" "))) if args[:"#{name}?"] + end + + if args.remaining.empty? puts Formatter.columns(Formula.full_names.sort) - elsif ARGV.include? "--macports" - exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}" - elsif ARGV.include? "--fink" - exec_browser "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}" - elsif ARGV.include? "--debian" - exec_browser "https://packages.debian.org/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all" - elsif ARGV.include? "--opensuse" - exec_browser "https://software.opensuse.org/search?q=#{ARGV.next}" - elsif ARGV.include? "--fedora" - exec_browser "https://apps.fedoraproject.org/packages/s/#{ARGV.next}" - elsif ARGV.include? "--ubuntu" - exec_browser "https://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all" - elsif ARGV.include? "--desc" - query = ARGV.next + elsif args.desc? + query = args.remaining.first regex = query_regexp(query) Descriptions.search(regex, :desc).print - elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX - query = ARGV.first + elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX + query = args.remaining.first begin result = Formulary.factory(query).name @@ -52,7 +66,7 @@ module Homebrew puts Formatter.columns(results.sort) unless results.empty? else - query = ARGV.first + query = args.remaining.first regex = query_regexp(query) local_results = search_formulae(regex) puts Formatter.columns(local_results.sort) unless local_results.empty? @@ -78,10 +92,10 @@ module Homebrew end return unless $stdout.tty? - return if ARGV.empty? + return if args.remaining.empty? metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze return unless metacharacters.any? do |char| - ARGV.any? do |arg| + args.remaining.any? do |arg| arg.include?(char) && !arg.start_with?("/") end end diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index a29b43c8de..ec144bf2f8 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -15,6 +15,10 @@ module Formatter "#{Tty.green}#{string}#{Tty.default}" end + def option(string) + "#{Tty.bold}#{string}#{Tty.reset}" + end + def success(string, label: nil) label(label, string, :green) end