From 1087df1016f65e3b1cef9a11597086e56674a369 Mon Sep 17 00:00:00 2001 From: Martin Afanasjew Date: Wed, 11 May 2016 05:08:09 +0200 Subject: [PATCH] ARGV: fix 'value' method, make it more predictable The fix changes behavior in same cases, but those cases were all either broken or showed unexpected behavior. The new behavior is very simple: - If an argument starts with `--=`, return whatever comes after the equals sign. Prior to this change, `ARGV.value` showed some unexpected behavior: - `ARGV.value("foo")` returned `nil` for `--foo=` because at least one character needed to be present after the equals sign. (All other option parser implementations I'm aware of allow for empty values.) - `ARGV.value("bar")` returned `"baz"` for `--foo=--bar=baz` because the regular expression was not anchored to the start of the argument. - `ARGV.value("++")` raised an exception because the string wasn't escaped for use in the regular expression. (An unlikely corner case.) Closes #231. Signed-off-by: Martin Afanasjew --- Library/Homebrew/extend/ARGV.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/extend/ARGV.rb b/Library/Homebrew/extend/ARGV.rb index bb26d453fb..8f783aea1a 100644 --- a/Library/Homebrew/extend/ARGV.rb +++ b/Library/Homebrew/extend/ARGV.rb @@ -87,9 +87,10 @@ module HomebrewArgvExtension at(@n+1) || raise(UsageError) end - def value(arg) - arg = find { |o| o =~ /--#{arg}=(.+)/ } - $1 if arg + def value(name) + arg_prefix = "--#{name}=" + flag_with_value = find { |arg| arg.start_with?(arg_prefix) } + flag_with_value.strip_prefix(arg_prefix) if flag_with_value end def force?