Sha256: 82f56655024f3212e5b8c623d5214732ab1bdba4620538957dd4e62542fd0743

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'slop/option'
require 'slop/options'
require 'slop/parser'
require 'slop/result'
require 'slop/types'
require 'slop/error'

module Slop
  VERSION = '4.9.1'

  # Parse an array of options (defaults to ARGV). Accepts an
  # optional hash of configuration options and block.
  #
  # Example:
  #
  #   opts = Slop.parse(["-host", "localhost"]) do |o|
  #     o.string '-host', 'a hostname', default: '0.0.0.0'
  #   end
  #   opts.to_hash #=> { host: 'localhost' }
  #
  # Returns a Slop::Result.
  def self.parse(items = ARGV, **config, &block)
    Options.new(**config, &block).parse(items)
  end

  # Example:
  #
  #   Slop.option_defined?(:string) #=> true
  #   Slop.option_defined?(:omg)    #=> false
  #
  # Returns true if an option is defined.
  def self.option_defined?(name)
    const_defined?(string_to_option(name.to_s))
  rescue NameError
    # If a NameError is raised, it wasn't a valid constant name,
    # and thus couldn't have been defined.
    false
  end

  # Example:
  #
  #   Slop.string_to_option("string")     #=> "StringOption"
  #   Slop.string_to_option("some_thing") #=> "SomeThingOption"
  #
  # Returns a camel-cased class looking string with Option suffix.
  def self.string_to_option(s)
    s.gsub(/(?:^|_)([a-z])/) { $1.capitalize } + "Option"
  end

  # Example:
  #
  #   Slop.string_to_option_class("string") #=> Slop::StringOption
  #   Slop.string_to_option_class("foo")    #=> uninitialized constant FooOption
  #
  # Returns the full qualified option class. Uses `#string_to_option`.
  def self.string_to_option_class(s)
    const_get(string_to_option(s))
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slop-4.9.1 lib/slop.rb