class Slop::Options

Constants

DEFAULT_CONFIG

Attributes

banner[RW]

The String banner prefixed to the help string.

config[R]

A Hash of configuration options.

options[R]

The Array of Option instances we've created.

parser[R]

Our Parser instance.

separators[R]

An Array of separators used for the help text.

Public Class Methods

new(**config) { |self| ... } click to toggle source
# File lib/slop/options.rb, line 26
def initialize(**config)
  @options    = []
  @separators = []
  @banner     = config[:banner].is_a?(String) ? config[:banner] : config.fetch(:banner, "usage: #{$0} [options]")
  @config     = DEFAULT_CONFIG.merge(config)
  @parser     = Parser.new(self, @config)

  yield self if block_given?
end

Public Instance Methods

each(&block) click to toggle source

Implements the Enumerable interface.

# File lib/slop/options.rb, line 75
def each(&block)
  options.each(&block)
end
method_missing(name, *args, **config, &block) click to toggle source

Handle custom option types. Will fall back to raising an exception if an option is not defined.

Calls superclass method
# File lib/slop/options.rb, line 81
def method_missing(name, *args, **config, &block)
  if respond_to_missing?(name)
    config[:type] = name
    on(*args, config, &block)
  else
    super
  end
end
on(*flags, **config, &block) click to toggle source

Add a new option. This method is an alias for adding a NullOption (i.e an option with an ignored return value).

Example:

opts = Slop.parse do |o|
  o.on '--version' do
    puts Slop::VERSION
  end
end

opts.to_hash #=> {}

Returns the newly created Option subclass.

# File lib/slop/options.rb, line 50
def on(*flags, **config, &block)
  desc   = flags.pop unless flags.last.start_with?('-')
  config = self.config.merge(config)
  klass  = Slop.string_to_option_class(config[:type].to_s)
  option = klass.new(flags, desc, config, &block)

  add_option option
end
parse(strings) click to toggle source

Sugar to avoid `options.parser.parse(x)`.

# File lib/slop/options.rb, line 70
def parse(strings)
  parser.parse(strings)
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/slop/options.rb, line 90
def respond_to_missing?(name, include_private = false)
  Slop.option_defined?(name) || super
end
separator(string) click to toggle source

Add a separator between options. Used when displaying the help text.

# File lib/slop/options.rb, line 61
def separator(string)
  if separators[options.size]
    separators.last << "\n#{string}"
  else
    separators[options.size] = string
  end
end
to_a() click to toggle source

Return a copy of our options Array.

# File lib/slop/options.rb, line 95
def to_a
  options.dup
end
to_s(prefix: " " * 4) click to toggle source

Returns the help text for this options. Used by Slop::Result#to_s.

# File lib/slop/options.rb, line 100
def to_s(prefix: " " * 4)
  str = config[:banner] ? "#{banner}\n" : ""
  len = longest_flag_length

  options.select(&:help?).sort_by(&:tail).each_with_index do |opt, i|
    # use the index to fetch an associated separator
    if sep = separators[i]
      str << "#{sep}\n"
    end

    str << "#{prefix}#{opt.to_s(offset: len)}\n"
  end

  str
end

Private Instance Methods

add_option(option) click to toggle source
# File lib/slop/options.rb, line 126
def add_option(option)
  options.each do |o|
    flags = o.flags & option.flags

    # Raise an error if we found an existing option with the same
    # flags. I can't immediately see a use case for this..
    if flags.any?
      raise ArgumentError, "duplicate flags: #{flags}"
    end
  end

  options << option
  option
end
longest_flag_length() click to toggle source
# File lib/slop/options.rb, line 118
def longest_flag_length
  (o = longest_option) && o.flag.length || 0
end
longest_option() click to toggle source
# File lib/slop/options.rb, line 122
def longest_option
  options.max { |a, b| a.flag.length <=> b.flag.length }
end