Class: Argy::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/argy/options.rb

Overview

The resulting options that were parsed from the command line.

Examples:

Getting a value

options = Options.new(foo: "bar")
options.foo #=> "bar"

Querying for a value's truthiness

options = Options.new(foo: "bar")
options.foo? #=> true

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Options

Create a new Options

Parameters:

  • values (Hash{Symbol => Object})


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

See Also:

  • Hash#[]


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.

See Also:

  • Hash#fetch


30
31
32
# File 'lib/argy/options.rb', line 30

def fetch(*args, &block)
  @values.fetch(*args, &block)
end

#to_hHash{Symbol => Object}

The values as a hash

Returns:

  • (Hash{Symbol => Object})


18
19
20
# File 'lib/argy/options.rb', line 18

def to_h
  @values
end