aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/options.rb
diff options
context:
space:
mode:
authorJack Nagel2013-01-23 00:26:28 -0600
committerJack Nagel2013-01-26 12:14:47 -0600
commit775184240d039973f3cd0751eb648d8840f70276 (patch)
treeca30d0b1e06f8e872e0d40f70a45ac70185ee9ec /Library/Homebrew/options.rb
parent7a8935455b43273291b37d2e2f62757bb8fa705e (diff)
downloadhomebrew-775184240d039973f3cd0751eb648d8840f70276.tar.bz2
FormulaInstaller: construct new ARGV from an Options collection
The array of options that is passed to the spawned build process is a combination of the current ARGV, options passed in by a dependent formula, and an existing install receipt. The objects that are interacting here each expect the resulting collection to have certain properties, and the expectations are not consistent. Clear up this confusing mess by only dealing with Options collections. This keeps our representation of options uniform across the codebase. We can remove BuildOptions dependency on HomebrewArgvExtension, which allows us to pass any Array-like collection to Tab.create. The only other site inside of FormulaInstaller that uses the array is the #exec call, and there it is splatted and thus we can substitute our Options collection there as well.
Diffstat (limited to 'Library/Homebrew/options.rb')
-rw-r--r--Library/Homebrew/options.rb34
1 files changed, 32 insertions, 2 deletions
diff --git a/Library/Homebrew/options.rb b/Library/Homebrew/options.rb
index 10abc1fae..e10d9483e 100644
--- a/Library/Homebrew/options.rb
+++ b/Library/Homebrew/options.rb
@@ -4,9 +4,8 @@ class Option
attr_reader :name, :description, :flag
def initialize(name, description=nil)
- @name = name.to_s[/^(?:--)?(.+)$/, 1]
+ @name, @flag = split_name(name)
@description = description.to_s
- @flag = "--#{@name}"
end
def to_s
@@ -29,6 +28,19 @@ class Option
def hash
name.hash
end
+
+ private
+
+ def split_name(name)
+ case name
+ when /^-[a-zA-Z]$/
+ [name[1..1], name]
+ when /^--(.+)$/
+ [$1, name]
+ else
+ [name, "--#{name}"]
+ end
+ end
end
class Options
@@ -55,6 +67,10 @@ class Options
Options.new(@options - o)
end
+ def &(o)
+ Options.new(@options & o)
+ end
+
def *(arg)
@options.to_a * arg
end
@@ -71,8 +87,22 @@ class Options
any? { |opt| opt == o || opt.name == o || opt.flag == o }
end
+ def concat(o)
+ o.each { |opt| @options << opt }
+ self
+ end
+
def to_a
@options.to_a
end
alias_method :to_ary, :to_a
+
+ def self.coerce(arg)
+ case arg
+ when self then arg
+ when Option then new << arg
+ when Array then new(arg.map { |a| Option.new(a.to_s) })
+ else raise TypeError, "Cannot convert #{arg.inspect} to Options"
+ end
+ end
end