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 `--<option-name>=`, 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 <martin@afanasjew.de>
This commit is contained in:
Martin Afanasjew 2016-05-11 05:08:09 +02:00
parent 2e961dc9de
commit 1087df1016

View File

@ -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?