brew/Library/Homebrew/options.rb

132 lines
2.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-08-17 19:53:37 +02:00
# A formula option.
#
# @api private
class Option
attr_reader :name, :description, :flag
def initialize(name, description = "")
@name = name
@flag = "--#{name}"
@description = description
end
def to_s
flag
end
def <=>(other)
2016-09-20 22:03:08 +02:00
return unless other.is_a?(Option)
2018-09-17 02:45:00 +02:00
name <=> other.name
end
2014-07-03 19:29:18 -05:00
def ==(other)
instance_of?(other.class) && name == other.name
end
2016-09-23 18:13:48 +02:00
alias eql? ==
def hash
name.hash
end
2013-05-24 10:56:02 -05:00
def inspect
"#<#{self.class.name}: #{flag.inspect}>"
2013-05-24 10:56:02 -05:00
end
end
2020-08-17 19:53:37 +02:00
# A deprecated formula option.
#
# @api private
class DeprecatedOption
attr_reader :old, :current
def initialize(old, current)
@old = old
@current = current
end
def old_flag
"--#{old}"
end
def current_flag
"--#{current}"
end
def ==(other)
instance_of?(other.class) && old == other.old && current == other.current
end
2016-09-23 18:13:48 +02:00
alias eql? ==
end
2020-08-17 19:53:37 +02:00
# A collection of formula options.
#
# @api private
class Options
include Enumerable
def self.create(array)
new array.map { |e| Option.new(e[/^--([^=]+=?)(.+)?$/, 1] || e) }
end
def initialize(*args)
@options = Set.new(*args)
end
def each(*args, &block)
@options.each(*args, &block)
end
2017-09-24 20:12:58 +01:00
def <<(other)
@options << other
self
end
2017-09-24 20:12:58 +01:00
def +(other)
self.class.new(@options + other)
end
2017-09-24 20:12:58 +01:00
def -(other)
self.class.new(@options - other)
end
2017-09-24 20:12:58 +01:00
def &(other)
self.class.new(@options & other)
end
2017-09-24 20:12:58 +01:00
def |(other)
self.class.new(@options | other)
2014-02-27 14:22:42 -06:00
end
2017-09-24 20:12:58 +01:00
def *(other)
@options.to_a * other
end
def empty?
@options.empty?
end
def as_flags
map(&:flag)
end
def include?(o)
any? { |opt| opt == o || opt.name == o || opt.flag == o }
end
2016-09-23 18:13:48 +02:00
alias to_ary to_a
2013-05-24 10:56:02 -05:00
def inspect
"#<#{self.class.name}: #{to_a.inspect}>"
2013-05-24 10:56:02 -05:00
end
2016-09-26 01:44:51 +02:00
2020-08-17 19:53:37 +02:00
def self.dump_for_formula(f)
f.options.sort_by(&:flag).each do |opt|
puts "#{opt.flag}\n\t#{opt.description}"
end
puts "--HEAD\n\tInstall HEAD version" if f.head
end
end