aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMax Howell2012-03-06 20:11:35 +0000
committerMax Howell2012-03-06 20:28:06 +0000
commitb63ec18564017534822d749e7e712cd0d45b8ef6 (patch)
treeaf36d131ca435786ba1205763e87bc54e9197302 /Library
parent9c5ddb5721ec0a676ac3445214c6495aa4e53364 (diff)
downloadbrew-b63ec18564017534822d749e7e712cd0d45b8ef6.tar.bz2
Proper single character switch handling
Includes a test. So now you can do `brew cleanup -ns` and it will work.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/extend/ARGV.rb10
-rw-r--r--Library/Homebrew/test/test_ARGV.rb17
2 files changed, 26 insertions, 1 deletions
diff --git a/Library/Homebrew/extend/ARGV.rb b/Library/Homebrew/extend/ARGV.rb
index 24d70cb1f..130058327 100644
--- a/Library/Homebrew/extend/ARGV.rb
+++ b/Library/Homebrew/extend/ARGV.rb
@@ -103,6 +103,16 @@ module HomebrewArgvExtension
return false
end
+ # eg. `foo -ns -i --bar` has three switches, n, s and i
+ def switch? switch_character
+ return false if switch_character.length > 1
+ options_only.each do |arg|
+ next if arg[1..1] == '-'
+ return true if arg.include? switch_character
+ end
+ return false
+ end
+
def usage
require 'cmd/help'
Homebrew.help_s
diff --git a/Library/Homebrew/test/test_ARGV.rb b/Library/Homebrew/test/test_ARGV.rb
index 134921dca..7832f71e4 100644
--- a/Library/Homebrew/test/test_ARGV.rb
+++ b/Library/Homebrew/test/test_ARGV.rb
@@ -29,5 +29,20 @@ class ARGVTests < Test::Unit::TestCase
assert_equal 1, ARGV.kegs.length
assert_raises(FormulaUnavailableError) { ARGV.formulae }
end
-
+
+ def test_switch?
+ ARGV.reset
+ ARGV.unshift "-ns"
+ ARGV.unshift "-i"
+ ARGV.unshift "--bar"
+ assert ARGV.switch?('n')
+ assert ARGV.switch?('s')
+ assert ARGV.switch?('i')
+ assert !ARGV.switch?('b')
+ assert !ARGV.switch?('ns')
+ assert !ARGV.switch?('bar')
+ assert !ARGV.switch?('--bar')
+ assert !ARGV.switch?('-n')
+ end
+
end