diff options
Diffstat (limited to 'lib/slop/result.rb')
-rw-r--r-- | lib/slop/result.rb | 28 |
1 files changed, 21 insertions, 7 deletions
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 |