blob: 5d8567090e8d1b114506707ecf9c4be161776d34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
require 'slop/option'
require 'slop/options'
require 'slop/parser'
require 'slop/result'
require 'slop/types'
require 'slop/error'
module Slop
VERSION = '4.4.2'
# 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
|