class Slop::Result

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.

Attributes

options[R]
parser[R]

Public Class Methods

new(parser) click to toggle source
# File lib/slop/result.rb, line 12
def initialize(parser)
  @parser  = parser
  @options = parser.options
end

Public Instance Methods

[](flag) click to toggle source

Returns an option's value, nil if the option does not exist.

# File lib/slop/result.rb, line 18
def [](flag)
  (o = option(flag)) && o.value
end
Also aliased as: get
[]=(flag, value) click to toggle source

Set the value for an option. Raises an ArgumentError if the option does not exist.

# 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
Also aliased as: set
args()
Alias for: arguments
arguments() click to toggle source

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.

# File lib/slop/result.rb, line 85
def arguments
  parser.arguments
end
Also aliased as: args
fetch(flag) click to toggle source

Returns an option's value, raises UnknownOption if the option does not exist.

# 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
get(flag)
Alias for: []
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/slop/result.rb, line 52
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
option(flag) click to toggle source

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

# File lib/slop/result.rb, line 46
def option(flag)
  options.find do |o|
    o.flags.any? { |f| clean_key(f) == clean_key(flag) }
  end
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/slop/result.rb, line 60
def respond_to_missing?(name, include_private = false)
  name.to_s.end_with?("?") || super
end
set(flag, value)
Alias for: []=
to_h()
Alias for: to_hash
to_hash() click to toggle source

Returns a hash with option key => value.

# File lib/slop/result.rb, line 91
def to_hash
  Hash[options.reject(&:null?).map { |o| [o.key, o.value] }]
end
Also aliased as: to_h
to_s(**opts) click to toggle source
# File lib/slop/result.rb, line 96
def to_s(**opts)
  options.to_s(**opts)
end
unused_options() click to toggle source

Returns an Array of Option instances that were not used.

# File lib/slop/result.rb, line 70
def unused_options
  parser.unused_options
end
used_options() click to toggle source

Returns an Array of Option instances that were used.

# File lib/slop/result.rb, line 65
def used_options
  parser.used_options
end

Private Instance Methods

clean_key(key) click to toggle source
# File lib/slop/result.rb, line 102
def clean_key(key)
  key = key.to_s.sub(/\A--?/, '')
  key = key.tr '-', '_' if parser.config[:underscore_flags]
  key.to_sym
end