Class: Slop::Result

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


46
47
48
49
50
51
52
# File 'lib/slop/result.rb', line 46

def method_missing(name, *args, &block)
  if respond_to_missing?(name)
    (o = option(name.to_s.chomp("?"))) && used_options.include?(o)
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


10
11
12
# File 'lib/slop/result.rb', line 10

def options
  @options
end

#parserObject (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 options 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.


25
26
27
28
29
30
31
# File 'lib/slop/result.rb', line 25

def []=(flag, value)
  if o = option(flag)
    o.value = value
  else
    raise ArgumentError, "no option with flag `#{flag}'"
  end
end

#argumentsObject Also known as: args

Example:

opts = Slop.parse do |o|
  o.string '--host'
  o.int '-p'
end

# ruby run.rb connect --host 123 helo
opts.arguments #=> ["connect", "helo"]

Returns an Array of String arguments that were not parsed.


79
80
81
# File 'lib/slop/result.rb', line 79

def arguments
  parser.arguments
end

#option(flag) ⇒ Object

Returns an Option if it exists. Ignores any prefixed hyphens.


35
36
37
38
39
40
41
42
43
44
# File 'lib/slop/result.rb', line 35

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) }
  end
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/slop/result.rb', line 54

def respond_to_missing?(name, include_private = false)
  name.to_s.end_with?("?") || super
end

#to_hashObject Also known as: to_h

Returns a hash with option key => value.


85
86
87
# File 'lib/slop/result.rb', line 85

def to_hash
  Hash[options.reject(&:null?).map { |o| [o.key, o.value] }]
end

#to_s(**opts) ⇒ Object


90
91
92
# File 'lib/slop/result.rb', line 90

def to_s(**opts)
  options.to_s(**opts)
end

#unused_optionsObject

Returns an Array of Option instances that were not used.


64
65
66
# File 'lib/slop/result.rb', line 64

def unused_options
  parser.unused_options
end

#used_optionsObject

Returns an Array of Option instances that were used.


59
60
61
# File 'lib/slop/result.rb', line 59

def used_options
  parser.used_options
end