Module: Slop

Defined in:
lib/slop.rb,
lib/slop/error.rb,
lib/slop/types.rb,
lib/slop/option.rb,
lib/slop/parser.rb,
lib/slop/result.rb,
lib/slop/options.rb

Defined Under Namespace

Classes: ArrayOption, BoolOption, Error, FloatOption, IntegerOption, MissingArgument, NotImplementedError, NullOption, Option, Options, Parser, RegexpOption, Result, StringOption, UnknownOption

Constant Summary collapse

VERSION =
'4.2.1'
BooleanOption =
BoolOption
IntOption =
IntegerOption

Class Method Summary collapse

Class Method Details

.option_defined?(name) ⇒ Boolean

Example:

Slop.option_defined?(:string) #=> true
Slop.option_defined?(:omg)    #=> false

Returns true if an option is defined.

Returns:

  • (Boolean)


32
33
34
# File 'lib/slop.rb', line 32

def self.option_defined?(name)
  const_defined?(string_to_option(name.to_s))
end

.parse(items = ARGV, **config, &block) ⇒ Object

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.



22
23
24
# File 'lib/slop.rb', line 22

def self.parse(items = ARGV, **config, &block)
  Options.new(config, &block).parse(items)
end

.string_to_option(s) ⇒ Object

Example:

Slop.string_to_option("string")     #=> "StringOption"
Slop.string_to_option("some_thing") #=> "SomeThingOption"

Returns a camel-cased class looking string with Option suffix.



42
43
44
# File 'lib/slop.rb', line 42

def self.string_to_option(s)
  s.gsub(/(?:^|_)([a-z])/) { $1.capitalize } + "Option"
end

.string_to_option_class(s) ⇒ Object

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`.



52
53
54
# File 'lib/slop.rb', line 52

def self.string_to_option_class(s)
  const_get(string_to_option(s))
end