diff options
author | Andrew Clemons <andrew.clemons@gmail.com> | 2016-04-04 15:10:25 +1200 |
---|---|---|
committer | Andrew Clemons <andrew.clemons@gmail.com> | 2016-04-04 15:21:45 +1200 |
commit | 7f6aef195716989d8fb7e66b1ca52e348551b67a (patch) | |
tree | 9adb41989b1ef1f982a0a0c2390648dbc215e392 /lib | |
parent | c77f3c3d0ce9fcda28f3df3a789c166c622d16ee (diff) | |
download | slop-7f6aef195716989d8fb7e66b1ca52e348551b67a.tar.gz |
Support arguments to options with leading -
This allows negative values as arguments to numeric options and strings
starting with - for any other type.
Fixes #170
Fixes #179
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/parser.rb | 10 | ||||
-rw-r--r-- | lib/slop/types.rb | 4 |
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/slop/parser.rb b/lib/slop/parser.rb index ea1869e..0b33d55 100644 --- a/lib/slop/parser.rb +++ b/lib/slop/parser.rb @@ -43,7 +43,8 @@ module Slop @arguments = strings.dup - pairs.each do |flag, arg| + pairs.each_with_index do |pair, idx| + flag, arg = pair break if !flag # ignore everything after '--', flag or not @@ -54,6 +55,7 @@ module Slop # support `foo=bar` orig_flag = flag.dup + orig_arg = arg if flag.include?("=") flag, arg = flag.split("=") end @@ -63,6 +65,12 @@ module Slop # arguments (plus the arg if necessary) # delete argument first while we can find its index. if opt.expects_argument? + + # if we consumed the argument, remove the next pair + if orig_arg == opt.value.to_s + pairs.delete_at(idx + 1) + end + arguments.each_with_index do |argument, i| if argument == orig_flag && !orig_flag.include?("=") arguments.delete_at(i + 1) diff --git a/lib/slop/types.rb b/lib/slop/types.rb index 7e80544..b7e41c8 100644 --- a/lib/slop/types.rb +++ b/lib/slop/types.rb @@ -44,7 +44,7 @@ module Slop # Cast the option argument to an Integer. class IntegerOption < Option def call(value) - value =~ /\A\d+\z/ && value.to_i + value =~ /\A-?\d+\z/ && value.to_i end end IntOption = IntegerOption @@ -53,7 +53,7 @@ module Slop class FloatOption < Option def call(value) # TODO: scientific notation, etc. - value =~ /\A\d*\.*\d+\z/ && value.to_f + value =~ /\A-?\d*\.*\d+\z/ && value.to_f end end |