summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiovanni Benussi <giovanni.benussi@usach.cl>2018-06-19 09:51:21 -0400
committerGiovanni Benussi <giovanni.benussi@usach.cl>2018-06-27 11:18:38 -0400
commit0b6d52e1f2f045f9678ac929c1c942cc6f356f45 (patch)
tree44ef04d2dcb962dda2014f670e8bc044767a0bc2
parent50ac7fb6be8d2e32ea3b62c801201a9b570c995b (diff)
downloadslop-0b6d52e1f2f045f9678ac929c1c942cc6f356f45.tar.gz
add Slop::Result#fetch
use Slop::Result#clean_key in Slop::Result#option fix typo in Slop::Result#[] and Slop::Result#fetch descriptions handle case when Slop::Result#fetch tries to fetch an option that is not provided and does not have a default value raise Slop::UnknownOption when Slop::Result#fetch tries to fetch an unexisting key set Slop::Result#clean_key method as private remove redundant Slop::Result#fetch tests update description of Slop::Result#fetch test when trying to access an unexisting option update error message when an option is not present on Slop::Result#fetch description of Slop::Result#fetch update expected error message on test for Slop::Result#fetch when an option is not present
-rw-r--r--lib/slop/error.rb5
-rw-r--r--lib/slop/result.rb28
-rw-r--r--test/result_test.rb25
3 files changed, 49 insertions, 9 deletions
diff --git a/lib/slop/error.rb b/lib/slop/error.rb
index e024645..ddfa1a7 100644
--- a/lib/slop/error.rb
+++ b/lib/slop/error.rb
@@ -22,8 +22,9 @@ module Slop
end
end
- # Raised when an unknown option is parsed. Suppress
- # with the `suppress_errors` config option.
+ # Raised when an unknown option is parsed or when trying to fetch an
+ # unexisting option via `Slop::Result#fetch`.
+ # Suppress with the `suppress_errors` config option.
class UnknownOption < Error
attr_reader :flag
diff --git a/lib/slop/result.rb b/lib/slop/result.rb
index 469d05c..0be3c11 100644
--- a/lib/slop/result.rb
+++ b/lib/slop/result.rb
@@ -14,12 +14,23 @@ module Slop
@options = parser.options
end
- # Returns an options value, nil if the option does not exist.
+ # Returns an option's value, nil if the option does not exist.
def [](flag)
(o = option(flag)) && o.value
end
alias get []
+ # Returns an option's value, raises UnknownOption if the option does not exist.
+ def fetch(flag)
+ o = option(flag)
+ if o.nil?
+ cleaned_key = clean_key(flag)
+ raise UnknownOption.new("option not found: '#{cleaned_key}'", "#{cleaned_key}")
+ else
+ o.value
+ end
+ end
+
# Set the value for an option. Raises an ArgumentError if the option
# does not exist.
def []=(flag, value)
@@ -33,13 +44,8 @@ module Slop
# Returns an Option if it exists. Ignores any prefixed hyphens.
def option(flag)
- cleaned = -> (f) do
- key = f.to_s.sub(/\A--?/, '')
- key = key.tr '-', '_' if parser.config[:underscore_flags]
- key.to_sym
- end
options.find do |o|
- o.flags.any? { |f| cleaned.(f) == cleaned.(flag) }
+ o.flags.any? { |f| clean_key(f) == clean_key(flag) }
end
end
@@ -90,5 +96,13 @@ module Slop
def to_s(**opts)
options.to_s(**opts)
end
+
+ private
+
+ def clean_key(key)
+ key = key.to_s.sub(/\A--?/, '')
+ key = key.tr '-', '_' if parser.config[:underscore_flags]
+ key.to_sym
+ end
end
end
diff --git a/test/result_test.rb b/test/result_test.rb
index 322daa1..30e4934 100644
--- a/test/result_test.rb
+++ b/test/result_test.rb
@@ -62,6 +62,31 @@ describe Slop::Result do
end
end
+ describe "#fetch" do
+ it "returns an options value" do
+ assert_equal "lee", @result.fetch("--name")
+ end
+
+ it "raises Slop::UnknownOption when an option does not exists" do
+ e = assert_raises(Slop::UnknownOption) { @result.fetch("--unexisting") }
+ assert_equal "option not found: 'unexisting'", e.message
+ end
+
+ it "returns the default value of an option when a value is not provided" do
+ @options.string("--foo", default: "bar")
+ @result.parser.parse %w(--foo)
+
+ assert_equal 'bar', @result.fetch('foo')
+ end
+
+ it "returns nil when an option is not provided and it does not have a default value" do
+ @options.string("--hello")
+ @result.parser.parse %w()
+
+ assert_equal nil, @result.fetch('hello')
+ end
+ end
+
describe "#[]=" do
it "sets an options value" do
assert_equal "lee", @result["name"]