89 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module HomebrewArgvExtension
def flags_only
select { |arg| arg.start_with?("--") }
end
def formulae
2014-06-19 21:35:47 -05:00
require "formula"
(downcased_unique_named - casks).map do |name|
2015-08-09 22:43:01 +08:00
if name.include?("/") || File.exist?(name)
Formulary.factory(name, spec)
else
Formulary.find_with_priority(name, spec)
end
end.uniq(&:name)
end
def casks
# TODO: use @instance variable to ||= cache when moving to CLI::Parser
downcased_unique_named.grep HOMEBREW_CASK_TAP_CASK_REGEX
end
def value(name)
arg_prefix = "--#{name}="
flag_with_value = find { |arg| arg.start_with?(arg_prefix) }
2018-09-15 00:04:01 +02:00
flag_with_value&.delete_prefix(arg_prefix)
2013-07-07 09:14:10 -07:00
end
def debug?
flag?("--debug") || !ENV["HOMEBREW_DEBUG"].nil?
end
def bottle_arch
arch = value "bottle-arch"
2017-09-24 19:24:46 +01:00
arch&.to_sym
end
def cc
value "cc"
end
2014-03-13 10:10:59 -05:00
def env
value "env"
2014-03-13 10:10:59 -05:00
end
2014-08-28 21:35:52 -05:00
private
def options_only
select { |arg| arg.start_with?("-") }
end
def flag?(flag)
options_only.include?(flag) || switch?(flag[2, 1])
end
2019-04-17 17:34:57 +09:00
# e.g. `foo -ns -i --bar` has three switches: `n`, `s` and `i`
def switch?(char)
return false if char.length > 1
options_only.any? { |arg| arg.scan("-").size == 1 && arg.include?(char) }
end
def spec(default = :stable)
2014-06-19 21:35:47 -05:00
if include?("--HEAD")
:head
elsif include?("--devel")
:devel
else
2015-07-30 16:31:47 +08:00
default
2014-06-19 21:35:47 -05:00
end
end
def named
self - options_only
end
2010-08-07 22:15:29 -07:00
def downcased_unique_named
# Only lowercase names, not paths, bottle filenames or URLs
named.map do |arg|
if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
arg
else
arg.downcase
end
end.uniq
2010-08-07 22:15:29 -07:00
end
end