Debian Codegraph @

vendor/mruby/mrbgems/mruby-slop/mrblib/99_slop.rb

raw
1
2 module Slop
3 VERSION = '4.10.1'
4
5 # Parse an array of options (defaults to ARGV). Accepts an
6 # optional hash of configuration options and block.
7 #
8 # Example:
9 #
10 # opts = Slop.parse(["-host", "localhost"]) do |o|
11 # o.string '-host', 'a hostname', default: '0.0.0.0'
12 # end
13 # opts.to_hash #=> { host: 'localhost' }
14 #
15 # Returns a Slop::Result.
16 def self.parse(items = ARGV, **config, &block)
17 Options.new(**config, &block).parse(items)
18 end
19
20 # Example:
21 #
22 # Slop.option_defined?(:string) #=> true
23 # Slop.option_defined?(:omg) #=> false
24 #
25 # Returns true if an option is defined.
26 def self.option_defined?(name)
27 const_defined?(string_to_option(name.to_s))
28 rescue NameError
29 # If a NameError is raised, it wasn't a valid constant name,
30 # and thus couldn't have been defined.
31 false
32 end
33
34 # Example:
35 #
36 # Slop.string_to_option("string") #=> "StringOption"
37 # Slop.string_to_option("some_thing") #=> "SomeThingOption"
38 #
39 # Returns a camel-cased class looking string with Option suffix.
40 def self.string_to_option(s)
41 s.gsub(/(?:^|_)([a-z])/) { $1.capitalize } + "Option"
42 end
43
44 # Example:
45 #
46 # Slop.string_to_option_class("string") #=> Slop::StringOption
47 # Slop.string_to_option_class("foo") #=> uninitialized constant FooOption
48 #
49 # Returns the full qualified option class. Uses `#string_to_option`.
50 def self.string_to_option_class(s)
51 const_get(string_to_option(s))
52 end
53 end