Class: Slop::Result
- Inherits:
-
Object
- Object
- Slop::Result
- Defined in:
- lib/slop/result.rb
Overview
This class encapsulates a Parser and Options pair. The idea is that the Options class shouldn’t have to deal with what happens when options are parsed, and the Parser shouldn’t have to deal with the state of options once parsing is complete. This keeps the API really simple; A Parser parses, Options handles options, and this class handles the result of those actions. This class contains the important most used methods.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#[](flag) ⇒ Object
(also: #get)
Returns an option’s value, nil if the option does not exist.
-
#[]=(flag, value) ⇒ Object
(also: #set)
Set the value for an option.
-
#arguments ⇒ Object
(also: #args)
Example:.
-
#fetch(flag) ⇒ Object
Returns an option’s value, raises UnknownOption if the option does not exist.
-
#initialize(parser) ⇒ Result
constructor
A new instance of Result.
- #method_missing(name, *args, &block) ⇒ Object
-
#option(flag) ⇒ Object
Returns an Option if it exists.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_hash ⇒ Object
(also: #to_h)
Returns a hash with option key => value.
- #to_s(**opts) ⇒ Object
-
#unused_options ⇒ Object
Returns an Array of Option instances that were not used.
-
#used_options ⇒ Object
Returns an Array of Option instances that were used.
Constructor Details
#initialize(parser) ⇒ Result
Returns a new instance of Result.
12 13 14 15 |
# File 'lib/slop/result.rb', line 12 def initialize(parser) @parser = parser @options = parser. end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/slop/result.rb', line 52 def method_missing(name, *args, &block) if respond_to_missing?(name) (o = option(name.to_s.chomp("?"))) && .include?(o) else super end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/slop/result.rb', line 10 def @options end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
10 11 12 |
# File 'lib/slop/result.rb', line 10 def parser @parser end |
Instance Method Details
#[](flag) ⇒ Object Also known as: get
Returns an option’s value, nil if the option does not exist.
18 19 20 |
# File 'lib/slop/result.rb', line 18 def [](flag) (o = option(flag)) && o.value end |
#[]=(flag, value) ⇒ Object Also known as: set
Set the value for an option. Raises an ArgumentError if the option does not exist.
36 37 38 39 40 41 42 |
# File 'lib/slop/result.rb', line 36 def []=(flag, value) if o = option(flag) o.value = value else raise ArgumentError, "no option with flag `#{flag}'" end end |
#arguments ⇒ Object Also known as: args
85 86 87 |
# File 'lib/slop/result.rb', line 85 def arguments parser.arguments end |
#fetch(flag) ⇒ Object
Returns an option’s value, raises UnknownOption if the option does not exist.
24 25 26 27 28 29 30 31 32 |
# File 'lib/slop/result.rb', line 24 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 |
#option(flag) ⇒ Object
Returns an Option if it exists. Ignores any prefixed hyphens.
46 47 48 49 50 |
# File 'lib/slop/result.rb', line 46 def option(flag) .find do |o| o.flags.any? { |f| clean_key(f) == clean_key(flag) } end end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
60 61 62 |
# File 'lib/slop/result.rb', line 60 def respond_to_missing?(name, include_private = false) name.to_s.end_with?("?") || super end |
#to_hash ⇒ Object Also known as: to_h
Returns a hash with option key => value.
91 92 93 |
# File 'lib/slop/result.rb', line 91 def to_hash Hash[.reject(&:null?).map { |o| [o.key, o.value] }] end |
#to_s(**opts) ⇒ Object
96 97 98 |
# File 'lib/slop/result.rb', line 96 def to_s(**opts) .to_s(**opts) end |
#unused_options ⇒ Object
Returns an Array of Option instances that were not used.
70 71 72 |
# File 'lib/slop/result.rb', line 70 def parser. end |
#used_options ⇒ Object
Returns an Array of Option instances that were used.
65 66 67 |
# File 'lib/slop/result.rb', line 65 def parser. end |