| 1 |
module Slop |
| 2 |
# This class encapsulates a Parser and Options pair. The idea is that |
| 3 |
# the Options class shouldn't have to deal with what happens when options |
| 4 |
# are parsed, and the Parser shouldn't have to deal with the state of |
| 5 |
# options once parsing is complete. This keeps the API really simple; A |
| 6 |
# Parser parses, Options handles options, and this class handles the |
| 7 |
# result of those actions. This class contains the important most used |
| 8 |
# methods. |
| 9 |
class Result |
| 10 |
attr_reader :parser, :options |
| 11 |
|
| 12 |
def initialize(parser) |
| 13 |
@parser = parser |
| 14 |
@options = parser.options |
| 15 |
end |
| 16 |
|
| 17 |
# Returns an option's value, nil if the option does not exist. |
| 18 |
def [](flag) |
| 19 |
(o = option(flag)) && o.value |
| 20 |
end |
| 21 |
alias get [] |
| 22 |
|
| 23 |
# Returns an option's value, raises UnknownOption if the option does not exist. |
| 24 |
def fetch(flag) |
| 25 |
o = option(flag) |
| 26 |
if o.nil? |
| 27 |
cleaned_key = clean_key(flag) |
| 28 |
raise UnknownOption.new("option not found: '#{cleaned_key}'", "#{cleaned_key}") |
| 29 |
else |
| 30 |
o.value |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
# Set the value for an option. Raises an ArgumentError if the option |
| 35 |
# does not exist. |
| 36 |
def []=(flag, value) |
| 37 |
if o = option(flag) |
| 38 |
o.value = value |
| 39 |
else |
| 40 |
raise ArgumentError, "no option with flag `#{flag}'" |
| 41 |
end |
| 42 |
end |
| 43 |
alias set []= |
| 44 |
|
| 45 |
# Returns an Option if it exists. Ignores any prefixed hyphens. |
| 46 |
def option(flag) |
| 47 |
options.find do |o| |
| 48 |
o.flags.any? { |f| clean_key(f) == clean_key(flag) } |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
def method_missing(name, *args, &block) |
| 53 |
if respond_to_missing?(name) |
| 54 |
(o = option(name.to_s.chomp("?"))) && used_options.include?(o) |
| 55 |
else |
| 56 |
super |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
def respond_to_missing?(name, include_private = false) |
| 61 |
name.to_s.end_with?("?") || super |
| 62 |
end |
| 63 |
|
| 64 |
# Returns an Array of Option instances that were used. |
| 65 |
def used_options |
| 66 |
parser.used_options |
| 67 |
end |
| 68 |
|
| 69 |
# Returns an Array of Option instances that were not used. |
| 70 |
def unused_options |
| 71 |
parser.unused_options |
| 72 |
end |
| 73 |
|
| 74 |
# Example: |
| 75 |
# |
| 76 |
# opts = Slop.parse do |o| |
| 77 |
# o.string '--host' |
| 78 |
# o.int '-p' |
| 79 |
# end |
| 80 |
# |
| 81 |
# # ruby run.rb connect --host 123 helo |
| 82 |
# opts.arguments #=> ["connect", "helo"] |
| 83 |
# |
| 84 |
# Returns an Array of String arguments that were not parsed. |
| 85 |
def arguments |
| 86 |
parser.arguments |
| 87 |
end |
| 88 |
alias args arguments |
| 89 |
|
| 90 |
# Returns a hash with option key => value. |
| 91 |
def to_hash |
| 92 |
Hash[options.reject(&:null?).map { |o| [o.key, o.value] }] |
| 93 |
end |
| 94 |
alias to_h to_hash |
| 95 |
|
| 96 |
def to_s(**opts) |
| 97 |
options.to_s(**opts) |
| 98 |
end |
| 99 |
|
| 100 |
private |
| 101 |
|
| 102 |
def clean_key(key) |
| 103 |
key = key.to_s.sub(/\A--?/, '') |
| 104 |
key = key.tr '-', '_' if parser.config[:underscore_flags] |
| 105 |
key.to_sym |
| 106 |
end |
| 107 |
end |
| 108 |
end |