| 1 |
module Slop |
| 2 |
class Options |
| 3 |
include Enumerable |
| 4 |
|
| 5 |
DEFAULT_CONFIG = { |
| 6 |
suppress_errors: false, |
| 7 |
type: "null", |
| 8 |
banner: true, |
| 9 |
underscore_flags: true, |
| 10 |
validate_types: false, |
| 11 |
} |
| 12 |
|
| 13 |
# The Array of Option instances we've created. |
| 14 |
attr_reader :options |
| 15 |
|
| 16 |
# An Array of separators used for the help text. |
| 17 |
attr_reader :separators |
| 18 |
|
| 19 |
# Our Parser instance. |
| 20 |
attr_reader :parser |
| 21 |
|
| 22 |
# A Hash of configuration options. |
| 23 |
attr_reader :config |
| 24 |
|
| 25 |
# The String banner prefixed to the help string. |
| 26 |
attr_accessor :banner |
| 27 |
|
| 28 |
# Whether we should validate types of values provided by the user |
| 29 |
attr_accessor :validate_types |
| 30 |
|
| 31 |
def initialize(**config, &block) |
| 32 |
@options = [] |
| 33 |
@separators = [] |
| 34 |
@banner = config[:banner].is_a?(String) ? config[:banner] : config.fetch(:banner, "usage: #{$0} [options]") |
| 35 |
@config = DEFAULT_CONFIG.merge(config) |
| 36 |
@parser = Parser.new(self, **@config) |
| 37 |
|
| 38 |
yield self if block_given? |
| 39 |
end |
| 40 |
|
| 41 |
# Add a new option. This method is an alias for adding a NullOption |
| 42 |
# (i.e an option with an ignored return value). |
| 43 |
# |
| 44 |
# Example: |
| 45 |
# |
| 46 |
# opts = Slop.parse do |o| |
| 47 |
# o.on '--version' do |
| 48 |
# puts Slop::VERSION |
| 49 |
# end |
| 50 |
# end |
| 51 |
# |
| 52 |
# opts.to_hash #=> {} |
| 53 |
# |
| 54 |
# Returns the newly created Option subclass. |
| 55 |
def on(*flags, **config, &block) |
| 56 |
desc = flags.pop unless flags.last.start_with?('-') |
| 57 |
config = self.config.merge(config) |
| 58 |
klass = Slop.string_to_option_class(config[:type].to_s) |
| 59 |
option = klass.new(flags, desc, **config, &block) |
| 60 |
|
| 61 |
add_option option |
| 62 |
end |
| 63 |
|
| 64 |
# Add a separator between options. Used when displaying |
| 65 |
# the help text. |
| 66 |
def separator(string = "") |
| 67 |
if separators[options.size] |
| 68 |
separators[-1] += "\n#{string}" |
| 69 |
else |
| 70 |
separators[options.size] = string |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
# Sugar to avoid `options.parser.parse(x)`. |
| 75 |
def parse(strings) |
| 76 |
parser.parse(strings) |
| 77 |
end |
| 78 |
|
| 79 |
# Implements the Enumerable interface. |
| 80 |
def each(&block) |
| 81 |
options.each(&block) |
| 82 |
end |
| 83 |
|
| 84 |
# Handle custom option types. Will fall back to raising an |
| 85 |
# exception if an option is not defined. |
| 86 |
def method_missing(name, *args, **config, &block) |
| 87 |
if respond_to_missing?(name) |
| 88 |
config[:type] = name |
| 89 |
on(*args, **config, &block) |
| 90 |
else |
| 91 |
super |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
def respond_to_missing?(name, include_private = false) |
| 96 |
Slop.option_defined?(name) || super |
| 97 |
end |
| 98 |
|
| 99 |
# Return a copy of our options Array. |
| 100 |
def to_a |
| 101 |
options.dup |
| 102 |
end |
| 103 |
|
| 104 |
# Returns the help text for this options. Used by Result#to_s. |
| 105 |
def to_s(prefix: " " * 4) |
| 106 |
str = config[:banner] ? "#{banner}\n" : "" |
| 107 |
len = longest_flag_length |
| 108 |
|
| 109 |
options.select.each_with_index.sort_by{ |o,i| [o.tail, i] }.each do |opt, i| |
| 110 |
# use the index to fetch an associated separator |
| 111 |
if sep = separators[i] |
| 112 |
str += "#{sep}\n" |
| 113 |
end |
| 114 |
|
| 115 |
str += "#{prefix}#{opt.to_s(offset: len)}\n" if opt.help? |
| 116 |
end |
| 117 |
|
| 118 |
if sep = separators[options.size] |
| 119 |
str += "#{sep}\n" |
| 120 |
end |
| 121 |
|
| 122 |
str |
| 123 |
end |
| 124 |
|
| 125 |
private |
| 126 |
|
| 127 |
def longest_flag_length |
| 128 |
(o = longest_option) && o.flag.length || 0 |
| 129 |
end |
| 130 |
|
| 131 |
def longest_option |
| 132 |
options.max { |a, b| a.flag.length <=> b.flag.length } |
| 133 |
end |
| 134 |
|
| 135 |
def add_option(option) |
| 136 |
options.each do |o| |
| 137 |
flags = o.flags & option.flags |
| 138 |
|
| 139 |
# Raise an error if we found an existing option with the same |
| 140 |
# flags. I can't immediately see a use case for this.. |
| 141 |
if flags.any? |
| 142 |
raise ArgumentError, "duplicate flags: #{flags}" |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
options << option |
| 147 |
option |
| 148 |
end |
| 149 |
end |
| 150 |
end |