Class: Slop::Options
Constant Summary collapse
- DEFAULT_CONFIG =
{ suppress_errors: false, type: "null", banner: true, underscore_flags: true, validate_types: false, }
Instance Attribute Summary collapse
-
#banner ⇒ Object
The String banner prefixed to the help string.
-
#config ⇒ Object
readonly
A Hash of configuration options.
-
#options ⇒ Object
readonly
The Array of Option instances we’ve created.
-
#parser ⇒ Object
readonly
Our Parser instance.
-
#separators ⇒ Object
readonly
An Array of separators used for the help text.
-
#validate_types ⇒ Object
Whether we should validate types of values provided by the user.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Implements the Enumerable interface.
-
#initialize(**config) {|_self| ... } ⇒ Options
constructor
A new instance of Options.
-
#method_missing(name, *args, **config, &block) ⇒ Object
Handle custom option types.
-
#on(*flags, **config, &block) ⇒ Object
Add a new option.
-
#parse(strings) ⇒ Object
Sugar to avoid ‘options.parser.parse(x)`.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#separator(string = "") ⇒ Object
Add a separator between options.
-
#to_a ⇒ Object
Return a copy of our options Array.
-
#to_s(prefix: " " * 4) ⇒ Object
Returns the help text for this options.
Constructor Details
#initialize(**config) {|_self| ... } ⇒ Options
Returns a new instance of Options.
31 32 33 34 35 36 37 38 39 |
# File 'lib/slop/options.rb', line 31 def initialize(**config, &block) @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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **config, &block) ⇒ Object
Handle custom option types. Will fall back to raising an exception if an option is not defined.
86 87 88 89 90 91 92 93 |
# File 'lib/slop/options.rb', line 86 def method_missing(name, *args, **config, &block) if respond_to_missing?(name) config[:type] = name on(*args, **config, &block) else super end end |
Instance Attribute Details
#banner ⇒ Object
The String banner prefixed to the help string.
26 27 28 |
# File 'lib/slop/options.rb', line 26 def @banner end |
#config ⇒ Object (readonly)
A Hash of configuration options.
23 24 25 |
# File 'lib/slop/options.rb', line 23 def config @config end |
#options ⇒ Object (readonly)
The Array of Option instances we’ve created.
14 15 16 |
# File 'lib/slop/options.rb', line 14 def @options end |
#parser ⇒ Object (readonly)
Our Parser instance.
20 21 22 |
# File 'lib/slop/options.rb', line 20 def parser @parser end |
#separators ⇒ Object (readonly)
An Array of separators used for the help text.
17 18 19 |
# File 'lib/slop/options.rb', line 17 def separators @separators end |
#validate_types ⇒ Object
Whether we should validate types of values provided by the user
29 30 31 |
# File 'lib/slop/options.rb', line 29 def validate_types @validate_types end |
Instance Method Details
#each(&block) ⇒ Object
Implements the Enumerable interface.
80 81 82 |
# File 'lib/slop/options.rb', line 80 def each(&block) .each(&block) end |
#on(*flags, **config, &block) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/slop/options.rb', line 55 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) ⇒ Object
Sugar to avoid ‘options.parser.parse(x)`.
75 76 77 |
# File 'lib/slop/options.rb', line 75 def parse(strings) parser.parse(strings) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
95 96 97 |
# File 'lib/slop/options.rb', line 95 def respond_to_missing?(name, include_private = false) Slop.option_defined?(name) || super end |
#separator(string = "") ⇒ Object
Add a separator between options. Used when displaying the help text.
66 67 68 69 70 71 72 |
# File 'lib/slop/options.rb', line 66 def separator(string = "") if separators[.size] separators[-1] += "\n#{string}" else separators[.size] = string end end |
#to_a ⇒ Object
Return a copy of our options Array.
100 101 102 |
# File 'lib/slop/options.rb', line 100 def to_a .dup end |
#to_s(prefix: " " * 4) ⇒ Object
Returns the help text for this options. Used by Result#to_s.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/slop/options.rb', line 105 def to_s(prefix: " " * 4) str = config[:banner] ? "#{}\n" : "" len = longest_flag_length .select.each_with_index.sort_by{ |o,i| [o.tail, i] }.each 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" if opt.help? end if sep = separators[.size] str += "#{sep}\n" end str end |