Class: Argy::Options
- Inherits:
-
Object
- Object
- Argy::Options
- Defined in:
- lib/argy/options.rb
Overview
The resulting options that were parsed from the command line.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a value by key.
-
#fetch(*args, &block) ⇒ Object
Fetch a value by key or provide a default.
-
#initialize(values) ⇒ Options
constructor
Create a new Options.
-
#to_h ⇒ Hash{Symbol => Object}
The values as a hash.
Constructor Details
#initialize(values) ⇒ Options
Create a new Options
12 13 14 |
# File 'lib/argy/options.rb', line 12 def initialize(values) @values = values end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object (private)
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/argy/options.rb', line 40 def method_missing(meth, *args) query = meth[-1] == "?" key = query ? meth[0..-2].to_sym : meth.to_sym return super unless @values.key?(key) unless args.empty? raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0)" end query ? !!@values[key] : @values[key] end |
Instance Method Details
#[](key) ⇒ Object
Get a value by key
24 25 26 |
# File 'lib/argy/options.rb', line 24 def [](key) @values[key] end |
#fetch(*args, &block) ⇒ Object
Fetch a value by key or provide a default.
30 31 32 |
# File 'lib/argy/options.rb', line 30 def fetch(*args, &block) @values.fetch(*args, &block) end |
#to_h ⇒ Hash{Symbol => Object}
The values as a hash
18 19 20 |
# File 'lib/argy/options.rb', line 18 def to_h @values end |