Class: Slop::Options

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

Instance Method Summary collapse

Instance Method Details

#[](flag) ⇒ Option

Fetch an Option object

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/slop/options.rb', line 23

def [](flag)
  if flag.is_a?(Integer)
    slice flag
  else
    item = flag.to_s
    find do |option|
      option.short_flag == item || option.long_flag == item
    end
  end
end

#to_hash(symbols) ⇒ Hash

Parameters:

  • symbols (Boolean)

    true to cast hash keys to symbols

Returns:

  • (Hash)


6
7
8
9
10
11
12
13
# File 'lib/slop/options.rb', line 6

def to_hash(symbols)
  reduce({}) do |hsh, option|
    key = option.key
    key = key.to_sym if symbols
    hsh[key] = option.argument_value
    hsh
  end
end

#to_helpString

Returns All options in a pretty help string.

Returns:

  • (String)

    All options in a pretty help string

See Also:



36
37
38
39
40
41
# File 'lib/slop/options.rb', line 36

def to_help
  heads = reject(&:tail)
  tails = select(&:tail)
  all = (heads + tails).select(&:help)
  all.map(&:to_s).join("\n")
end