Class: Slop::Options

Inherits:
Array
  • Object
show all
Defined in:
lib/slop-2.3.1/lib/slop.rb

Overview

Used to hold a list of Option objects. This class inherits from Array and overwrites ‘Array#[]` so we can fetch Option objects via their short or long flags

Instance Method Summary collapse

Instance Method Details

#[](flag) ⇒ Option

Fetch an Option object. This method overrides Array#[] to provide a nicer interface for fetching options via their short or long flag. The reason we don’t use a Hash here is because an option cannot be identified by a single label. Instead this method tests against a short flag first, followed by a long flag. When passing this method an Integer, it will work as an Array usually would, fetching the Slop::Option at this index.

Examples:

opts = Slop.parse { on :v, "Verbose mode" }
opts.options[:v] #=> Option
opts.options[:v].description #=> "Verbose mode"

Parameters:

  • flag (Object)

    The short/long flag representing the option

Returns:

  • (Option)

    the option assoiated with this flag



257
258
259
260
261
262
263
264
265
# File 'lib/slop-2.3.1/lib/slop.rb', line 257

def [](flag)
  if flag.is_a? Integer
    super
  else
    find do |option|
      [option.short_flag, option.long_flag].include? flag.to_s
    end
  end
end